Penetration TestJan Kahmen4 min read
Google Tag Manager: Risks Posed by Custom JS
If misused, custom JS tags in GTM can access data, manipulate users, or reload third-party domains.

Google Tag Manager (GTM) is a powerful tool: marketing and analytics teams can integrate tags and scripts without developer deployments. However, this flexibility also carries risks. If misused, custom HTML or custom JS tags can access data, manipulate users, or reload third-party domains. In this article, I explain the risks involved, how to use GTM securely, how to detect misuse, and how to respond responsibly and legally in case of suspicion.
Why GTM Is an Attractive Attack Vector
- Central execution location: GTM containers run on all pages of a domain—a compromised tag has an immediate effect everywhere.
- Dynamic execution: Custom JS can read/modify DOM content, read forms, or initiate network calls.
- Often far-reaching permissions: Many sites trust scripts loaded via GTM (e.g., analytics, A/B testing).
- Operational practice: Marketing teams sometimes have publish rights without strict review processes.
Typical Abuse Scenarios
- Data exfiltration: Unauthorized collection and transmission of input fields (e.g., email, payment information).
- Content manipulation: Ads or links are replaced, phishing content is displayed.
- Supply chain attacks: GTM reloads malicious scripts from compromised third-party providers.
- Persistent tracking/fingerprinting: Extended tracking that goes beyond permitted tracking purposes.
Principles for Secure GTM Operation
- Least privilege: Only the necessary people are given publish or edit rights. Separate roles (reading/testing/publishing).
- Review & approval: Every change—especially custom HTML/JS—goes through a review process with developer or security sign-off.
- Avoid custom HTML if possible: Use predefined tag templates or server-side tagging.
- Version control & preview: Use workspaces, preview, and versioning; test changes in a staging environment.
- Monitoring & Logging: Monitor container changes, set alerts, and check network traffic for unusual targets.
- Enforce security policies: Content Security Policy (CSP), Subresource Integrity (if possible), SameSite cookies, etc.
Specific Technical Measures
- Limit who can do what
Use GTM areas/workspaces and assign roles restrictively.
Implement a change approval workflow (e.g., in your ticketing system) — no direct publishing without a ticket and review. - Tag design: Templates instead of free JS
Create and use custom tag templates (GTM templates) with strictly defined inputs instead of raw custom HTML.
Templates can restrict which network requests are allowed, thereby reducing risks. - Server-side tagging
Where possible, move sensitive logic to server-side tag management (GTM Server Container). This prevents critical scripts from running in the user's browser. - Content integrity & trusted hosts
Where possible, use signatures/hashes (SRI) for static files. SRI is often not practical for dynamically loaded tags, so whitelisting domains is a good idea.
Restrict external domains to which GTM can send data via CSP and network policies. - Configure Content Security Policy (CSP)
A CSP can significantly reduce risk by only executing scripts from allowed sources. Example of a more restrictive policy (as a starting point; adapt to your infrastructure):
Content-Security-Policy:
default-src ‘self’;
script-src ‘self’ https://www.googletagmanager.com https://www.google-analytics.com;
connect-src ‘self’ https://www.google-analytics.com https://your-trusted-endpoints.example.com;
frame-src ‘self’ https://www.youtube.com;
img-src ‘self’ data: https:;
style-src ‘self’ 'unsafe-inline';
object-src ‘none’;
base-uri ‘self’;
form-action ‘self’;
Notes:
- script-src lists the permitted script sources; any additional domain that GTM loads must be deliberately enabled.
- CSP can be extended with report-uri/report-to to detect violations.
- CSP is powerful but complex — test in report-only mode first.