mirror of
https://github.com/denoland/deno.git
synced 2025-03-03 17:34:47 -05:00
feat(std/http): Validate cookie path value (#8457)
This commit is contained in:
parent
14877f7fe2
commit
2c00f6c548
2 changed files with 45 additions and 0 deletions
|
@ -70,6 +70,7 @@ function toString(cookie: Cookie): string {
|
||||||
out.push(`SameSite=${cookie.sameSite}`);
|
out.push(`SameSite=${cookie.sameSite}`);
|
||||||
}
|
}
|
||||||
if (cookie.path) {
|
if (cookie.path) {
|
||||||
|
validatePath(cookie.path);
|
||||||
out.push(`Path=${cookie.path}`);
|
out.push(`Path=${cookie.path}`);
|
||||||
}
|
}
|
||||||
if (cookie.expires) {
|
if (cookie.expires) {
|
||||||
|
@ -92,6 +93,27 @@ function validateCookieName(name: string | undefined | null): void {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validate Path Value.
|
||||||
|
* @see https://tools.ietf.org/html/rfc6265#section-4.1.2.4
|
||||||
|
* @param path Path value.
|
||||||
|
*/
|
||||||
|
function validatePath(path: string | null): void {
|
||||||
|
if (path == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (let i = 0; i < path.length; i++) {
|
||||||
|
const c = path.charAt(i);
|
||||||
|
if (
|
||||||
|
c < String.fromCharCode(0x20) || c > String.fromCharCode(0x7E) || c == ";"
|
||||||
|
) {
|
||||||
|
throw new Error(
|
||||||
|
path + ": Invalid cookie path char '" + c + "'",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse the cookies of the Server Request
|
* Parse the cookies of the Server Request
|
||||||
* @param req An object which has a `headers` property
|
* @param req An object which has a `headers` property
|
||||||
|
|
|
@ -65,6 +65,29 @@ Deno.test({
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Deno.test({
|
||||||
|
name: "Cookie Path Validation",
|
||||||
|
fn(): void {
|
||||||
|
const res: Response = {};
|
||||||
|
const path = "/;domain=sub.domain.com";
|
||||||
|
res.headers = new Headers();
|
||||||
|
assertThrows(
|
||||||
|
(): void => {
|
||||||
|
setCookie(res, {
|
||||||
|
name: "Space",
|
||||||
|
value: "Cat",
|
||||||
|
httpOnly: true,
|
||||||
|
secure: true,
|
||||||
|
path,
|
||||||
|
maxAge: 3,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
Error,
|
||||||
|
path + ": Invalid cookie path char ';'",
|
||||||
|
);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
Deno.test({
|
Deno.test({
|
||||||
name: "Cookie Delete",
|
name: "Cookie Delete",
|
||||||
fn(): void {
|
fn(): void {
|
||||||
|
|
Loading…
Add table
Reference in a new issue