Skip to content

fix: invalid cookie parsing for the "=" character #271

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

Merged
merged 1 commit into from
Jun 9, 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
58 changes: 58 additions & 0 deletions __tests__/cookies.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,39 @@ describe('Cookie Tests:', function() {
})
}) // end it

/**
* There is no definitive standard on what the cookie value can contain.
* The most restrictive definition I could find comes from Safari which only supports
* the ASCII character set, excluding semi-colon, comma, backslash, and white space.
*
* The % character is also ambiguous, as it is used as part of the URL encoded scheme. For the purpose of this test, we will leave this character out.
*
* @see {@link https://stackoverflow.com/a/1969339 | This StackOverflow answer which provides more context regarding the cookie value}
*/
it('Parse cookie with the entire supported set of ASCII characters', async function() {
let asciiCharacterSet = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~';

asciiCharacterSet =
asciiCharacterSet.replace(' ', '')
.replace(';', '')
.replace(',', '')
.replace('/', '')
.replace('%', '');

let _event = Object.assign({},event,{
path: '/cookieParse',
multiValueHeaders: {
cookie: [`test=${asciiCharacterSet}`]
}
})
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(JSON.parse(result.body)).toEqual({
cookies: {
test: asciiCharacterSet,
},
})
}) // end it

it('Parse & decode two cookies', async function() {
let _event = Object.assign({},event,{
path: '/cookieParse',
Expand Down Expand Up @@ -330,6 +363,31 @@ describe('Cookie Tests:', function() {
})
}) // end it

it('Parse & decode multiple cookies with the entire supported set of ASCII characters', async function() {
let asciiCharacterSet = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~';

asciiCharacterSet =
asciiCharacterSet.replace(' ', '')
.replace(';', '')
.replace(',', '')
.replace('/', '')
.replace('%', '');

let _event = Object.assign({},event,{
path: '/cookieParse',
multiValueHeaders: {
cookie: [`test=${asciiCharacterSet}; test2=${asciiCharacterSet}`]
}
})
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(JSON.parse(result.body)).toEqual({
cookies: {
test: asciiCharacterSet,
test2: asciiCharacterSet,
},
})
}) // end it

}) // end parse tests

describe("Clear", function() {
Expand Down
4 changes: 3 additions & 1 deletion lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ class REQUEST {
this.cookies = cookies.reduce((acc, cookie) => {
cookie = cookie.trim().split('=');
return Object.assign(acc, {
[cookie[0]]: UTILS.parseBody(decodeURIComponent(cookie[1])),
[cookie[0]]: UTILS.parseBody(
decodeURIComponent(cookie.slice(1).join('='))
),
});
}, {});

Expand Down
Loading