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;
/**
* 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;
/**
@ -2090,7 +2090,7 @@ export interface Output {
*/
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;
/**
@ -3047,7 +3047,7 @@ export interface OutputNormalized {
*/
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;
/**

View File

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

View File

@ -4363,7 +4363,7 @@
]
},
"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"
},
"UmdNamedDefine": {

View File

@ -5208,13 +5208,13 @@ Object {
"output-trusted-types-policy-name": Object {
"configs": Array [
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,
"path": "output.trustedTypesPolicyName",
"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,
"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;
/**
* 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;
@ -7943,7 +7943,7 @@ declare interface OutputNormalized {
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;