Skip to content

Commit 4d96741

Browse files
mahirhirposva
andauthored
fix(unplugin): reject invalid hex digits in [x+hh] character codes (#2744)
* fix(unplugin): reject invalid hex digits in [x+hh] character codes * refactor: review --------- Co-authored-by: greymoth <246701683+greymoth-jp@users.noreply.github.com> Co-authored-by: Eduardo San Martin Morote <posva13@gmail.com>
1 parent 80f3cfd commit 4d96741

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

packages/router/src/unplugin/core/tree.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,14 @@ describe('Tree', () => {
225225
)
226226
})
227227

228+
it('throws when only the second digit is not hex', () => {
229+
const tree = new PrefixTree(RESOLVED_OPTIONS)
230+
231+
expect(() => tree.insert('[x+1g]', '[x+1g].vue')).toThrow(
232+
/Invalid hex code "1g"/
233+
)
234+
})
235+
228236
it('throws on too many digits in hex code', () => {
229237
const tree = new PrefixTree(RESOLVED_OPTIONS)
230238

packages/router/src/unplugin/core/treeNodeValue.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ export const enum TreeNodeType {
99
param,
1010
}
1111

12+
const RE_HEX_CHARS = /^[0-9a-fA-F]{2}$/
13+
1214
/**
1315
* Internal merged-overrides shape used by tree nodes. Same structure as
1416
* {@link CustomRouteBlock} (the user-facing `<route>` block / `definePage()`
@@ -684,13 +686,15 @@ function parseFileSegment(
684686
`Invalid character code in segment "${segment}". Hex code must be exactly 2 digits, got "${buffer}"`
685687
)
686688
}
687-
const hexCode = parseInt(buffer, 16)
688-
if (!Number.isInteger(hexCode) || hexCode < 0 || hexCode > 255) {
689+
// `parseInt` stops at the first non-hex character, so a buffer like
690+
// `1g` would be leniently parsed as `0x1` instead of being rejected.
691+
// Require both characters to be hex digits before decoding.
692+
if (!RE_HEX_CHARS.test(buffer)) {
689693
throw new SyntaxError(
690694
`Invalid hex code "${buffer}" in segment "${segment}"`
691695
)
692696
}
693-
pathSegment += String.fromCharCode(hexCode)
697+
pathSegment += String.fromCharCode(parseInt(buffer, 16))
694698
}
695699
buffer = ''
696700
}

0 commit comments

Comments
 (0)