Skip using Trusted Types checks if trustedTypesPolicyName is empty.

This commit is contained in:
Krzysztof Kotowicz 2021-05-07 10:30:57 +02:00
parent b6e112dabe
commit 3561285ea8
8 changed files with 50 additions and 8 deletions

View File

@ -571,7 +571,7 @@ export type StrictModuleErrorHandling = boolean;
*/ */
export type StrictModuleExceptionHandling = boolean; export type StrictModuleExceptionHandling = boolean;
/** /**
* The name of the trusted types policy created by webpack to serve bundle chunks. Defaults to 'webpack'. * The name of the Trusted Types policy created by webpack to serve bundle chunks. Defaults to 'webpack'. Set to an empty string, if webpack should not use Trusted Types.
*/ */
export type TrustedTypesPolicyName = string; export type TrustedTypesPolicyName = string;
/** /**
@ -2090,7 +2090,7 @@ export interface Output {
*/ */
strictModuleExceptionHandling?: StrictModuleExceptionHandling; strictModuleExceptionHandling?: StrictModuleExceptionHandling;
/** /**
* The name of the trusted types policy created by webpack to serve bundle chunks. Defaults to 'webpack'. * The name of the Trusted Types policy created by webpack to serve bundle chunks. Defaults to 'webpack'. Set to an empty string, if webpack should not use Trusted Types.
*/ */
trustedTypesPolicyName?: TrustedTypesPolicyName; trustedTypesPolicyName?: TrustedTypesPolicyName;
/** /**
@ -3047,7 +3047,7 @@ export interface OutputNormalized {
*/ */
strictModuleExceptionHandling?: StrictModuleExceptionHandling; strictModuleExceptionHandling?: StrictModuleExceptionHandling;
/** /**
* The name of the trusted types policy created by webpack to serve bundle chunks. Defaults to 'webpack'. * The name of the Trusted Types policy created by webpack to serve bundle chunks. Defaults to 'webpack'. Set to an empty string, if webpack should not use Trusted Types.
*/ */
trustedTypesPolicyName?: TrustedTypesPolicyName; trustedTypesPolicyName?: TrustedTypesPolicyName;
/** /**

View File

@ -22,6 +22,13 @@ class TrustedTypesRuntimeModule extends HelperRuntimeModule {
const { trustedTypesPolicyName } = outputOptions; const { trustedTypesPolicyName } = outputOptions;
const fn = RuntimeGlobals.createScriptURL; const fn = RuntimeGlobals.createScriptURL;
if (trustedTypesPolicyName === "") {
// Skip Trusted Types logic.
return Template.asString([
`${fn} = ${runtimeTemplate.basicFunction("url", ["return url;"])};`
]);
}
return Template.asString([ return Template.asString([
"var policy;", "var policy;",
`${fn} = ${runtimeTemplate.basicFunction("url", [ `${fn} = ${runtimeTemplate.basicFunction("url", [

View File

@ -4363,7 +4363,7 @@
] ]
}, },
"TrustedTypesPolicyName": { "TrustedTypesPolicyName": {
"description": "The name of the trusted types policy created by webpack to serve bundle chunks. Defaults to 'webpack'.", "description": "The name of the Trusted Types policy created by webpack to serve bundle chunks. Defaults to 'webpack'. Set to an empty string, if webpack should not use Trusted Types.",
"type": "string" "type": "string"
}, },
"UmdNamedDefine": { "UmdNamedDefine": {

View File

@ -5208,13 +5208,13 @@ Object {
"output-trusted-types-policy-name": Object { "output-trusted-types-policy-name": Object {
"configs": Array [ "configs": Array [
Object { Object {
"description": "The name of the trusted types policy created by webpack to serve bundle chunks. Defaults to 'webpack'.", "description": "The name of the Trusted Types policy created by webpack to serve bundle chunks. Defaults to 'webpack'. Set to an empty string, if webpack should not use Trusted Types.",
"multiple": false, "multiple": false,
"path": "output.trustedTypesPolicyName", "path": "output.trustedTypesPolicyName",
"type": "string", "type": "string",
}, },
], ],
"description": "The name of the trusted types policy created by webpack to serve bundle chunks. Defaults to 'webpack'.", "description": "The name of the Trusted Types policy created by webpack to serve bundle chunks. Defaults to 'webpack'. Set to an empty string, if webpack should not use Trusted Types.",
"multiple": false, "multiple": false,
"simpleType": "string", "simpleType": "string",
}, },

View File

@ -0,0 +1,21 @@
it("should skip trusted types logic when policy name is empty", function() {
// emulate trusted types in a window object
const noop = (i) => i
const rules = {
createScriptURL: noop,
}
window.trustedTypes = {
createPolicy: () => rules
}
const createScriptURLSpy = jest.spyOn(rules, 'createScriptURL')
const createPolicySpy = jest.spyOn(window.trustedTypes, 'createPolicy')
const promise = import("./empty?b" /* webpackChunkName: "no-trusted-types-policy-name" */);
var script = document.head._children.pop();
__non_webpack_require__("./no-trusted-types-policy-name.web.js");
expect(script.src).toBe("https://test.cases/path/no-trusted-types-policy-name.web.js");
expect(createScriptURLSpy).not.toHaveBeenCalled();
expect(createPolicySpy).not.toHaveBeenCalled();
return promise;
});

View File

@ -0,0 +1,14 @@
module.exports = {
target: "web",
output: {
chunkFilename: "[name].web.js",
crossOriginLoading: "anonymous",
trustedTypesPolicyName: "" // Skip Trusted Types.
},
performance: {
hints: false
},
optimization: {
minimize: false
}
};

4
types.d.ts vendored
View File

@ -7676,7 +7676,7 @@ declare interface Output {
strictModuleExceptionHandling?: boolean; strictModuleExceptionHandling?: boolean;
/** /**
* The name of the trusted types policy created by webpack to serve bundle chunks. Defaults to 'webpack'. * The name of the Trusted Types policy created by webpack to serve bundle chunks. Defaults to 'webpack'. Set to an empty string, if webpack should not use Trusted Types.
*/ */
trustedTypesPolicyName?: string; trustedTypesPolicyName?: string;
@ -7943,7 +7943,7 @@ declare interface OutputNormalized {
strictModuleExceptionHandling?: boolean; strictModuleExceptionHandling?: boolean;
/** /**
* The name of the trusted types policy created by webpack to serve bundle chunks. Defaults to 'webpack'. * The name of the Trusted Types policy created by webpack to serve bundle chunks. Defaults to 'webpack'. Set to an empty string, if webpack should not use Trusted Types.
*/ */
trustedTypesPolicyName?: string; trustedTypesPolicyName?: string;