MCP server CSS z-index stacking consent security
Four SA-CSS-ZID consent attack patterns that use CSS z-index and stacking context manipulation to render consent text behind page background elements. The text has correct color, font, and layout dimensions, but is painted below a background layer and therefore invisible. Unlike color attacks (where getComputedStyle reveals suspicious values), z-index attacks on background-covered elements are harder to detect: the element's own computed styles appear normal; only the rendering order relative to overlapping elements reveals the attack.
CSS z-index controls the painting order of positioned elements within a stacking context. Elements with higher z-index are painted on top of elements with lower z-index within the same stacking context. Negative z-index values place elements below their parent stacking context, potentially behind the background of ancestor elements. Malicious consent UIs can exploit this by ensuring consent text has a negative z-index that places it behind a background-colored ancestor, while maintaining correct layout dimensions (the consent text still occupies space), making the text appear completely absent visually while present in the DOM.
SA-CSS-ZID-001: z-index:-1 on consent text — behind parent background
z-index: -1 combined with position: relative (or any non-static positioning) on a consent text element causes the element to paint below its containing block's background. If the parent element has a background color (e.g., background: white for a white dialog box), the consent text is painted behind this background and is invisible. The text occupies layout space — getBoundingClientRect() returns non-zero dimensions — but zero text pixels reach the user's screen. getComputedStyle(el).color still returns the text color; the element appears normal in all style checks except the computed z-index value.
.consent-dialog {
position: relative;
background: white; /* consent dialog has white background */
z-index: 0; /* creates stacking context */
}
.consent-text {
position: relative;
z-index: -1; /* paint BELOW .consent-dialog's background */
color: black; /* color is correct — getComputedStyle.color = "rgb(0,0,0)" */
}
/* Stacking order within .consent-dialog:
Layer 1 (bottom): .consent-text (z-index:-1) ← text pixels here
Layer 2 (top): .consent-dialog background (white) ← covers text pixels
Layer 3: .consent-dialog border
Layer 4: .allow-btn, .deny-btn (normal z-index:0)
Visual result:
- Allow and Deny buttons: visible (above background in paint order)
- Consent text: invisible (behind white background, white on white)
- getBoundingClientRect().height on consent text: non-zero (space reserved)
- getComputedStyle(el).color: 'rgb(0, 0, 0)' (correct — attack is paint order)
Prerequisite: parent must have a non-transparent background AND create stacking context.
If parent has background:transparent AND no stacking context:
z-index:-1 text would be visible (no opaque cover)
Detection:
Check consent text: position !== 'static' AND z-index < 0 → HIGH
Also check parent: if parent has background-color AND creates stacking context → CRITICAL
getComputedStyle(el).zIndex parsed as integer < 0 → flag for compound check */
Layout space present, text pixels absent: This attack is deceptive because the consent text element occupies its expected layout space (the dialog box height accounts for the text block), creating a visual impression of a normal dialog with slightly sparse spacing. The empty area where the text should be appears to be padding. Only careful inspection of the stacking context reveals that the text is painted behind the background.
z-index: -1 on consent text: text painted behind parent's white background; layout space preserved; all style checks appear normal. Detection: position non-static + zIndex < 0 + parent has opaque background + creates stacking context → CRITICAL.SA-CSS-ZID-002: z-index:1 cover overlay over z-index:0 consent — sibling cover attack
A consent dialog that places the consent text at z-index: 0 (the default for positioned elements in a stacking context) can be covered by a sibling position: absolute element at z-index: 1 with a background matching the dialog background. The consent text element has a positive z-index and no negative stacking; the attack uses a separate element layered above it. The consent text's own computed styles are entirely normal — positive z-index, correct color, correct dimensions. The cover element is a separate DOM node, requiring auditors to examine the full set of overlapping elements at the consent text's screen coordinates.
<div class="consent-dialog" style="position:relative; background:white">
<p class="consent-text" style="position:relative; z-index:0; color:black">
By clicking Allow you authorize permanent file access.
</p>
<div class="cover-overlay" style="
position: absolute;
inset: 0; /* covers the full dialog area */
background: white; /* same as dialog background */
z-index: 1; /* above consent-text (z-index:0) */
pointer-events: none; /* clicks pass through to buttons below */
"></div>
<button class="allow-btn" style="position:relative; z-index:2">Allow</button>
<button class="deny-btn" style="position:relative; z-index:2">Deny</button>
</div>
/* Stacking order:
z-index:0 → consent text (painted first, at bottom)
z-index:1 → cover-overlay (white, covers consent text pixels)
z-index:2 → buttons (above overlay — visible)
Visual result:
- Consent text: zero visible pixels (covered by z-index:1 white overlay)
- Buttons: visible at z-index:2 (above the cover overlay)
- pointer-events:none on overlay: click events reach buttons correctly
Consent text computed styles:
- z-index: 0 (positive — not suspicious by itself)
- color: black (correct)
- display: block (correct)
→ consent text alone looks normal; cover overlay is a separate element
Detection:
Requires spatial overlap analysis:
const textRect = consentTextEl.getBoundingClientRect();
const centerX = textRect.x + textRect.width / 2;
const centerY = textRect.y + textRect.height / 2;
const topElement = document.elementFromPoint(centerX, centerY);
if (topElement !== consentTextEl && topElement !== null) {
// something is painted on top of consent text
const topBg = getComputedStyle(topElement).backgroundColor;
if (topBg !== 'transparent' && topBg !== 'rgba(0, 0, 0, 0)') → CRITICAL SA-CSS-ZID-002
} */
z-index: 1 cover overlay over z-index: 0 consent text: sibling element covers consent; consent's own styles appear normal. Detection: elementFromPoint() at consent text center ≠ consent element + opaque background on top element → CRITICAL.SA-CSS-ZID-003: isolation:isolate — stacking context containment for cover attacks
isolation: isolate creates a new stacking context for the element without affecting its z-index in the parent context. It is the CSS property designed for exactly this purpose: “isolate this element's z-index space from the surrounding page.” In a consent attack, isolation: isolate applied to the consent container means that z-index values of elements inside the container are resolved relative to the container's stacking context, not the page-level stacking context. This allows an attacker to use z-index: 9999 on a cover overlay inside the consent container without that overlay interfering with page-level modal dialogs or navigation elements — the high z-index is “contained” within the isolated context.
.consent-dialog {
isolation: isolate; /* create new stacking context without triggering z-index stacking */
/* z-index values inside are now resolved within this context, not page-level */
}
.consent-text {
position: relative;
z-index: 0; /* low within isolated context */
}
.cover-overlay {
position: absolute;
inset: 0;
background: white;
z-index: 9999; /* high within isolated context — covers consent-text */
/* BUT: z-index:9999 here is LOCAL to .consent-dialog's stacking context */
/* Does NOT interfere with page-level z-index:9999 elements (nav, modals) */
}
/* Why isolation:isolate is used for attacks:
Without isolation: a z-index:9999 overlay inside consent-dialog might conflict
with page navigation (z-index:1000) or other elements (z-index:9000)
— making the attack obvious or breaking the page layout
With isolation:isolate: the cover overlay's z-index:9999 is scoped to consent-dialog
— no conflicts with page elements
— cover overlay works silently within the consent container
Stacking context creation methods (and their side effects):
isolation:isolate → new stacking context; NO z-index change; NO opacity; NO transform
→ cleanest attack method; minimal CSS property side effects to detect
Other stacking context triggers (all with detectable side effects):
z-index + position → changes z-index (detectable)
opacity < 1 → semi-transparent (visually detectable)
transform → changes geometry (layout change detectable)
will-change: transform → GPU compositing (audit flag via separate check)
Detection:
getComputedStyle(el).isolation === 'isolate' → requires inspection of children
If isolation:isolate on consent container AND opaque positioned child covers text → HIGH */
isolation:isolate vs other stacking context triggers: Most CSS properties that create stacking contexts (opacity, transform, will-change) have observable side effects: opacity makes the element semi-transparent; transform changes its geometry. isolation: isolate creates a stacking context with no other visual side effect, making it the cleanest tool for constructing a contained cover overlay. Its presence on a consent container is inherently suspicious unless the consent dialog genuinely needs to isolate its blend modes from the page background.
isolation: isolate on consent container containing an opaque cover child: cleanest stacking context for cover attack; no z-index or opacity side effects. Detection: isolation === 'isolate' + opaque positioned descendant over consent text → HIGH.SA-CSS-ZID-004: position:relative + z-index:-999 — root stacking context pushdown
position: relative; z-index: -999 on a consent text element pushes it below the root stacking context's background when the consent text is a direct or near-direct descendant of the root. In this configuration, the consent text is painted below the <body> element's background color. If the page body has a white background (the default), the consent text at z-index: -999 is behind the white body background and invisible. A large negative value like -999 is used to ensure the element falls below any other negative-z-index layers. The consent buttons remain at normal z-index and are visible above the body background.
/* HTML structure: */
<body style="background: white">
<div class="consent-dialog">
<p class="consent-text" style="position:relative; z-index:-999">
By clicking Allow you authorize permanent file access.
</p>
<button class="allow-btn">Allow</button>
<button class="deny-btn">Deny</button>
</div>
</body>
/* Stacking order (root stacking context):
z-index:-999 → consent-text (painted below root background)
root background (body background: white) → covers everything below z-index:0
z-index:auto → allow-btn, deny-btn (painted above root background)
Why -999 instead of -1:
- Ensures consent text is below ALL other negative-z-index elements
- More visually reliable: less likely to be accidentally "above" something
- Auditor heuristic: any element with z-index < -10 in consent UI is suspicious
Conditions for attack success:
1. consent-text must have position !== static (z-index only works on positioned elements)
2. consent-text must NOT be inside an ancestor that creates a new stacking context
(if inside an isolated ancestor, z-index:-999 is relative to that context, not root)
3. body (or html element) must have a non-transparent background color
Page-level detection:
- Inspect all consent text elements for position + negative z-index
- Check that no ancestor creates a stacking context that would contain the z-index
- Verify parent/ancestor background is opaque at element's screen coordinates
- getComputedStyle(el).zIndex < -10 on consent text → HIGH
- getComputedStyle(el).zIndex < -10 + opaque ancestor background → CRITICAL */
Containment escape: If the consent text at z-index: -999 is inside an ancestor that creates its own stacking context (e.g., an ancestor with position: relative; z-index: 1), the z-index: -999 is resolved within that ancestor's stacking context, not the root. In this case, the text would be below the ancestor's background but above the root background — visible if the ancestor has a transparent background. Attackers typically ensure the consent container does NOT create a stacking context to allow the z-index: -999 to propagate to the root context.
position: relative; z-index: -999: consent text pushed below root stacking context background; invisible on any non-transparent body background. Detection: parseInt(zIndex) < -10 on consent text + opaque ancestor → CRITICAL.Summary of SA-CSS-ZID findings
| ID | Severity | Properties | Cover mechanism | Detection |
|---|---|---|---|---|
| SA-CSS-ZID-001 | CRITICAL | position:relative; z-index:-1 | Text below parent background layer | zIndex < 0 + parent stacking context + opaque bg |
| SA-CSS-ZID-002 | CRITICAL | z-index:0 consent + z-index:1 white overlay sibling | Sibling overlay above text in same stacking context | elementFromPoint() at text center ≠ text element |
| SA-CSS-ZID-003 | HIGH | isolation:isolate + cover child | Isolated stacking context hides cover overlay from page | isolation === 'isolate' + opaque positioned descendant |
| SA-CSS-ZID-004 | CRITICAL | position:relative; z-index:-999 | Text below root stacking context background | parseInt(zIndex) < -10 on consent text + opaque ancestor |