Skip to content

Commit

Permalink
fix: handles esi Args that are strings but have leading integers
Browse files Browse the repository at this point in the history
parseInt() returns a valid number if any of the leading characters are ints
it drops proceeding in the string if it doesn't match
  • Loading branch information
cdloh committed Jun 5, 2024
1 parent 40674a2 commit 6e9076d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"scripts": {
"build": "tsc",
"build-worker": "node worker/build.js",
"test": "npm run-script build-worker && node --experimental-vm-modules node_modules/jest/bin/jest.js --verbose --collectCoverage --coverageProvider=v8",
"test-debug": "npm run-script build-worker && node --experimental-vm-modules --inspect-brk node_modules/jest/bin/jest.js --runInBand",
"test": "npm run-script build-worker && node --no-experimental-fetch --experimental-vm-modules node_modules/jest/bin/jest.js --verbose --collectCoverage --coverageProvider=v8",
"test-debug": "npm run-script build-worker && node --no-experimental-fetch --experimental-vm-modules --inspect-brk node_modules/jest/bin/jest.js --runInBand",
"format": "prettier --write '*.{json,js}' 'src/**/*.{js,ts}' 'test/**/*.{js,ts}'",
"lint": "eslint --max-warnings=0 src && prettier --check '*.{json,js}' 'src/**/*.{js,ts}' 'test/**/*.{js,ts}'",
"prepack": "npm run-script test && npm run-script lint && npm run-script build",
Expand Down Expand Up @@ -111,4 +111,4 @@
"dependencies": {
"worktop": "0.7.3"
}
}
}
17 changes: 13 additions & 4 deletions src/processConditionals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,25 @@ function esi_eval_var_in_when_tag(
match: [String: string, ...args: string[]],
): string {
const varInTag = esi_eval_var(eventData, match);

const number = parseInt(varInTag, 10);
if (number) {
return number.toString();
// we have to check varInTag is *actually* a number and doesn't just have leading numbers in it
if (strIsNumber(varInTag)) {
return parseInt(varInTag, 10).toString();
} else {
// Change to replaceAll once upgraded node
return "'" + varInTag.replace(/'/g, "\\'") + "'";
}
}

/**
* Evaluates str and returns if it *actually* is a number not just a str with leading numbers
*
* @param {string} str string to check
* @returns {boolean} check result
*/
function strIsNumber(str: string): boolean {
return !isNaN(Number(str))
}

/**
* Takes a condition string and splits it into its two sides and operator
* passes that to the tester and returns the result
Expand Down
32 changes: 32 additions & 0 deletions test/esi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@ test("TEST 9e: List variable syntax (accept-language) with multiple headers", as
);

const res = await makeRequest(`${url}?t=1`, {
// @ts-ignore
headers: {
// @ts-ignore
"Accept-Language": ["da, en-gb", "fr"],
Expand Down Expand Up @@ -1063,6 +1064,7 @@ test("TEST 20b: Test we advertise Surrogate-Capability (with COLO Data)", async
res.writeHead(200, esiHead);
res.end(req.headers["surrogate-capability"]);
});
// @ts-ignore
const res = await makeRequest(url, { cf: { colo: "DFW" } });
expect(res.ok).toBeTruthy();
expect(checkSurrogate(res)).toBeTruthy();
Expand Down Expand Up @@ -1911,3 +1913,33 @@ test("TEST 50: Multiple ESI Args make it all the way through", async () => {
`esi_args1=1&esi_args2=2&esi_args3=3&esi_args4=4`,
);
});

describe("TEST 51: ESI Args that lead with ints shouldn't convert to ints", () => {
const checks = [
{ arg: "1719,2405", result: "first-lineage" },
{ arg: "1719,1732", result: "second-lineage" },
{ arg: "1719,1918", result: "third-lineage" },
{ arg: "1719,1922", result: "forth-lineage" },
{ arg: "1719,1926", result: "fith-lineage" },
];

const url = `/esi/test-51`;
checks.forEach(function (check) {
it(check.result, async () => {
routeHandler.add(url, function (req, res) {
res.writeHead(200, esiHead);
res.end(`<esi:choose>
<esi:when test="$(ESI_ARGS{lineage}) =~ '/(?:^|,)(2405)(?:,|$)/'">first-lineage</esi:when>
<esi:when test="$(ESI_ARGS{lineage}) =~ '/(?:^|,)(1732)(?:,|$)/'">second-lineage</esi:when>
<esi:when test="$(ESI_ARGS{lineage}) =~ '/(?:^|,)(1918)(?:,|$)/'">third-lineage</esi:when>
<esi:when test="$(ESI_ARGS{lineage}) =~ '/(?:^|,)(1922)(?:,|$)/'">forth-lineage</esi:when>
<esi:when test="$(ESI_ARGS{lineage}) =~ '/(?:^|,)(1926)(?:,|$)/'">fith-lineage</esi:when>
</esi:choose>`);
});
const res = await makeRequest(`${url}?esi_lineage=${check.arg}`);
expect(res.ok).toBeTruthy();
expect(await res.text()).toEqual(check.result);
expect(checkSurrogate(res)).toBeTruthy();
});
});
});

0 comments on commit 6e9076d

Please sign in to comment.