mirror of
https://github.com/ZoneMinder/zoneminder.git
synced 2026-12-04 01:00:16 -05:00
Merge pull request #6542 from AJ0070/fix-cookie-encoding
fix: encode and decode cookie values in skin.js
This commit is contained in:
2 files changed
+54
-1
No files matched your search
@@ -0,0 +1,46 @@
|
||||
'use strict';
|
||||
|
||||
// setCookie() wrote the value raw, so a typed filter value such as 'a;b' was cut
|
||||
// at the ';' and a '%41' was decoded by PHP into 'A'. getCookie() must decode
|
||||
// what setCookie() wrote and still accept an older unencoded value.
|
||||
|
||||
const assert = require('assert');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const vm = require('vm');
|
||||
|
||||
const skin = fs.readFileSync(
|
||||
path.join(__dirname, '../../web/skins/classic/js/skin.js'), 'utf8');
|
||||
|
||||
function slice(source, startMarker, endMarker) {
|
||||
const start = source.indexOf(startMarker);
|
||||
assert.ok(start >= 0, 'missing ' + startMarker);
|
||||
return source.slice(start, source.indexOf(endMarker, start) + endMarker.length);
|
||||
}
|
||||
|
||||
const jar = {value: ''};
|
||||
const ctx = {document: {}, sessionCookies: [], Date, JSON, decodeURIComponent, encodeURIComponent};
|
||||
Object.defineProperty(ctx.document, 'cookie', {
|
||||
get: () => jar.value,
|
||||
set: (v) => {
|
||||
// A browser keeps only name=value up to the first ';'
|
||||
jar.value = v.split(';')[0];
|
||||
},
|
||||
});
|
||||
vm.createContext(ctx);
|
||||
vm.runInContext(slice(skin, 'function isJSON', '\n}\n'), ctx);
|
||||
vm.runInContext(slice(skin, 'function setCookie', '\n}\n'), ctx);
|
||||
vm.runInContext(slice(skin, 'function getCookie', '\n}\n'), ctx);
|
||||
|
||||
for (const value of ['plain', 'a;b', '100%41', 'x=y', 'café bar']) {
|
||||
ctx.setCookie('zmFilter_MonitorName', value);
|
||||
assert.strictEqual(ctx.getCookie('zmFilter_MonitorName'), value, 'round trip of ' + JSON.stringify(value));
|
||||
}
|
||||
|
||||
ctx.setCookie('zmFilter_MonitorId', JSON.stringify([1, 2]));
|
||||
assert.deepStrictEqual(Array.from(ctx.getCookie('zmFilter_MonitorId')), [1, 2]);
|
||||
|
||||
jar.value = 'zmFilter_Source=50%';
|
||||
assert.strictEqual(ctx.getCookie('zmFilter_Source'), '50%', 'legacy value with a bare %');
|
||||
|
||||
console.log('ok');
|
||||
@@ -994,7 +994,9 @@ function setCookie(name, value, seconds) {
|
||||
// that cannot overflow a signed 32bit int in a client that converts it.
|
||||
expires = "; max-age=2147483647";
|
||||
}
|
||||
document.cookie = name + "=" + (newValue || "") + expires + "; path=/; samesite=strict";
|
||||
// Encoded so a ';' or '%' in a typed filter value is not cut off or decoded
|
||||
// twice; PHP decodes $_COOKIE and getCookie() below decodes it again.
|
||||
document.cookie = name + "=" + encodeURIComponent(newValue || "") + expires + "; path=/; samesite=strict";
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -1020,6 +1022,11 @@ function getCookie(name) {
|
||||
while (c.charAt(0)==' ') c = c.substring(1, c.length);
|
||||
if (c.indexOf(nameEQ) == 0) {
|
||||
result = c.substring(nameEQ.length, c.length);
|
||||
try {
|
||||
result = decodeURIComponent(result);
|
||||
} catch {
|
||||
// Written before values were encoded and holding a bare '%': keep it as is
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user