eval() usage detected
Tags use eval(), which executes arbitrary strings as code. This is a security risk (XSS vector) and prevents JavaScript engine optimizations.
Summary
- rule_id —
eval-usage - purpose — Detect Custom HTML tags that call
eval()to execute strings as code - effect — XSS vector; disables JavaScript engine optimizations for the affected function
- layer — container
- reversibility — non-destructive
What fires this
The rule iterates active Custom HTML tags via getHtmlTags and filters for those where codeAnalysis.hasEval is true. The parser sets this flag when it detects a eval( call in the tag code. Every tag with that flag fires the rule. Paused tags are excluded. The rule does not inspect what string is passed to eval() — any call triggers the issue regardless of the source.
Why it matters
eval() executes an arbitrary string as JavaScript in the current scope. If any part of that string is influenced by user input — URL parameters, cookie values, dataLayer contents, or remote API responses — the tag becomes an XSS vector allowing arbitrary script injection. Beyond security, V8 exits optimized compilation mode for any function containing eval(), degrading performance across the affected script. There is no GTM use case that requires eval(): JSON strings belong in JSON.parse(), and dynamic logic belongs in explicit conditionals.
Before / after
// Parsing a JSON string — triggers eval-usage
var config = eval('(' + window.myConfig + ')')
dataLayer.push({ config: config })
// Safe parse — no code execution
var config = JSON.parse(window.myConfig)
dataLayer.push({ config: config })
Remediation
- Open each flagged tag and identify what string is passed to
eval(). - If the string is a JSON payload: replace
eval(str)withJSON.parse(str). - If the string is conditional logic: restructure using
if/elseorswitchstatements. - If the string originates from a URL parameter, cookie, or remote API: treat the tag as a potential XSS vulnerability and involve your security team before proceeding.
- If the
eval()is from a vendor-supplied snippet: contact the vendor for a non-eval version, or use a native GTM tag template if one is available. - Test the refactored tag —
eval()is sometimes used to work around scope issues that will require explicit fixes.
Metadata
- Severity
- high
- Tier
- error
- Category
- security
- Layer
- container
- Version
- 1
- Effort
2hr(about 2 hours)- Reversibility
- non-destructive
See also
- Security rules — other rules in this category
- Health score — how this rule contributes to the score
- Rules catalog — browse all rules
Examples
curl https://developer.tagmanifest.com/v1/evaluate-rule \
-H "Authorization: Bearer $TM_API_KEY" \
-H "Content-Type: application/json" \
-d '{"rule_id":"eval-usage","url":"https://example.com"}'
User: Run eval-usage on https://example.com
# Tool call
{
"tool": "evaluate_rule",
"arguments": {
"rule_id": "eval-usage",
"url": "https://example.com"
}
}