Skip to content

Commit c0e123a

Browse files
committed
fix(http): escape req.query.to in replaceTemplates
1 parent 2ba03c3 commit c0e123a

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

src/node/http.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { normalize, Options } from "../common/util"
77
import { AuthType, DefaultedArgs } from "./cli"
88
import { commit, rootPath } from "./constants"
99
import { Heart } from "./heart"
10-
import { getPasswordMethod, IsCookieValidArgs, isCookieValid, sanitizeString } from "./util"
10+
import { getPasswordMethod, IsCookieValidArgs, isCookieValid, sanitizeString, escapeHtml } from "./util"
1111

1212
declare global {
1313
// eslint-disable-next-line @typescript-eslint/no-namespace
@@ -35,7 +35,7 @@ export const replaceTemplates = <T extends object>(
3535
...extraOpts,
3636
}
3737
return content
38-
.replace(/{{TO}}/g, (typeof req.query.to === "string" && req.query.to) || "/")
38+
.replace(/{{TO}}/g, (typeof req.query.to === "string" && escapeHtml(req.query.to)) || "/")
3939
.replace(/{{BASE}}/g, options.base)
4040
.replace(/{{CS_STATIC_BASE}}/g, options.csStaticBase)
4141
.replace(/"{{OPTIONS}}"/, `'${JSON.stringify(options)}'`)

src/node/routes/login.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ router.post("/", async (req, res) => {
112112

113113
throw new Error("Incorrect password")
114114
} catch (error) {
115-
const htmlToRender = await getRoot(req, error)
116-
res.send(htmlToRender)
115+
const renderedHtml = await getRoot(req, error)
116+
res.send(renderedHtml)
117117
}
118118
})

test/unit/routes/login.test.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
import { RateLimiter } from "../../../src/node/routes/login"
12
import * as httpserver from "../../utils/httpserver"
23
import * as integration from "../../utils/integration"
34

4-
import { RateLimiter } from "../../../src/node/routes/login"
5-
65
describe("login", () => {
76
describe("RateLimiter", () => {
87
it("should allow one try ", () => {
@@ -56,8 +55,12 @@ describe("login", () => {
5655
_codeServer = await integration.setup(["--auth=password"], "")
5756
})
5857

59-
afterEach(() => {
58+
afterEach(async () => {
6059
process.env.PASSWORD = previousEnvPassword
60+
if (_codeServer) {
61+
await _codeServer.close()
62+
_codeServer = undefined
63+
}
6164
})
6265

6366
it("should return HTML with 'Missing password' message", async () => {

0 commit comments

Comments
 (0)