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: avoid to url-encode cookie value #27

Merged
merged 3 commits into from
Oct 30, 2021
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
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
},
"types": "index.d.ts",
"dependencies": {
"agent-base": "^6.0.2",
"cookie": "^0.4.1"
"agent-base": "^6.0.2"
},
"peerDependencies": {
"tough-cookie": "^4.0.0"
Expand All @@ -38,7 +37,6 @@
"@hapi/wreck": "17.1.0",
"@semantic-release/changelog": "5.0.1",
"@semantic-release/git": "9.0.1",
"@types/cookie": "0.4.1",
"@types/http-proxy": "1.17.7",
"@types/needle": "2.5.2",
"@types/node": "12.20.36",
Expand Down
22 changes: 22 additions & 0 deletions src/__tests__/http.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,28 @@ test('should send cookies from CookieJar', async (t) => {
t.plan(1);
});

test('should send cookies from CookieJar when value is url-encoded', async (t) => {
const jar = new CookieJar();
const agent = new HttpCookieAgent({ jar });

const { port } = await createTestServer([
(req, res) => {
t.is(req.headers['cookie'], 'key=hello%20world');
res.end();
},
]);

await jar.setCookie('key=hello%20world', `http://localhost:${port}`);

const { promise } = request(`http://localhost:${port}`, {
method: 'GET',
agent,
});
await promise;

t.plan(1);
});

test('should send cookies from both a request options and CookieJar', async (t) => {
const jar = new CookieJar();
const agent = new HttpCookieAgent({ jar });
Expand Down
30 changes: 17 additions & 13 deletions src/create_cookie_agent.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type http from 'http';
import liburl from 'url';
import libcookie from 'cookie';
import { CookieJar } from 'tough-cookie';
import { Cookie, CookieJar } from 'tough-cookie';

declare module 'http' {
interface Agent {
Expand Down Expand Up @@ -64,21 +63,26 @@ export function createCookieAgent<
const requestUrl = this[GET_REQUEST_URL](req);

const cookies = await this.jar.getCookies(requestUrl);
const cookiesEntries: [string, string][] = cookies.map((cookie) => [cookie.key, cookie.value]);
const cookiesMap = new Map(cookies.map((cookie) => [cookie.key, cookie]));

const cookieHeaderFromRequest = req.getHeader('Cookie');
if (Array.isArray(cookieHeaderFromRequest)) {
for (const str of cookieHeaderFromRequest) {
const cookie = libcookie.parse(str);
cookiesEntries.push(...Object.entries(cookie));
const cookieHeaderList = [req.getHeader('Cookie')].flat();

for (const header of cookieHeaderList) {
if (typeof header !== 'string') {
continue;
}

for (const str of header.split(';')) {
const cookie = Cookie.parse(str.trim());
if (cookie === undefined) {
continue;
}
cookiesMap.set(cookie.key, cookie);
}
} else if (typeof cookieHeaderFromRequest === 'string') {
const cookie = libcookie.parse(cookieHeaderFromRequest);
cookiesEntries.push(...Object.entries(cookie));
}

const cookieHeader = Array.from(new Map(cookiesEntries))
.map(([key, value]) => libcookie.serialize(key, value))
const cookieHeader = Array.from(cookiesMap.values())
.map((cookie) => cookie.cookieString())
.join(';\x20');

return cookieHeader;
Expand Down
10 changes: 0 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -490,11 +490,6 @@
resolved "https://registry.yarnpkg.com/@types/caseless/-/caseless-0.12.2.tgz#f65d3d6389e01eeb458bd54dc8f52b95a9463bc8"
integrity sha512-6ckxMjBBD8URvjB6J3NcnuAn5Pkl7t3TizAg+xdlzzQGSPSmBcXf8KoIH0ua/i+tio+ZRUHEXp0HEmvaR4kt0w==

"@types/cookie@0.4.1":
version "0.4.1"
resolved "https://registry.yarnpkg.com/@types/cookie/-/cookie-0.4.1.tgz#bfd02c1f2224567676c1545199f87c3a861d878d"
integrity sha512-XW/Aa8APYr6jSVVA1y/DEIZX0/GMKLEVekNG727R8cs56ahETkRAy/3DR7+fJyh7oUgGwNQaRfXCun0+KbWY7Q==

"@types/cookiejar@*":
version "2.1.2"
resolved "https://registry.yarnpkg.com/@types/cookiejar/-/cookiejar-2.1.2.tgz#66ad9331f63fe8a3d3d9d8c6e3906dd10f6446e8"
Expand Down Expand Up @@ -1471,11 +1466,6 @@ convert-to-spaces@^1.0.1:
resolved "https://registry.yarnpkg.com/convert-to-spaces/-/convert-to-spaces-1.0.2.tgz#7e3e48bbe6d997b1417ddca2868204b4d3d85715"
integrity sha1-fj5Iu+bZl7FBfdyihoIEtNPYVxU=

cookie@^0.4.1:
version "0.4.1"
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.4.1.tgz#afd713fe26ebd21ba95ceb61f9a8116e50a537d1"
integrity sha512-ZwrFkGJxUR3EIoXtO+yVE69Eb7KlixbaeAWfBQB9vVsNn/o+Yw69gBWSSDK825hQNdN+wF8zELf3dFNl/kxkUA==

cookiejar@^2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/cookiejar/-/cookiejar-2.1.2.tgz#dd8a235530752f988f9a0844f3fc589e3111125c"
Expand Down