javascript / expert
Snippet
Verhinderung von Prototype-Pollution-Angriffen bei dynamischer JSON-Deserialisierung
Verhindert Prototype-Pollution-Schwachstellen beim JSON-Parsing in Svelte-Datenpipelines durch Ablehnung gefährlicher Eigenschaftsschlüssel und Trennung der Prototypen-Kette via Object.setPrototypeOf.
snippet.js
javascript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
export function safeReviver(key, value) {if (key === '__proto__' || key === 'constructor' || key === 'prototype') {throw new TypeError(`Forbidden property key detected during parsing: ${key}`);}return Object.freeze(value && typeof value === 'object' ? Object.setPrototypeOf(value, null) : value);}export function parseSveltePayload(jsonString) {try {return JSON.parse(jsonString, safeReviver);} catch (err) {console.error('Payload validation failure:', err.message);return Object.create(null);}}
svelte
Erklärung
1
if (key === '__proto__' || key === 'constructor' || key === 'prototype') {
Erkennt bösartige Eigenschaftsschlüssel, die den globalen Objekt-Prototyp manipulieren wollen.
2
throw new TypeError(`Forbidden property key detected during parsing: ${key}`);
Bricht den Parsing-Vorgang sofort ab, wenn verdächtige Strukturen erkannt werden.
3
return Object.freeze(value && typeof value === 'object' ? Object.setPrototypeOf(value, null) : value);
Entfernt vererbte Prototyp-Methoden und friert verarbeitete Datenobjekte unveränderlich ein.
4
return Object.create(null);
Gibt im Fehlerfall ein reines Fallback-Objekt ohne Standard-Prototyp-Eigenschaften zurück.