Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"test:pwa": "node scripts/validate-pwa.mjs",
"test:modules": "node scripts/validate-modules.mjs",
"test:health": "node scripts/validate-health.mjs",
"test": "npm run test:pwa && npm run test:modules && npm run test:health"
"test": "npm run test:pwa && npm run test:modules && npm run test:health && node scripts/validate-escape-html.mjs"
},
"dependencies": {
"express": "^4.22.2",
Expand Down
17 changes: 17 additions & 0 deletions scripts/validate-escape-html.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { escapeHTML } from '../src/html-utils.js';

const source = readFileSync('src/app.js', 'utf8');

assert.match(source, /import\s+\{\s*escapeHTML\s*\}\s+from\s+['"]\.\/html-utils\.js['"]/);
assert.equal(
escapeHTML(`<img src=x onerror="alert('xss')"> & waste`),
'&lt;img src=x onerror=&quot;alert(&#39;xss&#39;)&quot;&gt; &amp; waste',
);
assert.equal(escapeHTML(null), '');
assert.equal(escapeHTML(undefined), '');
assert.equal(escapeHTML(42), '42');
assert.equal(escapeHTML('Safe Provider'), 'Safe Provider');

console.log('escapeHTML regression check passed');
3 changes: 2 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { ReGenXRealtime } from './realtime-sync.js';
import { CloudSync } from './cloud-sync.js';
import { ESGReporter } from './esg-reporter.js';
import { AccessibilityManager } from './accessibility.js';
import { escapeHTML } from './html-utils.js';
const STORAGE_KEY_PREFIX = "regenx-v3:";
const TRUST_LEDGER_KEY = STORAGE_KEY_PREFIX + "trust-ledger";
const ESG_ALERTS_KEY = STORAGE_KEY_PREFIX + "esg-alerts";
Expand Down Expand Up @@ -5351,4 +5352,4 @@ if ('serviceWorker' in navigator) {
console.error('Service Worker registration failed:', error);
}
});
}
}
16 changes: 16 additions & 0 deletions src/html-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const HTML_ESCAPE_MAP = Object.freeze({
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#39;',
});

/**
* Escapes user-provided values before interpolating them into HTML strings.
* @param {unknown} value
* @returns {string}
*/
export function escapeHTML(value) {
return String(value ?? '').replace(/[&<>"']/g, (char) => HTML_ESCAPE_MAP[char]);
}