@blocklet/pages-kit 0.5.36 → 0.5.38
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/esm/utils/preload.js
CHANGED
|
@@ -1,4 +1,23 @@
|
|
|
1
1
|
import { PreloadComponentScriptModule, PreloadComponentsStateGlobalVariableName, } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Safely stringify JSON data to prevent XSS injection by escaping dangerous characters
|
|
4
|
+
* @param obj - The object to stringify
|
|
5
|
+
* @returns Safe JSON string with escaped characters
|
|
6
|
+
*/
|
|
7
|
+
export function safeJSONStringify(obj) {
|
|
8
|
+
return JSON.stringify(obj).replace(/[<>/]/g, (char) => {
|
|
9
|
+
switch (char) {
|
|
10
|
+
case '<':
|
|
11
|
+
return '\\u003c';
|
|
12
|
+
case '>':
|
|
13
|
+
return '\\u003e';
|
|
14
|
+
case '/':
|
|
15
|
+
return '\\u002f';
|
|
16
|
+
default:
|
|
17
|
+
return char;
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
}
|
|
2
21
|
export function injectPreloadComponents(data) {
|
|
3
22
|
if (!data)
|
|
4
23
|
return null;
|
|
@@ -22,7 +41,7 @@ ${script}
|
|
|
22
41
|
.join('\n')}
|
|
23
42
|
|
|
24
43
|
<script>
|
|
25
|
-
var ${PreloadComponentsStateGlobalVariableName} = ${
|
|
44
|
+
var ${PreloadComponentsStateGlobalVariableName} = ${safeJSONStringify(injectState)}
|
|
26
45
|
</script>
|
|
27
46
|
`,
|
|
28
47
|
};
|