-
Notifications
You must be signed in to change notification settings - Fork 500
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cookies: improve validateCookieName (#2884)
- Loading branch information
Showing
3 changed files
with
164 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { bench, group, run } from 'mitata' | ||
import { validateCookieName } from '../../lib/web/cookies/util.js' | ||
|
||
const valid = 'Cat' | ||
|
||
group('validateCookieName', () => { | ||
bench(`valid: ${valid}`, () => { | ||
return validateCookieName(valid) | ||
}) | ||
}) | ||
|
||
await run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
'use strict' | ||
|
||
const { test, describe } = require('node:test') | ||
const { throws, strictEqual } = require('node:assert') | ||
|
||
const { | ||
validateCookieName | ||
} = require('../../lib/web/cookies/util') | ||
|
||
describe('validateCookieName', () => { | ||
test('should throw for CTLs', () => { | ||
throws(() => validateCookieName('\x00'), new Error('Invalid cookie name')) | ||
throws(() => validateCookieName('\x01'), new Error('Invalid cookie name')) | ||
throws(() => validateCookieName('\x02'), new Error('Invalid cookie name')) | ||
throws(() => validateCookieName('\x03'), new Error('Invalid cookie name')) | ||
throws(() => validateCookieName('\x04'), new Error('Invalid cookie name')) | ||
throws(() => validateCookieName('\x05'), new Error('Invalid cookie name')) | ||
throws(() => validateCookieName('\x06'), new Error('Invalid cookie name')) | ||
throws(() => validateCookieName('\x07'), new Error('Invalid cookie name')) | ||
throws(() => validateCookieName('\x08'), new Error('Invalid cookie name')) | ||
throws(() => validateCookieName('\x09'), new Error('Invalid cookie name')) | ||
throws(() => validateCookieName('\x0A'), new Error('Invalid cookie name')) | ||
throws(() => validateCookieName('\x0B'), new Error('Invalid cookie name')) | ||
throws(() => validateCookieName('\x0C'), new Error('Invalid cookie name')) | ||
throws(() => validateCookieName('\x0D'), new Error('Invalid cookie name')) | ||
throws(() => validateCookieName('\x0E'), new Error('Invalid cookie name')) | ||
throws(() => validateCookieName('\x0F'), new Error('Invalid cookie name')) | ||
throws(() => validateCookieName('\x10'), new Error('Invalid cookie name')) | ||
throws(() => validateCookieName('\x11'), new Error('Invalid cookie name')) | ||
throws(() => validateCookieName('\x12'), new Error('Invalid cookie name')) | ||
throws(() => validateCookieName('\x13'), new Error('Invalid cookie name')) | ||
throws(() => validateCookieName('\x14'), new Error('Invalid cookie name')) | ||
throws(() => validateCookieName('\x15'), new Error('Invalid cookie name')) | ||
throws(() => validateCookieName('\x16'), new Error('Invalid cookie name')) | ||
throws(() => validateCookieName('\x17'), new Error('Invalid cookie name')) | ||
throws(() => validateCookieName('\x18'), new Error('Invalid cookie name')) | ||
throws(() => validateCookieName('\x19'), new Error('Invalid cookie name')) | ||
throws(() => validateCookieName('\x1A'), new Error('Invalid cookie name')) | ||
throws(() => validateCookieName('\x1B'), new Error('Invalid cookie name')) | ||
throws(() => validateCookieName('\x1C'), new Error('Invalid cookie name')) | ||
throws(() => validateCookieName('\x1D'), new Error('Invalid cookie name')) | ||
throws(() => validateCookieName('\x1E'), new Error('Invalid cookie name')) | ||
throws(() => validateCookieName('\x1F'), new Error('Invalid cookie name')) | ||
throws(() => validateCookieName('\x7F'), new Error('Invalid cookie name')) | ||
}) | ||
|
||
test('should throw for " " character', () => { | ||
throws(() => validateCookieName(' '), new Error('Invalid cookie name')) | ||
}) | ||
|
||
test('should throw for Horizontal Tab character', () => { | ||
throws(() => validateCookieName('\t'), new Error('Invalid cookie name')) | ||
}) | ||
|
||
test('should throw for ; character', () => { | ||
throws(() => validateCookieName(';'), new Error('Invalid cookie name')) | ||
}) | ||
|
||
test('should throw for " character', () => { | ||
throws(() => validateCookieName('"'), new Error('Invalid cookie name')) | ||
}) | ||
|
||
test('should throw for , character', () => { | ||
throws(() => validateCookieName(','), new Error('Invalid cookie name')) | ||
}) | ||
|
||
test('should throw for \\ character', () => { | ||
throws(() => validateCookieName('\\'), new Error('Invalid cookie name')) | ||
}) | ||
|
||
test('should throw for ( character', () => { | ||
throws(() => validateCookieName('('), new Error('Invalid cookie name')) | ||
}) | ||
|
||
test('should throw for ) character', () => { | ||
throws(() => validateCookieName(')'), new Error('Invalid cookie name')) | ||
}) | ||
|
||
test('should throw for < character', () => { | ||
throws(() => validateCookieName('<'), new Error('Invalid cookie name')) | ||
}) | ||
|
||
test('should throw for > character', () => { | ||
throws(() => validateCookieName('>'), new Error('Invalid cookie name')) | ||
}) | ||
|
||
test('should throw for @ character', () => { | ||
throws(() => validateCookieName('@'), new Error('Invalid cookie name')) | ||
}) | ||
|
||
test('should throw for : character', () => { | ||
throws(() => validateCookieName(':'), new Error('Invalid cookie name')) | ||
}) | ||
|
||
test('should throw for / character', () => { | ||
throws(() => validateCookieName('/'), new Error('Invalid cookie name')) | ||
}) | ||
|
||
test('should throw for [ character', () => { | ||
throws(() => validateCookieName('['), new Error('Invalid cookie name')) | ||
}) | ||
|
||
test('should throw for ] character', () => { | ||
throws(() => validateCookieName(']'), new Error('Invalid cookie name')) | ||
}) | ||
|
||
test('should throw for ? character', () => { | ||
throws(() => validateCookieName('?'), new Error('Invalid cookie name')) | ||
}) | ||
|
||
test('should throw for = character', () => { | ||
throws(() => validateCookieName('='), new Error('Invalid cookie name')) | ||
}) | ||
|
||
test('should throw for { character', () => { | ||
throws(() => validateCookieName('{'), new Error('Invalid cookie name')) | ||
}) | ||
|
||
test('should throw for } character', () => { | ||
throws(() => validateCookieName('}'), new Error('Invalid cookie name')) | ||
}) | ||
|
||
test('should pass for a printable character', t => { | ||
strictEqual(validateCookieName('A'), undefined) | ||
strictEqual(validateCookieName('Z'), undefined) | ||
strictEqual(validateCookieName('a'), undefined) | ||
strictEqual(validateCookieName('z'), undefined) | ||
strictEqual(validateCookieName('!'), undefined) | ||
}) | ||
}) |