What a canvas may not see
Some content is protected by the browser: permission buttons, the colour of visited links, spelling markers, autofill suggestions,
frames and images from other origins. I put each one in a <canvas content="drawable"> and the page measures three things:
whether it is in the picture, whether script can read its pixels, and whether it still works.
The table shows results recorded in the test browser. Press the button to measure this browser. Four cases need a person, and they're under "Try it by hand".
| Content | In the picture | Script can read it | Still works | Detail |
|---|
Show the code the fixture and the measurements
- The fixture: a 300 by 200 canvas with one drawable child.
ink()counts the pixels inside one element's box that aren't transparent. - Permission elements. Draw the element at three places, read
isValidandinvalidReason, then compare with the same element outside a canvas. - Content from another origin. Count its pixels, then call
getImageData()andtoDataURL()to see whether the canvas is tainted. - The same frame as a WebGL texture.
texElementSubImage2D(), thenreadPixels()through a framebuffer. - The visited link. A link to this page, with different
:linkand:visitedcolours. - All the cases.
Try it by hand
The white panel is one canvas. Everything in it is a real element drawn with drawElementImage() at its own position, with no transform.
The dashed box under it holds the same permission elements and the same link outside any canvas.
The readout updates each time the canvas repaints.
- Tap each permission button in the canvas, then the same button in the dashed box. A prompt from the browser means the button works.
- Tap the link in the dashed box once. It opens this page again, which puts the page in your history. Then compare the colour of the two links.
- Type a misspelt word in the first field and leave the field.
- Tap the name field and move through the autofill suggestions without choosing one, then choose one.
This page (outside the canvas)
Show the code the canvas you can touch
- Markup: the elements are children of the canvas.
- Draw the panel at its own position on each
paintevent. The canvas keeps one backing pixel per device pixel. - Read the picture back after each paint and count pixels in the colours the page chose for
:visited,::spelling-errorand the autofill background.
How it works
A drawable canvas lays out its children and paints them into a picture that drawElementImage() copies into the canvas.
The page can then read the canvas with getImageData(), or pass the element through a shader.
That would expose anything the browser paints but doesn't tell script about, so the browser has to do one of three things with protected content:
leave it out of the picture, paint a neutral version of it, or paint it and stop it working.
Each row above shows which.
Findings
- The canvas is never tainted. In Chromium 157 no case makes
getImageData()ortoDataURL()throw. Content the page may not read is left out of the picture: a cross-origin<iframe>, a cross-origin<img>or<video>loaded without CORS, and a cross-origin CSS background all draw 0 pixels. An image loaded withcrossoriginfrom a server that allows it is drawn. - A shader gets the same picture.
texElementSubImage2D()of an element holding a cross-origin frame uploads a texture with the frame missing. - Permission elements are drawn and readable, and don't work.
<geolocation>reportsisValidfalse with the reasonintersection_occluded_or_distortedas soon as it is a canvas child, even when it's drawn exactly over its own box. A real click reaches it and opens no prompt. The same element outside a canvas is valid, and a click opens the prompt. To keep a permission button usable, position it over the canvas and don't make it a child. - Links are drawn as unvisited. The picture uses the
:linkcolour while the screen uses the:visitedcolour (recorded in Chromium 153 with a profile that keeps history). - System colours are readable, and they were before.
AccentColorand the others read back as the valuesgetComputedStyle()reports. - Popovers and modal dialogs aren't in the picture. The browser draws the top layer itself, above the canvas.
- Open: spelling markers and the autofill preview can't be produced by a script or by the test browser. They need the steps under "Try it by hand" on a real device. So does the system accent colour on a device where the person has changed it.
- Chromium 157 crash: calling
WEBGL_lose_context.loseContext()on a drawable canvas and then removing the canvas crashes the tab. It happens at once when both are in one task, and after a wait too when the canvas has an<iframe>child. The WebGL row here keeps one context and doesn't release it.