Syntax
The syntax of data URIs is defined in Request for Comments (RFC) 2397, published in August 1998, and follows the URI scheme syntax. A data URI consists of: data: '<media type>'';base64],''<data>'' * The scheme,data
. It is followed by a colon (:
).
* An optional media type. The media type part may include one or more parameters, in the format attribute=value
, separated by semicolons (;
) . A common media type parameter is charset
, specifying the character set of the media type, where the value is from the IANA list of character set names. If one is not specified, the text/plain;charset=US-ASCII
.
* An optional base64 extension base64
, separated from the preceding part by a semicolon. When present, this indicates that the data content of the URI is binary data, encoded in ASCII format using the Base64 scheme for binary-to-text encoding. The base64 extension is distinguished from any media type parameters by virtue of not having a =value
component and by coming after any media type parameters. Since Base64 encoded data is approximately 33% larger than original data, it is recommended to use Base64 data URIs only if the server supports HTTP compression or embedded files are smaller than 1KB.
* The data, separated from the preceding part by a comma (,
). The data is a sequence of zero or more data:
URIs use the standard Base64 character set (with '+
' and '/
' as characters 62 and 63) rather than the so-called " URL-safe Base64" character set.
Examples of data URIs showing most of the features are:
:data:text/vnd-example+xyz;foo=bar;base64,R0lGODdh
:data:text/plain;charset=UTF-8;page=21,the%20data:1234,5678
(outputs: "the data:1234,5678")
The minimal data URI is data:,
, consisting of the
scheme, no media-type, and zero-length data.
Thus, within the overall URI syntax, a data URI consists of a scheme and a path, with no authority part, query string, or fragment. The optional media type, the optional base64 indicator, and the data are all parts of the
URI path.
Examples of use
HTML
An HTML fragment embedding a picture of a small red dot:CSS
A Cascading Style Sheets (CSS) rule that includes a background image:\ +
line terminators
are a feature of CSS, indicating continuation on the next line. These would be removed by the CSS stylesheet processor, and the data URI would be reconstituted without whitespace, making it correct, since whitespace is not allowed within the data component of a data:
URI.
JavaScript
A JavaScript statement that opens an embedded subwindow, as for a footnote link:SVG
A Scalable Vector Graphic image containing an embedded JPEG image encoded in Base64:Malware and phishing
The data URI can be utilized to construct attack pages that attempt to obtain usernames and passwords from unsuspecting web users. It can also be used to get around cross-site scripting (XSS) restrictions, embedding the attack payload fully inside the address bar, and hosted via URL shortening services rather than needing a full website that is controlled by a third party. As a result, some browsers now block webpages from navigating to data URIs.References
{{DEFAULTSORT:Data Uri Scheme URI schemes Internet Standards