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

feat(http): Add Cookie domain validation #1009

Merged
merged 3 commits into from
Jul 8, 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
21 changes: 20 additions & 1 deletion http/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ function toString(cookie: Cookie): string {
out.push(`Max-Age=${cookie.maxAge}`);
}
if (cookie.domain) {
validateDomain(cookie.domain);
out.push(`Domain=${cookie.domain}`);
}
if (cookie.sameSite) {
Expand Down Expand Up @@ -115,7 +116,7 @@ function validatePath(path: string | null): void {
}

/**
*Validate Cookie Value.
* Validate Cookie Value.
* @see https://tools.ietf.org/html/rfc6265#section-4.1
* @param value Cookie value.
*/
Expand All @@ -141,6 +142,24 @@ function validateValue(name: string, value: string | null): void {
}
}

/**
* Validate Cookie Domain.
* @see https://datatracker.ietf.org/doc/html/rfc6265#section-4.1.2.3
* @param domain Cookie domain.
*/
function validateDomain(domain: string): void {
if (domain == null) {
return;
}
const char1 = domain.charAt(0);
const charN = domain.charAt(domain.length - 1);
if (char1 == "-" || charN == "." || charN == "-") {
throw new Error(
"Invalid first/last char in cookie domain: " + domain,
);
}
}

/**
* Parse the cookies of the Server Request
* @param req An object which has a `headers` property
Expand Down
25 changes: 25 additions & 0 deletions http/cookie_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,31 @@ Deno.test({
},
});

Deno.test({
name: "Cookie Domain Validation",
fn(): void {
const res: Response = {};
const tokens = ["-domain.com", "domain.org.", "domain.org-"];
res.headers = new Headers();
tokens.forEach((domain) => {
assertThrows(
(): void => {
setCookie(res, {
name: "Space",
value: "Cat",
httpOnly: true,
secure: true,
domain,
maxAge: 3,
});
},
Error,
"Invalid first/last char in cookie domain: " + domain,
);
});
},
});

Deno.test({
name: "Cookie Delete",
fn(): void {
Expand Down