Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Check digest if body is present #1235

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading