Skip to content

Commit

Permalink
Merge pull request #267 from rathahin/fix-basic-auth
Browse files Browse the repository at this point in the history
Fix Basic Auth Configuration for Multiple Paths
  • Loading branch information
Baroshem committed Nov 1, 2023
2 parents 1f83cb2 + d8df6ed commit 76841ce
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
5 changes: 5 additions & 0 deletions SECURITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Security Policy

If you discover a vulnerability in the package, please raise an issue with the description of the security problem you have discovered.

Contributions with the fixes are welcome, so feel free to create a Pull Request with a fix :)
17 changes: 14 additions & 3 deletions src/runtime/server/middleware/basicAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,22 @@ const securityConfig = useRuntimeConfig().private

export default defineEventHandler((event) => {
const credentials = getCredentials(event.node.req)
const basicAuthConfig: BasicAuth = securityConfig.basicAuth
const basicAuthConfig = securityConfig.basicAuth

if (basicAuthConfig?.exclude?.some(el => event.path?.startsWith(el)) || basicAuthConfig?.include?.some(el => !event.path?.startsWith(el))) { return }
// Check for exclusion paths
const excludePaths = basicAuthConfig?.exclude || ['/']
const isPathExcluded = excludePaths.some(el => event.path?.startsWith(el))

if (!credentials || !validateCredentials(credentials!, basicAuthConfig)) {
// Check for inclusion paths
const includePaths = basicAuthConfig?.include || []
const isPathIncluded = includePaths.some(el => event.path?.startsWith(el))

if (isPathExcluded && !isPathIncluded) {
return
}

if (!credentials || !validateCredentials(credentials, basicAuthConfig)) {
// Set the authentication header and send an error response
setHeader(event, 'WWW-Authenticate', `Basic realm=${basicAuthConfig.message || 'Please enter username and password'}`)
sendError(event, createError({ statusCode: 401, statusMessage: 'Access denied' }))
}
Expand Down
24 changes: 24 additions & 0 deletions test/basicAuth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,28 @@ describe('[nuxt-security] Basic Auth', async () => {

expect(res.status).toBe(200)
})

it ('should return 401 status code for included route', async () => {
const res = await fetch('/api/hello/world/nuxt')

expect(res.status).toBe(401)
})

it ('should return 200 status code for multiple included route', async () => {
const res = await fetch('/admin')

expect(res.status).toBe(401)
})

it ('should return 200 status code for multiple excluded route', async () => {
const res = await fetch('/content')

expect(res.status).toBe(200)
})

it ('should return a 401 status code for any arbitrary path', async () => {
const res = await fetch('/arbitrary')

expect(res.status).toBe(401)
})
})
3 changes: 2 additions & 1 deletion test/fixtures/basicAuth/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export default defineNuxtConfig({
],
security: {
basicAuth: {
exclude: ['/api'],
exclude: ['/api', '/content'],
include: ['/api/hello/world', '/admin'],
name: 'test',
pass: 'test',
enabled: true,
Expand Down

0 comments on commit 76841ce

Please sign in to comment.