fix(security): reject all control bytes in Origin validation
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
2 files changed
+15
-2
No files matched your search
+1
-1
@@ -23,7 +23,7 @@ type Headers = Record<string, string>;
|
|||||||
const HSTS_DEFAULT_MAXAGE = 15552000; // 180 days
|
const HSTS_DEFAULT_MAXAGE = 15552000; // 180 days
|
||||||
|
|
||||||
// scheme "://" host [":" port], or literal "null". Rejects control chars/whitespace.
|
// scheme "://" host [":" port], or literal "null". Rejects control chars/whitespace.
|
||||||
const ORIGIN_RE = /^(?:null|[a-z][a-z0-9+.-]*:\/\/[^\s/?#]+)$/i;
|
const ORIGIN_RE = /^(?:null|[a-z][a-z0-9+.-]*:\/\/[^\s/?#\x00-\x1f\x7f]+)$/i;
|
||||||
export function isValidOrigin(o: string): boolean { return ORIGIN_RE.test(o); }
|
export function isValidOrigin(o: string): boolean { return ORIGIN_RE.test(o); }
|
||||||
|
|
||||||
export function mergeVary(existing: string | undefined, add: string): string {
|
export function mergeVary(existing: string | undefined, add: string): string {
|
||||||
|
|||||||
+14
-1
@@ -1,5 +1,5 @@
|
|||||||
import { describe, it, expect } from 'vitest';
|
import { describe, it, expect } from 'vitest';
|
||||||
import { buildSecurityHeaders, resolveCors, mergeVary } from '../src/security';
|
import { buildSecurityHeaders, resolveCors, mergeVary, isValidOrigin } from '../src/security';
|
||||||
|
|
||||||
describe('buildSecurityHeaders', () => {
|
describe('buildSecurityHeaders', () => {
|
||||||
it('default set (security:true), no HSTS over insecure', () => {
|
it('default set (security:true), no HSTS over insecure', () => {
|
||||||
@@ -53,6 +53,19 @@ describe('resolveCors', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('isValidOrigin', () => {
|
||||||
|
it('accepts valid origins and literal null', () => {
|
||||||
|
expect(isValidOrigin('https://a.com')).toBe(true);
|
||||||
|
expect(isValidOrigin('http://localhost:3000')).toBe(true);
|
||||||
|
expect(isValidOrigin('null')).toBe(true);
|
||||||
|
});
|
||||||
|
it('rejects CRLF and other control bytes', () => {
|
||||||
|
expect(isValidOrigin('https://a.com\r\nX: y')).toBe(false);
|
||||||
|
expect(isValidOrigin('https://a.com\x00evil')).toBe(false);
|
||||||
|
expect(isValidOrigin('https://a.com\x1bevil')).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('mergeVary', () => {
|
describe('mergeVary', () => {
|
||||||
it('appends, dedupes, handles empty', () => {
|
it('appends, dedupes, handles empty', () => {
|
||||||
expect(mergeVary(undefined, 'Origin')).toBe('Origin');
|
expect(mergeVary(undefined, 'Origin')).toBe('Origin');
|
||||||
|
|||||||
Reference in new issue
Block a user