@revclamp/google-ads-transparency-mcp-actor 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,69 @@
1
+ /**
2
+ * Self-Contained Outbound SSRF & Host-Pinning Guard (src/egress_guard.mjs)
3
+ *
4
+ * Enforces HTTPS-only egress, blocks private/loopback/metadata/IP-literal hosts,
5
+ * and pins outbound requests to canonical Google Ads Transparency hosts.
6
+ */
7
+
8
+ export const ALLOWED_GOOGLE_ADS_RPC_HOSTS = new Set([
9
+ 'adstransparency.google.com',
10
+ 'displayads-formats.googleusercontent.com',
11
+ 'tpc.googlesyndication.com',
12
+ ]);
13
+
14
+ const SSRF_BLOCKED_HOST_RE =
15
+ /^(localhost|.*\.localhost|.*\.local|.*\.internal|.*\.arpa|.*\.nip\.io|.*\.sslip\.io|metadata\.google\.internal|\d+\.\d+\.\d+\.\d+|\[.*\])$/i;
16
+
17
+ /**
18
+ * Validates whether a target URL is safe from SSRF against internal/cloud metadata networks.
19
+ * @param {string} rawUrl
20
+ * @returns {{safe: boolean, hostname?: string, reason?: string}}
21
+ */
22
+ export function verifyEgressUrlSsrfSafe(rawUrl) {
23
+ if (!rawUrl || typeof rawUrl !== 'string') {
24
+ return { safe: false, reason: 'URL is empty or not a string' };
25
+ }
26
+ try {
27
+ const parsed = new URL(rawUrl.trim());
28
+ if (parsed.protocol !== 'https:') {
29
+ return { safe: false, reason: `Egress protocol must be https: (got ${parsed.protocol})` };
30
+ }
31
+ if (parsed.username || parsed.password) {
32
+ return { safe: false, reason: 'Egress URL must not contain userinfo credentials' };
33
+ }
34
+ if (parsed.port && parsed.port !== '443') {
35
+ return { safe: false, reason: `Egress URL must not use non-standard port ${parsed.port}` };
36
+ }
37
+ const hostname = parsed.hostname.toLowerCase().replace(/\.+$/, '');
38
+ if (!hostname || !hostname.includes('.') || SSRF_BLOCKED_HOST_RE.test(hostname)) {
39
+ return {
40
+ safe: false,
41
+ hostname,
42
+ reason: `Host ${hostname} is blocked by SSRF private/metadata/IP-literal egress policy`,
43
+ };
44
+ }
45
+ return { safe: true, hostname };
46
+ } catch (err) {
47
+ return { safe: false, reason: `Malformed URL: ${err.message}` };
48
+ }
49
+ }
50
+
51
+ /**
52
+ * Validates that a target URL passes both SSRF checks and strict Google Ads Transparency host pinning.
53
+ * @param {string} rawUrl
54
+ * @returns {{safe: boolean, hostname?: string, reason?: string}}
55
+ */
56
+ export function assertAllowedGoogleAdsEgress(rawUrl) {
57
+ const ssrfCheck = verifyEgressUrlSsrfSafe(rawUrl);
58
+ if (!ssrfCheck.safe) {
59
+ return ssrfCheck;
60
+ }
61
+ if (!ALLOWED_GOOGLE_ADS_RPC_HOSTS.has(ssrfCheck.hostname)) {
62
+ return {
63
+ safe: false,
64
+ hostname: ssrfCheck.hostname,
65
+ reason: `Host '${ssrfCheck.hostname}' is not in ALLOWED_GOOGLE_ADS_RPC_HOSTS`,
66
+ };
67
+ }
68
+ return { safe: true, hostname: ssrfCheck.hostname };
69
+ }