Skip to content

Commit 6e12922

Browse files
committed
fix: sanitize doublequote string to prevent xss
1 parent 642232b commit 6e12922

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/util/visit.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@ import {
66
Function_,
77
} from "../mod.ts"
88
import * as cg from "./codegen.ts"
9-
import {
9+
import type {
1010
Expression,
1111
} from "./codegen.ts"
1212

1313
import JSON5 from "https://esm.sh/[email protected]"
1414

15+
const stringExpr =
16+
(str: string) =>
17+
`"` + str.replaceAll(`"`, `\\"`) + `"` as Expression
18+
1519
export class Visitor {
1620
visitProject(project: Project) {
1721
return [
@@ -76,7 +80,7 @@ export class Visitor {
7680
[
7781
...this.paramsToExpressions(event.params),
7882
this.blockGroupToArrow(rest),
79-
`"$obj$"` as Expression,
83+
stringExpr("$obj$"),
8084
]
8185
)
8286
}
@@ -140,16 +144,17 @@ export class Visitor {
140144
return block.toString() as Expression
141145

142146
if (typeof block == "string")
143-
return `"${block}"` as Expression
147+
return stringExpr(block)
144148

145149
if (!block)
146150
return "" as Expression
147151

148152
if (block.type == "number")
149153
return block.params[0]!.toString() as Expression
150154

151-
if (block.type == "text")
152-
return `"${block.params[0]}"` as Expression
155+
if (block.type == "text") {
156+
return stringExpr(String(block.params[0] as string | number))
157+
}
153158

154159
if (
155160
block.type.startsWith("stringParam_")
@@ -176,7 +181,7 @@ export class Visitor {
176181
...this.paramsToExpressions(block.params),
177182
...block.statements
178183
.map(blockGroup => this.blockGroupToArrow.bind(this)(blockGroup)),
179-
`"$obj$"` as Expression,
184+
stringExpr("$obj$"),
180185
]
181186
)
182187
}

0 commit comments

Comments
 (0)