-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathscript_guard.ts
More file actions
53 lines (40 loc) · 1.51 KB
/
script_guard.ts
File metadata and controls
53 lines (40 loc) · 1.51 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
import { createScriptGuard } from '../../shared/script_guard';
const SOURCEPOINT_CDN_HOST = 'cdn.privacy-mgmt.com';
function normalizeSourcepointUrl(url: string): string | null {
if (!url) return null;
const trimmed = url.trim();
if (!trimmed) return null;
if (trimmed.startsWith('//')) return `https:${trimmed}`;
if (trimmed.startsWith('http://') || trimmed.startsWith('https://')) return trimmed;
// Bare domain or path — attempt to parse as https URL.
// The host === check in isSourcepointUrl rejects non-matching domains.
return `https://${trimmed}`;
}
function parseSourcepointUrl(url: string): URL | null {
const normalized = normalizeSourcepointUrl(url);
if (!normalized) return null;
try {
return new URL(normalized);
} catch {
return null;
}
}
export function isSourcepointUrl(url: string): boolean {
const parsed = parseSourcepointUrl(url);
return parsed?.host === SOURCEPOINT_CDN_HOST;
}
export function rewriteSourcepointUrl(originalUrl: string): string {
const parsed = parseSourcepointUrl(originalUrl);
if (!parsed) return originalUrl;
const query = parsed.search || '';
return `${window.location.origin}/integrations/sourcepoint/cdn${parsed.pathname}${query}`;
}
const guard = createScriptGuard({
displayName: 'Sourcepoint',
id: 'sourcepoint',
isTargetUrl: isSourcepointUrl,
rewriteUrl: rewriteSourcepointUrl,
});
export const installSourcepointGuard = guard.install;
export const isGuardInstalled = guard.isInstalled;
export const resetGuardState = guard.reset;