Skip to content

Commit

Permalink
Removed url-allow-http policy support. (#165)
Browse files Browse the repository at this point in the history
This is linked to #65.
  • Loading branch information
koto authored May 8, 2019
1 parent f4c35d0 commit f7297a0
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 196 deletions.
65 changes: 32 additions & 33 deletions dist/es5/trustedtypes.build.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/es5/trustedtypes.build.js.map

Large diffs are not rendered by default.

42 changes: 21 additions & 21 deletions dist/es6/trustedtypes.build.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/es6/trustedtypes.build.js.map

Large diffs are not rendered by default.

14 changes: 0 additions & 14 deletions src/data/trustedtypeconfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,12 @@ export class TrustedTypeConfig {
* @param {?string} fallbackPolicyName If present, direct DOM sink usage
* will be passed through this policy (has to be exposed).
* @param {Array<string>} allowedPolicyNames Whitelisted policy names.
* @param {boolean=} allowHttpUrls if true, HTTP(s) urls will be transparently
* treated like TrustedURLs. Applied only if enforcement or logging is
* enabled.
* @param {?string} cspString String with the CSP policy.
*/
constructor(isLoggingEnabled,
isEnforcementEnabled,
fallbackPolicyName,
allowedPolicyNames,
allowHttpUrls = false,
cspString = null) {
/**
* True if logging is enabled.
Expand All @@ -60,12 +56,6 @@ export class TrustedTypeConfig {
*/
this.allowedPolicyNames = allowedPolicyNames;

/**
* True if http(s) URLs should be implicitly treated as TrustedURLs.
* @type {boolean}
*/
this.allowHttpUrls = allowHttpUrls;

/**
* CSP string that defined the policy.
* @type {?string}
Expand Down Expand Up @@ -102,12 +92,9 @@ export class TrustedTypeConfig {
const isLoggingEnabled = true;
const policy = TrustedTypeConfig.parseCSP(cspString);
const enforce = DIRECTIVE_NAME in policy;
let allowHttpUrls = false;
let policies = ['*'];
let fallbackPolicyName = 'default';
if (enforce) {
allowHttpUrls = policy[DIRECTIVE_NAME]
.indexOf('\'url-allow-http\'') !== -1;
policies = policy[DIRECTIVE_NAME].filter((p) => p.charAt(0) !== '\'');
}

Expand All @@ -116,7 +103,6 @@ export class TrustedTypeConfig {
enforce, /* isEnforcementEnabled */
fallbackPolicyName, /* fallbackPolicyName */
policies, /* allowedPolicyNames */
allowHttpUrls,
cspString
);
}
Expand Down
24 changes: 0 additions & 24 deletions src/enforcer.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,6 @@ function parseUrl_(url) {
}
}

/**
* Checks if the URL is a HTTP(s) URL.
* @param {string} url The URL to check.
* @return {boolean} True iff the value is a http(s) URL.
*/
function isHttpUrl_(url) {
const parsedUrl = parseUrl_(url);
if (!parsedUrl) {
return false;
}
return parsedUrl.protocol == 'http:' || parsedUrl.protocol == 'https:';
}

/**
* A map of attribute names to allowed types.
* @type {!Object<string, !Object<string, !Function>>}
Expand Down Expand Up @@ -734,17 +721,6 @@ export class TrustedTypesEnforcer {
}
}


// Apply url-allow-http
if (typeToEnforce === TrustedTypes.TrustedURL &&
this.config_.allowHttpUrls) {
const url = '' + value;
if (isHttpUrl_(url)) {
args[argNumber] = url;
return apply(originalSetter, context, args);
}
}

// Apply a fallback policy, if it exists.
const fallback = this.config_.fallbackPolicyName;
if (fallback) {
Expand Down
92 changes: 0 additions & 92 deletions tests/enforcer_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ describe('TrustedTypesEnforcer', function() {
/* isEnforcementEnabled */ true,
/* fallbackPolicy */ null,
/* allowedPolicyNames */ ['*'],
/* allowHttpUrls */ false,
/* cspString */ 'script-src https:; trusted-types *'
);

Expand All @@ -47,7 +46,6 @@ describe('TrustedTypesEnforcer', function() {
/* isEnforcementEnabled */ false,
/* fallbackPolicy */ null,
/* allowedPolicyNames */ ['*'],
/* allowHttpUrls */ false,
/* cspString */ 'script-src https:'
);

Expand Down Expand Up @@ -520,96 +518,6 @@ describe('TrustedTypesEnforcer', function() {
});
});

describe('url-allow-http config', () => {
let enforcer;
let el;
let policy;

beforeEach(function() {
if (typeof window.URL !== 'function') {
// Skip on IE, url-allow-http relies on URL parsing.
pending();
}
el = document.createElement('a');
enforcer = new TrustedTypesEnforcer(new TrustedTypeConfig(
/* isLoggingEnabled */ false,
/* isEnforcementEnabled */ true,
/* fallbackPolicy */ null,
/* allowedPolicyNames */ ['*'],
/* allowHttpUrls */ true));
enforcer.install();
policy = TrustedTypes.createPolicy(Math.random(), noopPolicy);
});

afterEach(function() {
enforcer.uninstall();
});

it('allows typed values for url sinks', () => {
el.href = policy.createURL('http://example.com/');

expect(el.href).toEqual('http://example.com/');
});

it('allows typed values for with javascript: protocol', () => {
el.href = policy.createURL('javascript:alert(1)');

expect(el.href).toEqual('javascript:alert(1)');
});

it('allows strings with http urls', () => {
el.href = 'http://example.com/';

expect(el.href).toEqual('http://example.com/');
});

it('allows strings with https urls', () => {
el.href = 'https://example.com/';

expect(el.href).toEqual('https://example.com/');
});

it('allows strings with relative urls', () => {
el.href = 'foo/bar';

expect(el.href).toEqual(location.origin + '/foo/bar');
});

it('rejects strings with javascript: URLs', () => {
expect(() => {
el.href = 'javascript:alert(1)';
}).toThrowError(TypeError);

expect(el.href).toEqual('');
});

it('rejects strings with data: URLs', () => {
expect(() => {
el.href = 'data:text/html,<script>alert(1)</scrip' + 't>';
}).toThrowError(TypeError);

expect(el.href).toEqual('');
});

it('rejects malformed URLs', () => {
expect(() => {
el.href = 'https://example.com:demo';
}).toThrowError(TypeError);

expect(el.href).toEqual('');
});

it('rejects http urls for TrustedScriptURL sinks', () => {
const el = document.createElement('script');

expect(() => {
el.src = 'https://evil.com';
}).toThrowError(TypeError);

expect(el.src).toEqual('');
});
});

describe('enforcement disables null assignments', function() {
let enforcer;

Expand Down
10 changes: 0 additions & 10 deletions tests/trustedtypeconfig_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,6 @@ describe('TrustedTypeConfig', () => {
.allowedPolicyNames).toEqual([]);
});

it('does not allow http urls by default', () => {
expect(TrustedTypeConfig.fromCSP('trusted-types')
.allowHttpUrls).toEqual(false);
});

it('recognizes url-allow-http', () => {
expect(TrustedTypeConfig.fromCSP('trusted-types \'url-allow-http\'')
.allowHttpUrls).toEqual(true);
});

it('passes the CSP string to config object', () => {
const csp = 'trusted-types a b c; script-src foo';

Expand Down

0 comments on commit f7297a0

Please sign in to comment.