Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️🐛 [Trusted Types] Make createExtensionScript Trusted Types compatible #39163

Merged
merged 35 commits into from
Jul 12, 2023
Merged
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
3912664
extension script tt compatible
youssef-attia Jun 16, 2023
87064b0
Merge pull request #5 from youssef-attia/tt_extension_script
eozmen410 Jun 16, 2023
6c787f4
Merge branch 'ampproject:main' into tt_extension_script
youssef-attia Jun 16, 2023
665a21e
Update extension-script.js
youssef-attia Jun 16, 2023
dc900b3
Merge branch 'ampproject:main' into tt_extension_script
youssef-attia Jun 20, 2023
84ab19b
testing if base case works
youssef-attia Jun 20, 2023
e1cc36d
console log url
youssef-attia Jun 20, 2023
9434ea1
allow consolelog
youssef-attia Jun 20, 2023
b006aae
Merge branch 'ampproject:main' into tt_extension_script
youssef-attia Jun 20, 2023
9cb2126
fix console log exempt
youssef-attia Jun 20, 2023
6171822
typo
youssef-attia Jun 20, 2023
1deb015
fix format
youssef-attia Jun 20, 2023
d07ef68
updated cdn and allowlist
youssef-attia Jun 21, 2023
0bb6bcc
linter
youssef-attia Jun 21, 2023
ff9b130
Merge branch 'ampproject:main' into tt_extension_script
youssef-attia Jun 21, 2023
9d6921e
add fonts to allowlist
youssef-attia Jun 21, 2023
0ff709d
Lint and fix logic
youssef-attia Jun 21, 2023
3e0fb8e
Update policy
youssef-attia Jun 28, 2023
0056df0
unused ignore statement
youssef-attia Jun 29, 2023
4bd9ea6
policy pattern change
youssef-attia Jun 29, 2023
d98063c
lint
youssef-attia Jun 29, 2023
2b92623
checking if localhost sufficient
youssef-attia Jun 29, 2023
b17919b
typo
youssef-attia Jun 29, 2023
ce267c5
get mode from win
youssef-attia Jun 29, 2023
73bde29
update comment
youssef-attia Jun 29, 2023
61de92c
adding font cdn
youssef-attia Jun 29, 2023
611f859
check if test detection works
youssef-attia Jun 29, 2023
0024c00
reformat
youssef-attia Jun 29, 2023
0bd9360
comment and policy change
youssef-attia Jun 29, 2023
e3c3570
reverting to last passing visual test
youssef-attia Jun 29, 2023
19de36c
removing filename limitations
youssef-attia Jun 29, 2023
781cd9c
change policy format
youssef-attia Jun 29, 2023
f9c963f
linting and format changes
youssef-attia Jun 29, 2023
fe2b68a
move regex to top level constant
youssef-attia Jul 5, 2023
424b231
typo
youssef-attia Jul 5, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/service/extension-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import {getMode} from '../mode';
const CUSTOM_TEMPLATES = ['amp-mustache'];
const LATEST_VERSION = 'latest';

const cdnRegexUrl = new RegExp(
// eslint-disable-next-line local/no-forbidden-terms
'^https://([a-zA-Z0-9_-]+.)?cdn.ampproject.org(/.*)?$'
);
const testCdnRegexUrl = new RegExp('^([a-zA-Z0-9_-]+.)?localhost$');

/**
* Calculate the base url for any scripts.
* @param {!Location} location The window's location
Expand Down Expand Up @@ -155,7 +161,30 @@ export function createExtensionScript(win, extensionId, version) {
version,
getMode(win).localDev
);
scriptElement.src = scriptSrc;

let policy = {
createScriptURL: function (url) {
// Only allow trusted URLs
if (
cdnRegexUrl.test(url) ||
(getMode().test && testCdnRegexUrl.test(new URL(url).hostname)) ||
new URL(url).host === 'fonts.googleapis.com'
) {
return url;
} else {
return '';
}
},
};

if (self.trustedTypes && self.trustedTypes.createPolicy) {
policy = self.trustedTypes.createPolicy(
'extension-script#createExtensionScript',
policy
);
}

scriptElement.src = policy.createScriptURL(scriptSrc);
return scriptElement;
}

Expand Down