safestyle.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. // Copyright 2014 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview The SafeStyle type and its builders.
  16. *
  17. * TODO(xtof): Link to document stating type contract.
  18. */
  19. goog.provide('goog.html.SafeStyle');
  20. goog.require('goog.array');
  21. goog.require('goog.asserts');
  22. goog.require('goog.html.SafeUrl');
  23. goog.require('goog.string');
  24. goog.require('goog.string.Const');
  25. goog.require('goog.string.TypedString');
  26. /**
  27. * A string-like object which represents a sequence of CSS declarations
  28. * ({@code propertyName1: propertyvalue1; propertyName2: propertyValue2; ...})
  29. * and that carries the security type contract that its value, as a string,
  30. * will not cause untrusted script execution (XSS) when evaluated as CSS in a
  31. * browser.
  32. *
  33. * Instances of this type must be created via the factory methods
  34. * ({@code goog.html.SafeStyle.create} or
  35. * {@code goog.html.SafeStyle.fromConstant}) and not by invoking its
  36. * constructor. The constructor intentionally takes no parameters and the type
  37. * is immutable; hence only a default instance corresponding to the empty string
  38. * can be obtained via constructor invocation.
  39. *
  40. * SafeStyle's string representation can safely be:
  41. * <ul>
  42. * <li>Interpolated as the content of a *quoted* HTML style attribute.
  43. * However, the SafeStyle string *must be HTML-attribute-escaped* before
  44. * interpolation.
  45. * <li>Interpolated as the content of a {}-wrapped block within a stylesheet.
  46. * '<' characters in the SafeStyle string *must be CSS-escaped* before
  47. * interpolation. The SafeStyle string is also guaranteed not to be able
  48. * to introduce new properties or elide existing ones.
  49. * <li>Interpolated as the content of a {}-wrapped block within an HTML
  50. * <style> element. '<' characters in the SafeStyle string
  51. * *must be CSS-escaped* before interpolation.
  52. * <li>Assigned to the style property of a DOM node. The SafeStyle string
  53. * should not be escaped before being assigned to the property.
  54. * </ul>
  55. *
  56. * A SafeStyle may never contain literal angle brackets. Otherwise, it could
  57. * be unsafe to place a SafeStyle into a &lt;style&gt; tag (where it can't
  58. * be HTML escaped). For example, if the SafeStyle containing
  59. * "{@code font: 'foo &lt;style/&gt;&lt;script&gt;evil&lt;/script&gt;'}" were
  60. * interpolated within a &lt;style&gt; tag, this would then break out of the
  61. * style context into HTML.
  62. *
  63. * A SafeStyle may contain literal single or double quotes, and as such the
  64. * entire style string must be escaped when used in a style attribute (if
  65. * this were not the case, the string could contain a matching quote that
  66. * would escape from the style attribute).
  67. *
  68. * Values of this type must be composable, i.e. for any two values
  69. * {@code style1} and {@code style2} of this type,
  70. * {@code goog.html.SafeStyle.unwrap(style1) +
  71. * goog.html.SafeStyle.unwrap(style2)} must itself be a value that satisfies
  72. * the SafeStyle type constraint. This requirement implies that for any value
  73. * {@code style} of this type, {@code goog.html.SafeStyle.unwrap(style)} must
  74. * not end in a "property value" or "property name" context. For example,
  75. * a value of {@code background:url("} or {@code font-} would not satisfy the
  76. * SafeStyle contract. This is because concatenating such strings with a
  77. * second value that itself does not contain unsafe CSS can result in an
  78. * overall string that does. For example, if {@code javascript:evil())"} is
  79. * appended to {@code background:url("}, the resulting string may result in
  80. * the execution of a malicious script.
  81. *
  82. * TODO(mlourenco): Consider whether we should implement UTF-8 interchange
  83. * validity checks and blacklisting of newlines (including Unicode ones) and
  84. * other whitespace characters (\t, \f). Document here if so and also update
  85. * SafeStyle.fromConstant().
  86. *
  87. * The following example values comply with this type's contract:
  88. * <ul>
  89. * <li><pre>width: 1em;</pre>
  90. * <li><pre>height:1em;</pre>
  91. * <li><pre>width: 1em;height: 1em;</pre>
  92. * <li><pre>background:url('http://url');</pre>
  93. * </ul>
  94. * In addition, the empty string is safe for use in a CSS attribute.
  95. *
  96. * The following example values do NOT comply with this type's contract:
  97. * <ul>
  98. * <li><pre>background: red</pre> (missing a trailing semi-colon)
  99. * <li><pre>background:</pre> (missing a value and a trailing semi-colon)
  100. * <li><pre>1em</pre> (missing an attribute name, which provides context for
  101. * the value)
  102. * </ul>
  103. *
  104. * @see goog.html.SafeStyle#create
  105. * @see goog.html.SafeStyle#fromConstant
  106. * @see http://www.w3.org/TR/css3-syntax/
  107. * @constructor
  108. * @final
  109. * @struct
  110. * @implements {goog.string.TypedString}
  111. */
  112. goog.html.SafeStyle = function() {
  113. /**
  114. * The contained value of this SafeStyle. The field has a purposely
  115. * ugly name to make (non-compiled) code that attempts to directly access this
  116. * field stand out.
  117. * @private {string}
  118. */
  119. this.privateDoNotAccessOrElseSafeStyleWrappedValue_ = '';
  120. /**
  121. * A type marker used to implement additional run-time type checking.
  122. * @see goog.html.SafeStyle#unwrap
  123. * @const {!Object}
  124. * @private
  125. */
  126. this.SAFE_STYLE_TYPE_MARKER_GOOG_HTML_SECURITY_PRIVATE_ =
  127. goog.html.SafeStyle.TYPE_MARKER_GOOG_HTML_SECURITY_PRIVATE_;
  128. };
  129. /**
  130. * @override
  131. * @const
  132. */
  133. goog.html.SafeStyle.prototype.implementsGoogStringTypedString = true;
  134. /**
  135. * Type marker for the SafeStyle type, used to implement additional
  136. * run-time type checking.
  137. * @const {!Object}
  138. * @private
  139. */
  140. goog.html.SafeStyle.TYPE_MARKER_GOOG_HTML_SECURITY_PRIVATE_ = {};
  141. /**
  142. * Creates a SafeStyle object from a compile-time constant string.
  143. *
  144. * {@code style} should be in the format
  145. * {@code name: value; [name: value; ...]} and must not have any < or >
  146. * characters in it. This is so that SafeStyle's contract is preserved,
  147. * allowing the SafeStyle to correctly be interpreted as a sequence of CSS
  148. * declarations and without affecting the syntactic structure of any
  149. * surrounding CSS and HTML.
  150. *
  151. * This method performs basic sanity checks on the format of {@code style}
  152. * but does not constrain the format of {@code name} and {@code value}, except
  153. * for disallowing tag characters.
  154. *
  155. * @param {!goog.string.Const} style A compile-time-constant string from which
  156. * to create a SafeStyle.
  157. * @return {!goog.html.SafeStyle} A SafeStyle object initialized to
  158. * {@code style}.
  159. */
  160. goog.html.SafeStyle.fromConstant = function(style) {
  161. var styleString = goog.string.Const.unwrap(style);
  162. if (styleString.length === 0) {
  163. return goog.html.SafeStyle.EMPTY;
  164. }
  165. goog.html.SafeStyle.checkStyle_(styleString);
  166. goog.asserts.assert(
  167. goog.string.endsWith(styleString, ';'),
  168. 'Last character of style string is not \';\': ' + styleString);
  169. goog.asserts.assert(
  170. goog.string.contains(styleString, ':'),
  171. 'Style string must contain at least one \':\', to ' +
  172. 'specify a "name: value" pair: ' + styleString);
  173. return goog.html.SafeStyle.createSafeStyleSecurityPrivateDoNotAccessOrElse(
  174. styleString);
  175. };
  176. /**
  177. * Checks if the style definition is valid.
  178. * @param {string} style
  179. * @private
  180. */
  181. goog.html.SafeStyle.checkStyle_ = function(style) {
  182. goog.asserts.assert(
  183. !/[<>]/.test(style), 'Forbidden characters in style string: ' + style);
  184. };
  185. /**
  186. * Returns this SafeStyle's value as a string.
  187. *
  188. * IMPORTANT: In code where it is security relevant that an object's type is
  189. * indeed {@code SafeStyle}, use {@code goog.html.SafeStyle.unwrap} instead of
  190. * this method. If in doubt, assume that it's security relevant. In particular,
  191. * note that goog.html functions which return a goog.html type do not guarantee
  192. * the returned instance is of the right type. For example:
  193. *
  194. * <pre>
  195. * var fakeSafeHtml = new String('fake');
  196. * fakeSafeHtml.__proto__ = goog.html.SafeHtml.prototype;
  197. * var newSafeHtml = goog.html.SafeHtml.htmlEscape(fakeSafeHtml);
  198. * // newSafeHtml is just an alias for fakeSafeHtml, it's passed through by
  199. * // goog.html.SafeHtml.htmlEscape() as fakeSafeHtml
  200. * // instanceof goog.html.SafeHtml.
  201. * </pre>
  202. *
  203. * @see goog.html.SafeStyle#unwrap
  204. * @override
  205. */
  206. goog.html.SafeStyle.prototype.getTypedStringValue = function() {
  207. return this.privateDoNotAccessOrElseSafeStyleWrappedValue_;
  208. };
  209. if (goog.DEBUG) {
  210. /**
  211. * Returns a debug string-representation of this value.
  212. *
  213. * To obtain the actual string value wrapped in a SafeStyle, use
  214. * {@code goog.html.SafeStyle.unwrap}.
  215. *
  216. * @see goog.html.SafeStyle#unwrap
  217. * @override
  218. */
  219. goog.html.SafeStyle.prototype.toString = function() {
  220. return 'SafeStyle{' + this.privateDoNotAccessOrElseSafeStyleWrappedValue_ +
  221. '}';
  222. };
  223. }
  224. /**
  225. * Performs a runtime check that the provided object is indeed a
  226. * SafeStyle object, and returns its value.
  227. *
  228. * @param {!goog.html.SafeStyle} safeStyle The object to extract from.
  229. * @return {string} The safeStyle object's contained string, unless
  230. * the run-time type check fails. In that case, {@code unwrap} returns an
  231. * innocuous string, or, if assertions are enabled, throws
  232. * {@code goog.asserts.AssertionError}.
  233. */
  234. goog.html.SafeStyle.unwrap = function(safeStyle) {
  235. // Perform additional Run-time type-checking to ensure that
  236. // safeStyle is indeed an instance of the expected type. This
  237. // provides some additional protection against security bugs due to
  238. // application code that disables type checks.
  239. // Specifically, the following checks are performed:
  240. // 1. The object is an instance of the expected type.
  241. // 2. The object is not an instance of a subclass.
  242. // 3. The object carries a type marker for the expected type. "Faking" an
  243. // object requires a reference to the type marker, which has names intended
  244. // to stand out in code reviews.
  245. if (safeStyle instanceof goog.html.SafeStyle &&
  246. safeStyle.constructor === goog.html.SafeStyle &&
  247. safeStyle.SAFE_STYLE_TYPE_MARKER_GOOG_HTML_SECURITY_PRIVATE_ ===
  248. goog.html.SafeStyle.TYPE_MARKER_GOOG_HTML_SECURITY_PRIVATE_) {
  249. return safeStyle.privateDoNotAccessOrElseSafeStyleWrappedValue_;
  250. } else {
  251. goog.asserts.fail('expected object of type SafeStyle, got \'' +
  252. safeStyle + '\' of type ' + goog.typeOf(safeStyle));
  253. return 'type_error:SafeStyle';
  254. }
  255. };
  256. /**
  257. * Package-internal utility method to create SafeStyle instances.
  258. *
  259. * @param {string} style The string to initialize the SafeStyle object with.
  260. * @return {!goog.html.SafeStyle} The initialized SafeStyle object.
  261. * @package
  262. */
  263. goog.html.SafeStyle.createSafeStyleSecurityPrivateDoNotAccessOrElse = function(
  264. style) {
  265. return new goog.html.SafeStyle().initSecurityPrivateDoNotAccessOrElse_(style);
  266. };
  267. /**
  268. * Called from createSafeStyleSecurityPrivateDoNotAccessOrElse(). This
  269. * method exists only so that the compiler can dead code eliminate static
  270. * fields (like EMPTY) when they're not accessed.
  271. * @param {string} style
  272. * @return {!goog.html.SafeStyle}
  273. * @private
  274. */
  275. goog.html.SafeStyle.prototype.initSecurityPrivateDoNotAccessOrElse_ = function(
  276. style) {
  277. this.privateDoNotAccessOrElseSafeStyleWrappedValue_ = style;
  278. return this;
  279. };
  280. /**
  281. * A SafeStyle instance corresponding to the empty string.
  282. * @const {!goog.html.SafeStyle}
  283. */
  284. goog.html.SafeStyle.EMPTY =
  285. goog.html.SafeStyle.createSafeStyleSecurityPrivateDoNotAccessOrElse('');
  286. /**
  287. * The innocuous string generated by goog.html.SafeStyle.create when passed
  288. * an unsafe value.
  289. * @const {string}
  290. */
  291. goog.html.SafeStyle.INNOCUOUS_STRING = 'zClosurez';
  292. /**
  293. * Mapping of property names to their values.
  294. * We don't support numbers even though some values might be numbers (e.g.
  295. * line-height or 0 for any length). The reason is that most numeric values need
  296. * units (e.g. '1px') and allowing numbers could cause users forgetting about
  297. * them.
  298. * @typedef {!Object<string, goog.string.Const|string>}
  299. */
  300. goog.html.SafeStyle.PropertyMap;
  301. /**
  302. * Creates a new SafeStyle object from the properties specified in the map.
  303. * @param {goog.html.SafeStyle.PropertyMap} map Mapping of property names to
  304. * their values, for example {'margin': '1px'}. Names must consist of
  305. * [-_a-zA-Z0-9]. Values might be strings consisting of
  306. * [-,.'"%_!# a-zA-Z0-9], where " and ' must be properly balanced. We also
  307. * allow simple functions like rgb() and url() which sanitizes its contents.
  308. * Other values must be wrapped in goog.string.Const. Null value causes
  309. * skipping the property.
  310. * @return {!goog.html.SafeStyle}
  311. * @throws {Error} If invalid name is provided.
  312. * @throws {goog.asserts.AssertionError} If invalid value is provided. With
  313. * disabled assertions, invalid value is replaced by
  314. * goog.html.SafeStyle.INNOCUOUS_STRING.
  315. */
  316. goog.html.SafeStyle.create = function(map) {
  317. var style = '';
  318. for (var name in map) {
  319. if (!/^[-_a-zA-Z0-9]+$/.test(name)) {
  320. throw Error('Name allows only [-_a-zA-Z0-9], got: ' + name);
  321. }
  322. var value = map[name];
  323. if (value == null) {
  324. continue;
  325. }
  326. if (value instanceof goog.string.Const) {
  327. value = goog.string.Const.unwrap(value);
  328. // These characters can be used to change context and we don't want that
  329. // even with const values.
  330. goog.asserts.assert(!/[{;}]/.test(value), 'Value does not allow [{;}].');
  331. } else {
  332. value = String(value);
  333. if (!goog.html.SafeStyle.VALUE_RE_.test(
  334. value.replace(goog.html.SafeUrl.URL_RE_, 'url'))) {
  335. goog.asserts.fail(
  336. 'String value allows only [-,."\'%_!# a-zA-Z0-9] and simple ' +
  337. 'functions, got: ' + value);
  338. value = goog.html.SafeStyle.INNOCUOUS_STRING;
  339. } else if (!goog.html.SafeStyle.hasBalancedQuotes_(value)) {
  340. goog.asserts.fail(
  341. 'String value requires balanced quotes, got: ' + value);
  342. value = goog.html.SafeStyle.INNOCUOUS_STRING;
  343. } else {
  344. value = goog.html.SafeStyle.sanitizeUrl_(value);
  345. }
  346. }
  347. style += name + ':' + value + ';';
  348. }
  349. if (!style) {
  350. return goog.html.SafeStyle.EMPTY;
  351. }
  352. goog.html.SafeStyle.checkStyle_(style);
  353. return goog.html.SafeStyle.createSafeStyleSecurityPrivateDoNotAccessOrElse(
  354. style);
  355. };
  356. /**
  357. * Checks that quotes (" and ') are properly balanced inside a string. Assumes
  358. * that neither escape (\) nor any other character that could result in
  359. * breaking out of a string parsing context are allowed;
  360. * see http://www.w3.org/TR/css3-syntax/#string-token-diagram.
  361. * @param {string} value Untrusted CSS property value.
  362. * @return {boolean} True if property value is safe with respect to quote
  363. * balancedness.
  364. * @private
  365. */
  366. goog.html.SafeStyle.hasBalancedQuotes_ = function(value) {
  367. var outsideSingle = true;
  368. var outsideDouble = true;
  369. for (var i = 0; i < value.length; i++) {
  370. var c = value.charAt(i);
  371. if (c == "'" && outsideDouble) {
  372. outsideSingle = !outsideSingle;
  373. } else if (c == '"' && outsideSingle) {
  374. outsideDouble = !outsideDouble;
  375. }
  376. }
  377. return outsideSingle && outsideDouble;
  378. };
  379. // Keep in sync with the error string in create().
  380. /**
  381. * Regular expression for safe values.
  382. *
  383. * Quotes (" and ') are allowed, but a check must be done elsewhere to ensure
  384. * they're balanced.
  385. *
  386. * ',' allows multiple values to be assigned to the same property
  387. * (e.g. background-attachment or font-family) and hence could allow
  388. * multiple values to get injected, but that should pose no risk of XSS.
  389. *
  390. * The expression inside () checks only for XSS safety, not for CSS validity.
  391. * @const {!RegExp}
  392. * @private
  393. */
  394. goog.html.SafeStyle.VALUE_RE_ = new RegExp(
  395. '^([-,."\'%_!# a-zA-Z0-9]+|(hsl|hsla|rgb|rgba' +
  396. '|(rotate|scale|translate)(X|Y|Z|3d)?)' +
  397. '\\([-0-9a-z.%, ]+\\))$');
  398. /**
  399. * Regular expression for url(). We support URLs allowed by
  400. * https://www.w3.org/TR/css-syntax-3/#url-token-diagram without using escape
  401. * sequences. Use percent-encoding if you need to use special characters like
  402. * backslash.
  403. * @private @const {!RegExp}
  404. */
  405. goog.html.SafeUrl.URL_RE_ = new RegExp(
  406. '\\b(url\\([ \t\n]*)(' +
  407. '\'[ -&(-\\[\\]-~]*\'' + // Printable characters except ' and \.
  408. '|"[ !#-\\[\\]-~]*"' + // Printable characters except " and \.
  409. '|[!#-&*-\\[\\]-~]*' + // Printable characters except [ "'()\\].
  410. ')([ \t\n]*\\))',
  411. 'g');
  412. /**
  413. * Sanitize URLs inside url().
  414. *
  415. * NOTE: We could also consider using CSS.escape once that's available in the
  416. * browsers. However, loosely matching URL e.g. with url\(.*\) and then escaping
  417. * the contents would result in a slightly different language than CSS leading
  418. * to confusion of users. E.g. url(")") is valid in CSS but it would be invalid
  419. * as seen by our parser. On the other hand, url(\) is invalid in CSS but our
  420. * parser would be fine with it.
  421. *
  422. * @param {string} value Untrusted CSS property value.
  423. * @return {string}
  424. * @private
  425. */
  426. goog.html.SafeStyle.sanitizeUrl_ = function(value) {
  427. return value.replace(
  428. goog.html.SafeUrl.URL_RE_, function(match, before, url, after) {
  429. var quote = '';
  430. url = url.replace(/^(['"])(.*)\1$/, function(match, start, inside) {
  431. quote = start;
  432. return inside;
  433. });
  434. var sanitized = goog.html.SafeUrl.sanitize(url).getTypedStringValue();
  435. return before + quote + sanitized + quote + after;
  436. });
  437. };
  438. /**
  439. * Creates a new SafeStyle object by concatenating the values.
  440. * @param {...(!goog.html.SafeStyle|!Array<!goog.html.SafeStyle>)} var_args
  441. * SafeStyles to concatenate.
  442. * @return {!goog.html.SafeStyle}
  443. */
  444. goog.html.SafeStyle.concat = function(var_args) {
  445. var style = '';
  446. /**
  447. * @param {!goog.html.SafeStyle|!Array<!goog.html.SafeStyle>} argument
  448. */
  449. var addArgument = function(argument) {
  450. if (goog.isArray(argument)) {
  451. goog.array.forEach(argument, addArgument);
  452. } else {
  453. style += goog.html.SafeStyle.unwrap(argument);
  454. }
  455. };
  456. goog.array.forEach(arguments, addArgument);
  457. if (!style) {
  458. return goog.html.SafeStyle.EMPTY;
  459. }
  460. return goog.html.SafeStyle.createSafeStyleSecurityPrivateDoNotAccessOrElse(
  461. style);
  462. };