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 1aa0067
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 19 deletions.
17 changes: 4 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 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 @@ -110,5 +110,8 @@
},
"dependencies": {
"worktop": "0.7.3"
},
"overrides": {
"@types/node": "20.8.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
30 changes: 30 additions & 0 deletions test/esi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1911,3 +1911,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 1aa0067

Please sign in to comment.