Skip to content

Commit

Permalink
refactor(secure-headers): optimize getPermissionsPolicyDirectives fun…
Browse files Browse the repository at this point in the history
…ction
  • Loading branch information
kbkn3 committed Sep 9, 2024
1 parent c2b0de4 commit 8add1b7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 23 deletions.
49 changes: 29 additions & 20 deletions src/middleware/secure-headers/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,27 +160,36 @@ describe('Secure Headers Middleware', () => {
expect(res.headers.get('X-XSS-Protection')).toEqual('1')
})

it('should set Permission-Policy header', async () => {
const app = new Hono()
app.use(
'/test',
secureHeaders({
permissionsPolicy: {
fullscreen: ['self'],
bluetooth: ['none'],
payment: ['self', 'example.com'],
syncXhr: [],
camera: false,
microphone: true,
geolocation: ['*'],
},
})
)
describe('secureHeaders middleware', () => {
it('should set Permission-Policy header', async () => {
const app = new Hono()
app.use(
'/test',
secureHeaders({
permissionsPolicy: {
fullscreen: ['self'],
bluetooth: ['none'],
payment: ['self', 'https://example.com'],
syncXhr: [],
camera: false,
microphone: true,
geolocation: ['*'],
usb: ['self', 'https://a.example.com', 'https://b.example.com'],
accelerometer: ['https://*.example.com'],
gyroscope: ['src'],
magnetometer: ['https://a.example.com', 'https://b.example.com'],
},
})
)

const res = await app.request('/test')
expect(res.headers.get('Permissions-Policy')).toEqual(
'fullscreen=(self), bluetooth=(none), payment=(self example.com), sync-xhr=(), camera=none, microphone=(), geolocation=(*)'
)
const res = await app.request('/test')
expect(res.headers.get('Permissions-Policy')).toEqual(
'fullscreen=(self), bluetooth=(none), payment=(self "https://example.com"), sync-xhr=(), camera=none, microphone=*, ' +
'geolocation=(*), usb=(self "https://a.example.com" "https://b.example.com"), ' +
'accelerometer=("https://*.example.com"), gyroscope=(src), ' +
'magnetometer=("https://a.example.com" "https://b.example.com")'
)
})
})
it('CSP Setting', async () => {
const app = new Hono()
Expand Down
12 changes: 9 additions & 3 deletions src/middleware/secure-headers/secure-headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,18 @@ function getPermissionsPolicyDirectives(policy: PermissionsPolicyOptions): strin
const kebabDirective = camelToKebab(directive)

if (typeof value === 'boolean') {
return `${kebabDirective}=${value ? '()' : 'none'}`
return `${kebabDirective}=${value ? '*' : 'none'}`
}

if (Array.isArray(value)) {
const allowlist = value.length === 0 ? '()' : `(${value.join(' ')})`
return `${kebabDirective}=${allowlist}`
const allowlist = value.map((item) => {
if (item === 'self' || item === 'src' || item === 'none' || item === '*') {
return item
}
return `"${item}"`
})
const allowlistString = allowlist.length === 0 ? '()' : `(${allowlist.join(' ')})`
return `${kebabDirective}=${allowlistString}`
}

return ''
Expand Down

0 comments on commit 8add1b7

Please sign in to comment.