forked from Disciplr-Org/Disciplr-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurl.ts
More file actions
40 lines (36 loc) · 1.2 KB
/
Copy pathurl.ts
File metadata and controls
40 lines (36 loc) · 1.2 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
/**
* Validates if a URL is safe to render as a link.
* Only allows http and https schemes.
* Rejects javascript:, data:, and other potentially dangerous schemes.
*/
export function normalizeEvidenceUrl(value: string): string | null {
const trimmed = value.trim()
if (!trimmed) {
return null
}
// Reject raw control characters (including newlines/tabs) that some
// renderers mishandle. Allow percent-encoded control bytes (e.g. %0A).
// Matching raw control bytes is intentional here, hence the rule override.
// eslint-disable-next-line no-control-regex
if (/[\x00-\x1f\x7f]/.test(trimmed)) {
return null
}
try {
const parsed = new URL(trimmed)
if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
return null
}
// Reject userinfo-bearing URLs (e.g. https://trusted.com@evil.com or
// https://user:pass@host). This includes credentials provided percent-
// encoded; the URL parser exposes them on `username`/`password`.
if (parsed.username || parsed.password) {
return null
}
return trimmed
} catch {
return null
}
}
export function isSafeEvidenceUrl(value: string): boolean {
return normalizeEvidenceUrl(value) !== null
}