Skip to content

Commit

Permalink
Fix 'set-cookie' — case-insensitive values
Browse files Browse the repository at this point in the history
Allow to use predefined values in any case variation in set-cookie scriptlet
  • Loading branch information
AdamWr committed Jul 20, 2023
1 parent ec9b054 commit 460e3d6
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 14 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- ability to use flags in regular expression scriptlet parameters
[#303](https://github.com/AdguardTeam/Scriptlets/issues/303)

### Changed

- `set-cookie` and `set-cookie-reload` scriptlets to accept predefined values in any case variation
[#342](https://github.com/AdguardTeam/Scriptlets/issues/342)

### Fixed

- issue with overwriting `google.ima` value if it was already set
Expand Down
22 changes: 12 additions & 10 deletions src/helpers/cookie-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,21 @@ export const getLimitedCookieValue = (value: string): string | number | null =>
}

const allowedCookieValues = new Set([
'true', 'True',
'false', 'False',
'yes', 'Yes',
'y', 'Y',
'no', 'No',
'n', 'N',
'ok', 'OK',
'Accept', 'Reject',
'allow', 'deny',
'true',
'false',
'yes',
'y',
'no',
'n',
'ok',
'accept',
'reject',
'allow',
'deny',
]);

let validValue;
if (allowedCookieValues.has(value)) {
if (allowedCookieValues.has(value.toLowerCase())) {
validValue = value;
} else if (/^\d+$/.test(value)) {
validValue = parseFloat(value);
Expand Down
6 changes: 4 additions & 2 deletions src/scriptlets/set-cookie-reload.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ import {
* - `name` — required, cookie name to be set
* - `value` — required, cookie value; possible values:
* - number `>= 0 && <= 15`
* - one of the predefined constants:
* - one of the predefined constants in any case variation:
* - `true` / `True`
* - `false` / `False`
* - `yes` / `Yes` / `Y`
* - `no`
* - `no` / `NO` / `N` / `n`
* - `ok` / `OK`
* - `accept` / `ACCEPT`
* - `allow` / `deny` / `reject`
* - `path` — optional, cookie path, defaults to `/`; possible values:
* - `/` — root path
* - `none` — to set no path at all
Expand Down
6 changes: 4 additions & 2 deletions src/scriptlets/set-cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ import {
* - `name` — required, cookie name to be set
* - `value` — required, cookie value; possible values:
* - number `>= 0 && <= 15`
* - one of the predefined constants:
* - one of the predefined constants in any case variation:
* - `true` / `True`
* - `false` / `False`
* - `yes` / `Yes` / `Y`
* - `no`
* - `no` / `NO` / `N` / `n`
* - `ok` / `OK`
* - `accept` / `ACCEPT`
* - `allow` / `deny` / `reject`
* - `path` — optional, cookie path, defaults to `/`; possible values:
* - `/` — root path
* - `none` — to set no path at all
Expand Down
7 changes: 7 additions & 0 deletions tests/scriptlets/set-cookie.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ test('Set cookie with valid value', (assert) => {
assert.strictEqual(document.cookie.includes(cName) && document.cookie.includes(cValue), true, 'Cookie is set');
clearCookie(cName);

cName = '__test-cookie_dENy';
cValue = 'dENy';
runScriptlet(name, [cName, cValue]);
assert.strictEqual(window.hit, 'FIRED', 'Hit was fired');
assert.strictEqual(document.cookie.includes(cName) && document.cookie.includes(cValue), true, 'Cookie is set');
clearCookie(cName);

cName = '__test-cookie_0';
cValue = '0';
runScriptlet(name, [cName, cValue]);
Expand Down

0 comments on commit 460e3d6

Please sign in to comment.