amphtml icon indicating copy to clipboard operation
amphtml copied to clipboard

Allow SVG image tag with inline data

Open emarchiori opened this issue 10 years ago • 13 comments

Currently there is no way to load image data into an SVG element. SVG tags have fixed width and height, so are naturally closer to <amp-img> than <img>.

One path is to allow <image> tags with xlink:href='data:..', which should cover all common use case a limit potential malicious uses

Potential problems

  • File size will increase due to embedding of images. Should we limit the size?
  • Could be exploited to get around delayed loading of <amp-img>
  • Optimization of images of this type in the cache might be limited or more complex

emarchiori avatar Aug 17 '16 10:08 emarchiori

Generally this option makes sense to me since we allow <img> element in SVG already.

/cc @Gregable

dvoytenko avatar Aug 17 '16 18:08 dvoytenko

Generally this option makes sense to me since we allow <img> element in SVG already.

Not currently, unless you mean an <img> inside an SVG subresource (ie: <amp-img src="foo.svg"></amp-img> and foo.svg includes an <img> element). The only context in an AMP page where an <img> element is allowed is inside a <noscript> element. We don't allow <image> in any context.

My concern would be the 2nd bullet point, which is that this subverts the AMP runtime's ability to control when an image is loaded. However, we already allow <amp-img> to have data: urls.

It seems like a parallel test would be:

  • Should we allow <img> anywhere in the document as long as the src attr is a data: url?

Gregable avatar Aug 17 '16 21:08 Gregable

@Gregable No, not the <img> tag in HTML, but SVG's <image> element - https://developer.mozilla.org/en-US/docs/Web/SVG/Element/image

Do we currently allow these?

dvoytenko avatar Aug 18 '16 23:08 dvoytenko

@dvoytenko , no we do not currently allow <image> tags anywhere.

Gregable avatar Aug 30 '16 01:08 Gregable

I believe we should enable SVG <image> tag provided that its xlink:href attribute contains a data URI.

dvoytenko avatar Aug 31 '16 21:08 dvoytenko

/to @Gregable

dvoytenko avatar Aug 31 '16 21:08 dvoytenko

sgtm

Gregable avatar Sep 06 '16 22:09 Gregable

@powdercloud thought of one more potential issue with this that I wanted to run by you, specifically that I could data encode an .svg image into the <image> URL, for example:

<svg ...>
  <image xlink:href="data:image/svg+xml;base64,BLAHBLAH"/>
</svg>

That base64 encoded SVG could in turn have any SVG syntax at all, including something like:

<svg ...>
  <image xlink:href="http://example.com/foo.png"/>
</svg>

This would effectively be a workaround to inject a remote image into the document not managed by amp runtime and not rewritten by the amp cache.

A similar thing can be done with <amp-img src="data:image/svg+xml;base64,BLAHBLAH", but that's at least managed by the amp runtime, though not rewritten by the cache.

Do we need to also constrain the file types in data: URLs? There is no mechanism for this currently, but it could be added.

Gregable avatar Sep 08 '16 20:09 Gregable

@dvoytenko

Gregable avatar Sep 08 '16 20:09 Gregable

Sure, this is very specific to raster images, so let's make sure the image type is NOT svg+xml.

dvoytenko avatar Sep 09 '16 20:09 dvoytenko

Two follow ups:

Are there other types you know of that we should constrain besides svg+xml?

There are a few other places we allow data: urls. Do you feel we should add this constraint to

  1. <amp-img src=, <amp-anim src=
  2. <img src=
  3. <video src=
  4. <audio src=
  5. Images in the custom CSS block

Gregable avatar Sep 09 '16 20:09 Gregable

No. It's enough for us to control the loading on top level for these elements. We only care about SVG since (a) we can control when it renders or not; (b) SVG is more of a composite element.

dvoytenko avatar Sep 09 '16 20:09 dvoytenko

This is fixed and live everywhere.

Gregable avatar Oct 11 '16 21:10 Gregable