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

fix: fix cookie parsing to allow invalid uri encoding #4915

Merged
merged 2 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 13 additions & 2 deletions packages/qwik-city/middleware/request-handler/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,26 @@ const createSetCookieValue = (cookieName: string, cookieValue: string, options:
return c.join('; ');
};

function tryDecodeUriComponent(str: string) {
if (str.indexOf('%') === -1) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this check is not-needed. If not %, then decodeURIComponent just returns a normal string.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, agreed. I added that only because the cookie library had something like that (probably for optimization?).

return str;
}
try {
return decodeURIComponent(str);
} catch {
return str;
}
}

const parseCookieString = (cookieString: string | undefined | null) => {
const cookie: Record<string, string> = {};
if (typeof cookieString === 'string' && cookieString !== '') {
const cookieSegments = cookieString.split(';');
for (const cookieSegment of cookieSegments) {
const separatorIndex = cookieSegment.indexOf('=');
if (separatorIndex !== -1) {
cookie[decodeURIComponent(cookieSegment.slice(0, separatorIndex).trim())] =
decodeURIComponent(cookieSegment.slice(separatorIndex + 1).trim());
cookie[tryDecodeUriComponent(cookieSegment.slice(0, separatorIndex).trim())] =
tryDecodeUriComponent(cookieSegment.slice(separatorIndex + 1).trim());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ test('parses cookie', () => {
a: 'hello=world',
b: '25',
c: '{"hello": "world"}',
d: '%badencoding',
};
const cookieString = Object.entries(cookieValues)
.reduce((prev: string[], [key, value]) => {
Expand All @@ -28,10 +29,11 @@ test('parses cookie', () => {
Object.entries(cookieValues).forEach(([key, value]) => {
equal(cookie.get(key)?.value, value);
});
equal(Object.keys(cookie.getAll()).length, 3);
equal(Object.keys(cookie.getAll()).length, 4);
equal(cookie.getAll().a.value, 'hello=world');
equal(cookie.getAll().b.number(), 25);
equal(cookie.getAll().c.json(), { hello: 'world' });
equal(cookie.getAll().d.value, '%badencoding');
});

test('creates correct headers', () => {
Expand Down
Loading