Skip to content

Commit 92bb54a

Browse files
fix(config): reject partial numeric strings in parseEnvInt
1 parent 1165019 commit 92bb54a

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

src/config.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ export type PluginConfig = {
1515
export function parseEnvInt(key: string, fallback: number): number {
1616
const raw = process.env[key]
1717
if (!raw) return fallback
18-
const n = parseInt(raw, 10)
19-
return Number.isFinite(n) && n > 0 ? n : fallback
18+
if (!/^[1-9]\d*$/.test(raw)) return fallback
19+
const n = Number(raw)
20+
return Number.isSafeInteger(n) ? n : fallback
2021
}
2122

2223
/**

tests/config.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ describe("parseEnvInt", () => {
2929

3030
test("returns fallback for float string", () => {
3131
process.env["TEST_INT"] = "1.5"
32-
expect(parseEnvInt("TEST_INT", 42)).toBe(1)
32+
expect(parseEnvInt("TEST_INT", 42)).toBe(42)
33+
})
34+
35+
test("returns fallback for partial numeric string", () => {
36+
process.env["TEST_INT"] = "5000ms"
37+
expect(parseEnvInt("TEST_INT", 42)).toBe(42)
3338
})
3439

3540
afterEach(() => { delete process.env["TEST_INT"] })

0 commit comments

Comments
 (0)