Skip to content

Commit f65642f

Browse files
harlan-zwgithub-advanced-security[bot]
andauthoredMar 13, 2025··
fix: ensure json strings are properly encoded (#520)
* Potential fix for code scanning alert no. 2: Incomplete string escaping or encoding Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> * chore: bump tests * chore: bump snapshots --------- Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
1 parent 0a9d285 commit f65642f

File tree

3 files changed

+3
-2
lines changed

3 files changed

+3
-2
lines changed
 

‎packages/unhead/src/utils/templateParams.ts

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ function sub(p: TemplateParams, token: string, isJson = false) {
1919
if (val !== undefined) {
2020
return isJson
2121
? (val || '')
22+
.replace(/\\/g, '\\\\')
2223
.replace(/</g, '\\u003C')
2324
.replace(/"/g, '\\"')
2425
: val || ''

‎packages/unhead/test/unit/client/templateParams.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ describe('templateParams', () => {
132132
expect(await useDelayedSerializedDom()).toMatchInlineSnapshot(`
133133
"<!DOCTYPE html><html><head>
134134
135-
<title>Home &amp; //&lt;"With Encoding"&gt;\\</title><script type="application/json">{"title":"Home & //\\u003C\\"With Encoding\\">\\"}</script></head>
135+
<title>Home &amp; //&lt;"With Encoding"&gt;\\</title><script type="application/json">{"title":"Home & //\\u003C\\"With Encoding\\">\\\\"}</script></head>
136136
<body>
137137
138138
<div>

‎packages/unhead/test/unit/server/templateParams.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ describe('ssr templateParams', () => {
116116

117117
expect(headTags).toMatchInlineSnapshot(`
118118
"<title>Home &amp; &#x2F;&#x2F;&lt;&quot;With Encoding&quot;&gt;\\</title>
119-
<script type="application/json">{"title":"Home & //\\u003C\\"With Encoding\\">\\"}</script>"
119+
<script type="application/json">{"title":"Home & //\\u003C\\"With Encoding\\">\\\\"}</script>"
120120
`)
121121
})
122122

0 commit comments

Comments
 (0)
Please sign in to comment.