formacao
This commit is contained in:
+30
-9
@@ -24,23 +24,41 @@ var verifySignature = async (base64Signature, value, secret) => {
|
||||
};
|
||||
var validCookieNameRegEx = /^[\w!#$%&'*.^`|~+-]+$/;
|
||||
var validCookieValueRegEx = /^[ !#-:<-[\]-~]*$/;
|
||||
var trimCookieWhitespace = (value) => {
|
||||
let start = 0;
|
||||
let end = value.length;
|
||||
while (start < end) {
|
||||
const charCode = value.charCodeAt(start);
|
||||
if (charCode !== 32 && charCode !== 9) {
|
||||
break;
|
||||
}
|
||||
start++;
|
||||
}
|
||||
while (end > start) {
|
||||
const charCode = value.charCodeAt(end - 1);
|
||||
if (charCode !== 32 && charCode !== 9) {
|
||||
break;
|
||||
}
|
||||
end--;
|
||||
}
|
||||
return start === 0 && end === value.length ? value : value.slice(start, end);
|
||||
};
|
||||
var parse = (cookie, name) => {
|
||||
if (name && cookie.indexOf(name) === -1) {
|
||||
return {};
|
||||
}
|
||||
const pairs = cookie.trim().split(";");
|
||||
const parsedCookie = {};
|
||||
for (let pairStr of pairs) {
|
||||
pairStr = pairStr.trim();
|
||||
const pairs = cookie.split(";");
|
||||
const parsedCookie = /* @__PURE__ */ Object.create(null);
|
||||
for (const pairStr of pairs) {
|
||||
const valueStartPos = pairStr.indexOf("=");
|
||||
if (valueStartPos === -1) {
|
||||
continue;
|
||||
}
|
||||
const cookieName = pairStr.substring(0, valueStartPos).trim();
|
||||
if (name && name !== cookieName || !validCookieNameRegEx.test(cookieName)) {
|
||||
const cookieName = trimCookieWhitespace(pairStr.substring(0, valueStartPos));
|
||||
if (name && name !== cookieName || !validCookieNameRegEx.test(cookieName) || cookieName in parsedCookie) {
|
||||
continue;
|
||||
}
|
||||
let cookieValue = pairStr.substring(valueStartPos + 1).trim();
|
||||
let cookieValue = trimCookieWhitespace(pairStr.substring(valueStartPos + 1));
|
||||
if (cookieValue.startsWith('"') && cookieValue.endsWith('"')) {
|
||||
cookieValue = cookieValue.slice(1, -1);
|
||||
}
|
||||
@@ -54,7 +72,7 @@ var parse = (cookie, name) => {
|
||||
return parsedCookie;
|
||||
};
|
||||
var parseSigned = async (cookie, secret, name) => {
|
||||
const parsedCookie = {};
|
||||
const parsedCookie = /* @__PURE__ */ Object.create(null);
|
||||
const secretKey = await getCryptoKey(secret);
|
||||
for (const [key, value] of Object.entries(parse(cookie, name))) {
|
||||
const signatureStartPos = value.lastIndexOf(".");
|
||||
@@ -72,6 +90,9 @@ var parseSigned = async (cookie, secret, name) => {
|
||||
return parsedCookie;
|
||||
};
|
||||
var _serialize = (name, value, opt = {}) => {
|
||||
if (!validCookieNameRegEx.test(name)) {
|
||||
throw new Error("Invalid cookie name");
|
||||
}
|
||||
let cookie = `${name}=${value}`;
|
||||
if (name.startsWith("__Secure-") && !opt.secure) {
|
||||
throw new Error("__Secure- Cookie must have Secure attributes");
|
||||
@@ -87,7 +108,7 @@ var _serialize = (name, value, opt = {}) => {
|
||||
throw new Error("__Host- Cookie must not have Domain attributes");
|
||||
}
|
||||
}
|
||||
for (const key of ["domain", "path"]) {
|
||||
for (const key of ["domain", "path", "sameSite", "priority"]) {
|
||||
if (opt[key] && /[;\r\n]/.test(opt[key])) {
|
||||
throw new Error(`${key} must not contain ";", "\\r", or "\\n"`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user