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

cookies: fix validateCookiePath #2866

Merged
merged 2 commits into from
Feb 28, 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: 8 additions & 3 deletions lib/web/cookies/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,14 @@ function validateCookieValue (value) {
* @param {string} path
*/
function validateCookiePath (path) {
for (const char of path) {
const code = char.charCodeAt(0)
for (let i = 0; i < path.length; ++i) {
const code = path.charCodeAt(i)

if (code < 0x21 || char === ';') {
if (
code < 0x20 || // exclude CTLs (0-31)
code === 0x7F || // DEL
code === 0x3B // ;
) {
throw new Error('Invalid cookie path')
}
}
Expand Down Expand Up @@ -281,6 +285,7 @@ function getHeadersList (headers) {

module.exports = {
isCTLExcludingHtab,
validateCookiePath,
toIMFDate,
stringify,
getHeadersList
Expand Down
59 changes: 59 additions & 0 deletions test/cookie/validate-cookie-path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict'

const { test, describe } = require('node:test')
const { throws, strictEqual } = require('node:assert')

const {
validateCookiePath
} = require('../../lib/web/cookies/util')

describe('validateCookiePath', () => {
test('should throw for CTLs', () => {
throws(() => validateCookiePath('\x00'))
throws(() => validateCookiePath('\x01'))
throws(() => validateCookiePath('\x02'))
throws(() => validateCookiePath('\x03'))
throws(() => validateCookiePath('\x04'))
throws(() => validateCookiePath('\x05'))
throws(() => validateCookiePath('\x06'))
throws(() => validateCookiePath('\x07'))
throws(() => validateCookiePath('\x08'))
throws(() => validateCookiePath('\x09'))
throws(() => validateCookiePath('\x0A'))
throws(() => validateCookiePath('\x0B'))
throws(() => validateCookiePath('\x0C'))
throws(() => validateCookiePath('\x0D'))
throws(() => validateCookiePath('\x0E'))
throws(() => validateCookiePath('\x0F'))
throws(() => validateCookiePath('\x10'))
throws(() => validateCookiePath('\x11'))
throws(() => validateCookiePath('\x12'))
throws(() => validateCookiePath('\x13'))
throws(() => validateCookiePath('\x14'))
throws(() => validateCookiePath('\x15'))
throws(() => validateCookiePath('\x16'))
throws(() => validateCookiePath('\x17'))
throws(() => validateCookiePath('\x18'))
throws(() => validateCookiePath('\x19'))
throws(() => validateCookiePath('\x1A'))
throws(() => validateCookiePath('\x1B'))
throws(() => validateCookiePath('\x1C'))
throws(() => validateCookiePath('\x1D'))
throws(() => validateCookiePath('\x1E'))
throws(() => validateCookiePath('\x1F'))
throws(() => validateCookiePath('\x7F'))
})

test('should throw for ; character', () => {
throws(() => validateCookiePath(';'))
})

test('should pass for a printable character', t => {
strictEqual(validateCookiePath('A'), undefined)
strictEqual(validateCookiePath('Z'), undefined)
strictEqual(validateCookiePath('a'), undefined)
strictEqual(validateCookiePath('z'), undefined)
strictEqual(validateCookiePath('!'), undefined)
strictEqual(validateCookiePath(' '), undefined)
})
})
Loading