Skip to content

Commit e1d2435

Browse files
feat(routeNormalization): extract and enhance route normalization service
- Extract normalizeRouteRecord from Validation.vue to dedicated service - Add explicit array rejection to prevent numeric keys (e.g., {'0': 'a', '1': 'b'}) - Type-cast string values explicitly for type safety - Drop non-string values with logging - Improve contract enforcement for route params/query handling Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent 587407d commit e1d2435

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

src/services/routeNormalization.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2026 LibreSign contributors
3+
* SPDX-License-Identifier: AGPL-3.0-or-later
4+
*/
5+
6+
import { getLoggerBuilder } from '@nextcloud/logger'
7+
8+
const logger = getLoggerBuilder()
9+
.setApp('libresign')
10+
.detectUser()
11+
.build()
12+
13+
/**
14+
* Normalizes route params/query values to string-only object.
15+
* Explicitly rejects arrays (which Vue Router may provide as repeated params/query values)
16+
* to prevent numeric keys from being inserted.
17+
*
18+
* @param value - The value to normalize (typically from route.params or route.query)
19+
* @param source - The source type for logging ('params' or 'query')
20+
* @returns Record with only string values, or empty object for invalid input
21+
*/
22+
export function normalizeRouteRecord(
23+
value: unknown,
24+
source: 'params' | 'query',
25+
): Record<string, string> {
26+
if (typeof value !== 'object' || value === null) {
27+
return {}
28+
}
29+
30+
if (Array.isArray(value)) {
31+
logger.warn('Validation route normalization rejected array input', {
32+
source,
33+
})
34+
return {}
35+
}
36+
37+
const result: Record<string, string> = {}
38+
const droppedKeys: string[] = []
39+
for (const [key, entry] of Object.entries(value)) {
40+
if (typeof entry === 'string') {
41+
result[key] = entry
42+
} else {
43+
droppedKeys.push(key)
44+
}
45+
}
46+
47+
if (droppedKeys.length > 0) {
48+
logger.warn('Validation route normalization dropped non-string entries', {
49+
source,
50+
droppedKeys,
51+
})
52+
}
53+
54+
return result
55+
}

0 commit comments

Comments
 (0)