forked from QuantumNous/new-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatus-code-risk-guard.ts
More file actions
103 lines (86 loc) · 3.26 KB
/
Copy pathstatus-code-risk-guard.ts
File metadata and controls
103 lines (86 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
Copyright (C) 2023-2026 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
const NON_REDIRECTABLE_STATUS_CODES = new Set([504, 524])
function parseStatusCodeKey(rawKey: string): number | null {
const normalized = rawKey.trim()
if (!/^[1-5]\d{2}$/.test(normalized)) return null
return Number.parseInt(normalized, 10)
}
function parseStatusCodeMappingTarget(rawValue: unknown): number | null {
if (typeof rawValue === 'number' && Number.isInteger(rawValue)) {
return rawValue >= 100 && rawValue <= 599 ? rawValue : null
}
if (typeof rawValue === 'string') {
const normalized = rawValue.trim()
if (!/^[1-5]\d{2}$/.test(normalized)) return null
const code = Number.parseInt(normalized, 10)
return code >= 100 && code <= 599 ? code : null
}
return null
}
export function collectInvalidStatusCodeEntries(
statusCodeMappingStr: string
): string[] {
if (!statusCodeMappingStr?.trim()) return []
let parsed: Record<string, unknown>
try {
parsed = JSON.parse(statusCodeMappingStr)
} catch {
return []
}
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) return []
const invalid: string[] = []
for (const [rawKey, rawValue] of Object.entries(parsed)) {
const fromCode = parseStatusCodeKey(rawKey)
const toCode = parseStatusCodeMappingTarget(rawValue)
if (fromCode === null || toCode === null) {
invalid.push(`${rawKey} → ${rawValue}`)
}
}
return invalid
}
export function collectDisallowedStatusCodeRedirects(
statusCodeMappingStr: string
): string[] {
if (!statusCodeMappingStr?.trim()) return []
let parsed: Record<string, unknown>
try {
parsed = JSON.parse(statusCodeMappingStr)
} catch {
return []
}
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) return []
const riskyMappings: string[] = []
for (const [rawFrom, rawTo] of Object.entries(parsed)) {
const fromCode = parseStatusCodeKey(rawFrom)
const toCode = parseStatusCodeMappingTarget(rawTo)
if (fromCode === null || toCode === null) continue
if (!NON_REDIRECTABLE_STATUS_CODES.has(fromCode)) continue
if (fromCode === toCode) continue
riskyMappings.push(`${fromCode} -> ${toCode}`)
}
return [...new Set(riskyMappings)].sort()
}
export function collectNewDisallowedStatusCodeRedirects(
originalStr: string,
currentStr: string
): string[] {
const currentRisky = collectDisallowedStatusCodeRedirects(currentStr)
if (currentRisky.length === 0) return []
const originalRiskySet = new Set(
collectDisallowedStatusCodeRedirects(originalStr)
)
return currentRisky.filter((mapping) => !originalRiskySet.has(mapping))
}