Skip to content

Commit

Permalink
Check if body (#1235)
Browse files Browse the repository at this point in the history
  • Loading branch information
ukstv authored Jul 10, 2024
1 parent a4495be commit 9f3e4be
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
11 changes: 9 additions & 2 deletions src/auth/__tests__/auth.middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ describe('Auth lambda', () => {
app.post('/', (req, res) => {
res.json({ hello: 'world' })
})
app.get('/', (req, res) => {
res.json({ hello: 'world' })
})
})

test('valid digest', async () => {
Expand All @@ -175,7 +178,7 @@ describe('Auth lambda', () => {
.set('did', did.id)
.set('digest', cid.toString())
.send(Buffer.from(carFile.bytes)) // Supertest quirk
expect(response.status).toBe(200)
expect(response.status).toEqual(200)
})
test('invalid digest', async () => {
const carFile = carFactory.build()
Expand All @@ -185,7 +188,11 @@ describe('Auth lambda', () => {
.set('did', did.id)
.set('digest', 'INVALID')
.send(Buffer.from(carFile.bytes)) // Supertest quirk
expect(response.status).toBe(403)
expect(response.status).toEqual(403)
})
test('get', async () => {
const response = await supertest(app).get('/')
expect(response.status).toEqual(200)
})
})

Expand Down
2 changes: 1 addition & 1 deletion src/auth/auth.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function auth(opts: AuthOpts): Handler {

// Use auth lambda
const didFromHeader = req.header('did')
if (didFromHeader && req.body) {
if (didFromHeader && req.body && Object.keys(req.body).length > 0) {
const digest = buildBodyDigest(req.header('Content-Type'), req.body)
if (req.header('digest') === digest) {
ServiceMetrics.count(METRIC_NAMES.AUTH_ALLOWED, 1, { did: didFromHeader })
Expand Down

0 comments on commit 9f3e4be

Please sign in to comment.