123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- goog.provide('goog.html.safeUrlTest');
- goog.require('goog.html.SafeUrl');
- goog.require('goog.html.TrustedResourceUrl');
- goog.require('goog.i18n.bidi.Dir');
- goog.require('goog.object');
- goog.require('goog.string.Const');
- goog.require('goog.testing.jsunit');
- goog.require('goog.userAgent');
- goog.setTestOnly('goog.html.safeUrlTest');
- function testSafeUrl() {
- var safeUrl = goog.html.SafeUrl.fromConstant(
- goog.string.Const.from('javascript:trusted();'));
- var extracted = goog.html.SafeUrl.unwrap(safeUrl);
- assertEquals('javascript:trusted();', extracted);
- assertEquals('javascript:trusted();', goog.html.SafeUrl.unwrap(safeUrl));
- assertEquals('SafeUrl{javascript:trusted();}', String(safeUrl));
-
- assertEquals(goog.i18n.bidi.Dir.LTR, safeUrl.getDirection());
-
- assertTrue(safeUrl.implementsGoogStringTypedString);
- assertTrue(safeUrl.implementsGoogI18nBidiDirectionalString);
- }
- function testSafeUrlFromBlob_withSafeType() {
- if (isIE9OrLower()) {
- return;
- }
- assertBlobTypeIsSafe('image/png', true);
- assertBlobTypeIsSafe('iMage/pNg', true);
- assertBlobTypeIsSafe('video/mpeg', true);
- assertBlobTypeIsSafe('video/ogg', true);
- assertBlobTypeIsSafe('video/mp4', true);
- assertBlobTypeIsSafe('video/ogg', true);
- assertBlobTypeIsSafe('video/webm', true);
- }
- function testSafeUrlFromBlob_withUnsafeType() {
- if (isIE9OrLower()) {
- return;
- }
- assertBlobTypeIsSafe('', false);
- assertBlobTypeIsSafe('ximage/png', false);
- assertBlobTypeIsSafe('image/pngx', false);
- assertBlobTypeIsSafe('video/whatever', false);
- assertBlobTypeIsSafe('video/', false);
- }
- function isIE9OrLower() {
- return goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('10');
- }
- function assertBlobTypeIsSafe(type, isSafe) {
- var safeUrl = goog.html.SafeUrl.fromBlob(new Blob(['test'], {type: type}));
- var extracted = goog.html.SafeUrl.unwrap(safeUrl);
- if (isSafe) {
- assertEquals('blob:', extracted.substring(0, 5));
- } else {
- assertEquals(goog.html.SafeUrl.INNOCUOUS_STRING, extracted);
- }
- }
- function testSafeUrlFromDataUrl_withSafeType() {
- assertDataUrlIsSafe(
- 'data:image/png;base64,' +
- 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/=',
- true);
- assertDataUrlIsSafe('dATa:iMage/pNg;bASe64,abc===', true);
- assertDataUrlIsSafe('data:image/webp;base64,abc===', true);
- assertDataUrlIsSafe('data:video/mpeg;base64,abc', true);
- assertDataUrlIsSafe('data:video/ogg;base64,z=', true);
- assertDataUrlIsSafe('data:video/mp4;base64,z=', true);
- assertDataUrlIsSafe('data:video/ogg;base64,z=', true);
- assertDataUrlIsSafe('data:video/webm;base64,z=', true);
- }
- function testSafeUrlFromDataUrl_withUnsafeType() {
- assertDataUrlIsSafe('', false);
- assertDataUrlIsSafe(':', false);
- assertDataUrlIsSafe('data:', false);
- assertDataUrlIsSafe('not-data:image/png;base64,z=', false);
- assertDataUrlIsSafe(' data:image/png;base64,z=', false);
- assertDataUrlIsSafe('data:image/png;base64,z= ', false);
- assertDataUrlIsSafe('data:ximage/png', false);
- assertDataUrlIsSafe('data:ximage/png;base64,z=', false);
- assertDataUrlIsSafe('data:image/pngx;base64,z=', false);
- assertDataUrlIsSafe('data:video/whatever;base64,z=', false);
- assertDataUrlIsSafe('data:video/;base64,z=', false);
- assertDataUrlIsSafe('data:image/png;base64,', false);
- assertDataUrlIsSafe('data:image/png;base64,abc=!', false);
- assertDataUrlIsSafe('data:image/png;base64,$$', false);
- assertDataUrlIsSafe('data:image/png;base64,\0', false);
- assertDataUrlIsSafe('data:video/mp4;baze64,z=', false);
- assertDataUrlIsSafe('data:video/mp4;,z=', false);
- assertDataUrlIsSafe('data:text/html,sdfsdfsdfsfsdfs;base64,anything', false);
-
- assertDataUrlIsSafe('data:image/svg+xml;base64,abc', false);
- }
- function assertDataUrlIsSafe(url, isSafe) {
- var safeUrl = goog.html.SafeUrl.fromDataUrl(url);
- assertEquals(
- isSafe ? url : goog.html.SafeUrl.INNOCUOUS_STRING,
- goog.html.SafeUrl.unwrap(safeUrl));
- }
- function testSafeUrlFromTelUrl_withSafeType() {
- assertTelUrlIsSafe('tEl:+1(23)129-29192A.ABC#;eXt=29', true);
- assertTelUrlIsSafe('tEL:123;randmomparam=123', true);
- }
- function testSafeUrlFromTelUrl_withUnsafeType() {
- assertTelUrlIsSafe('', false);
- assertTelUrlIsSafe(':', false);
- assertTelUrlIsSafe('tell:', false);
- assertTelUrlIsSafe('not-tel:+1', false);
- assertTelUrlIsSafe(' tel:+1', false);
- }
- function assertTelUrlIsSafe(url, isSafe) {
- var safeUrl = goog.html.SafeUrl.fromTelUrl(url);
- assertEquals(
- isSafe ? url : goog.html.SafeUrl.INNOCUOUS_STRING,
- goog.html.SafeUrl.unwrap(safeUrl));
- }
- function testFromTrustedResourceUrl() {
- var url = goog.string.Const.from('test');
- var trustedResourceUrl = goog.html.TrustedResourceUrl.fromConstant(url);
- var safeUrl = goog.html.SafeUrl.fromTrustedResourceUrl(trustedResourceUrl);
- assertEquals(
- goog.string.Const.unwrap(url), goog.html.SafeUrl.unwrap(safeUrl));
- }
- function testUnwrap() {
- var privateFieldName = 'privateDoNotAccessOrElseSafeHtmlWrappedValue_';
- var markerFieldName = 'SAFE_URL_TYPE_MARKER_GOOG_HTML_SECURITY_PRIVATE_';
- var propNames = goog.object.getKeys(goog.html.SafeUrl.sanitize(''));
- assertContains(privateFieldName, propNames);
- assertContains(markerFieldName, propNames);
- var evil = {};
- evil[privateFieldName] = 'javascript:evil()';
- evil[markerFieldName] = {};
- var exception = assertThrows(function() { goog.html.SafeUrl.unwrap(evil); });
- assertContains('expected object of type SafeUrl', exception.message);
- }
- function assertGoodUrl(url) {
- var expected = url;
- if (url.implementsGoogStringTypedString) {
- expected = url.getTypedStringValue();
- }
- var safeUrl = goog.html.SafeUrl.sanitize(url);
- var extracted = goog.html.SafeUrl.unwrap(safeUrl);
- assertEquals(expected, extracted);
- }
- function assertBadUrl(url) {
- assertEquals(
- goog.html.SafeUrl.INNOCUOUS_STRING,
- goog.html.SafeUrl.unwrap(goog.html.SafeUrl.sanitize(url)));
- }
- function testSafeUrlSanitize_validatesUrl() {
-
- assertGoodUrl('http://example.com/');
- assertGoodUrl('https://example.com');
- assertGoodUrl('mailto:foo@example.com');
- assertGoodUrl('ftp://example.com');
- assertGoodUrl('ftp://username@example.com');
- assertGoodUrl('ftp://username:password@example.com');
-
- assertGoodUrl('HTtp://example.com/');
-
- assertGoodUrl('https://example.com/path?foo=bar#baz');
-
- assertGoodUrl('//example.com/path');
-
- assertGoodUrl('/path');
- assertGoodUrl('/path?foo=bar#baz');
-
- assertGoodUrl('path');
- assertGoodUrl('path?foo=bar#baz');
- assertGoodUrl('p//ath');
- assertGoodUrl('p//ath?foo=bar#baz');
- assertGoodUrl('#baz');
-
- assertGoodUrl('?:');
-
- assertGoodUrl(goog.string.Const.from('http://example.com/'));
-
- assertBadUrl('javascript:evil();');
- assertBadUrl('javascript:evil();//\nhttp://good.com/');
- assertBadUrl('data:blah');
-
- assertBadUrl(':');
-
- assertBadUrl('\\:');
-
- assertBadUrl(':/:');
-
-
- assertBadUrl('path\n:');
-
- assertBadUrl(goog.string.Const.from('data:blah'));
- }
- function testSafeUrlSanitize_idempotentForSafeUrlArgument() {
-
- var safeUrl = goog.html.SafeUrl.sanitize('https://www.google.com/');
- var safeUrl2 = goog.html.SafeUrl.sanitize(safeUrl);
- assertEquals(
- goog.html.SafeUrl.unwrap(safeUrl), goog.html.SafeUrl.unwrap(safeUrl2));
-
-
- safeUrl = goog.html.SafeUrl.sanitize('disallowed:foo');
- safeUrl2 = goog.html.SafeUrl.sanitize(safeUrl);
- assertEquals(
- goog.html.SafeUrl.unwrap(safeUrl), goog.html.SafeUrl.unwrap(safeUrl2));
- }
|