@blocklet/pages-kit 0.5.37 → 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/cjs/utils/preload.js
CHANGED
|
@@ -1,7 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.safeJSONStringify = safeJSONStringify;
|
|
3
4
|
exports.injectPreloadComponents = injectPreloadComponents;
|
|
4
5
|
const types_1 = require("../types");
|
|
6
|
+
/**
|
|
7
|
+
* Safely stringify JSON data to prevent XSS injection by escaping dangerous characters
|
|
8
|
+
* @param obj - The object to stringify
|
|
9
|
+
* @returns Safe JSON string with escaped characters
|
|
10
|
+
*/
|
|
11
|
+
function safeJSONStringify(obj) {
|
|
12
|
+
return JSON.stringify(obj).replace(/[<>/]/g, (char) => {
|
|
13
|
+
switch (char) {
|
|
14
|
+
case '<':
|
|
15
|
+
return '\\u003c';
|
|
16
|
+
case '>':
|
|
17
|
+
return '\\u003e';
|
|
18
|
+
case '/':
|
|
19
|
+
return '\\u002f';
|
|
20
|
+
default:
|
|
21
|
+
return char;
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
}
|
|
5
25
|
function injectPreloadComponents(data) {
|
|
6
26
|
if (!data)
|
|
7
27
|
return null;
|
|
@@ -14,9 +34,6 @@ function injectPreloadComponents(data) {
|
|
|
14
34
|
})),
|
|
15
35
|
instances: Object.fromEntries(data.instances.map(({ id, ...instance }) => [id, instance])),
|
|
16
36
|
};
|
|
17
|
-
// Base64 encode the JSON data to prevent XSS injection
|
|
18
|
-
const jsonString = JSON.stringify(injectState);
|
|
19
|
-
const base64Data = Buffer.from(jsonString, 'utf8').toString('base64');
|
|
20
37
|
return {
|
|
21
38
|
html: `\
|
|
22
39
|
${Object.values(data.components)
|
|
@@ -28,7 +45,7 @@ ${script}
|
|
|
28
45
|
.join('\n')}
|
|
29
46
|
|
|
30
47
|
<script>
|
|
31
|
-
var ${types_1.PreloadComponentsStateGlobalVariableName} =
|
|
48
|
+
var ${types_1.PreloadComponentsStateGlobalVariableName} = ${safeJSONStringify(injectState)}
|
|
32
49
|
</script>
|
|
33
50
|
`,
|
|
34
51
|
};
|