forked from webpack/webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Trusted Types in EvalSourceMapDevToolPlugin
- Loading branch information
Showing
12 changed files
with
257 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
MIT License http://www.opensource.org/licenses/mit-license.php | ||
*/ | ||
|
||
"use strict"; | ||
|
||
const RuntimeGlobals = require("../RuntimeGlobals"); | ||
const Template = require("../Template"); | ||
const HelperRuntimeModule = require("./HelperRuntimeModule"); | ||
|
||
class CreateScriptRuntimeModule extends HelperRuntimeModule { | ||
constructor() { | ||
super("trusted types script"); | ||
} | ||
|
||
/** | ||
* @returns {string} runtime code | ||
*/ | ||
generate() { | ||
const { compilation } = this; | ||
const { runtimeTemplate, outputOptions } = compilation; | ||
const { trustedTypes } = outputOptions; | ||
const fn = RuntimeGlobals.createScript; | ||
|
||
return Template.asString( | ||
`${fn} = ${runtimeTemplate.returningFunction( | ||
trustedTypes | ||
? `${RuntimeGlobals.getTrustedTypesPolicy}().createScript(script)` | ||
: "script", | ||
"script" | ||
)};` | ||
); | ||
} | ||
} | ||
|
||
module.exports = CreateScriptRuntimeModule; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
MIT License http://www.opensource.org/licenses/mit-license.php | ||
*/ | ||
|
||
"use strict"; | ||
|
||
const RuntimeGlobals = require("../RuntimeGlobals"); | ||
const Template = require("../Template"); | ||
const HelperRuntimeModule = require("./HelperRuntimeModule"); | ||
|
||
class GetTrustedTypesPolicyRuntimeModule extends HelperRuntimeModule { | ||
/** | ||
* @param {Set<string>} runtimeRequirements runtime requirements | ||
*/ | ||
constructor(runtimeRequirements) { | ||
super("trusted types policy"); | ||
this.runtimeRequirements = runtimeRequirements; | ||
} | ||
|
||
/** | ||
* @returns {string} runtime code | ||
*/ | ||
generate() { | ||
const { compilation } = this; | ||
const { runtimeTemplate, outputOptions } = compilation; | ||
const { trustedTypes } = outputOptions; | ||
const fn = RuntimeGlobals.getTrustedTypesPolicy; | ||
|
||
return Template.asString([ | ||
"var policy;", | ||
`${fn} = ${runtimeTemplate.basicFunction("", [ | ||
"// Create Trusted Type policy if Trusted Types are available and the policy doesn't exist yet.", | ||
"if (policy === undefined) {", | ||
Template.indent([ | ||
"policy = {", | ||
Template.indent( | ||
[ | ||
...(this.runtimeRequirements.has(RuntimeGlobals.createScript) | ||
? [ | ||
`createScript: ${runtimeTemplate.returningFunction( | ||
"script", | ||
"script" | ||
)}` | ||
] | ||
: []), | ||
...(this.runtimeRequirements.has(RuntimeGlobals.createScriptUrl) | ||
? [ | ||
`createScriptURL: ${runtimeTemplate.returningFunction( | ||
"url", | ||
"url" | ||
)}` | ||
] | ||
: []) | ||
].join(",\n") | ||
), | ||
"};", | ||
...(trustedTypes | ||
? [ | ||
'if (typeof trustedTypes !== "undefined" && trustedTypes.createPolicy) {', | ||
Template.indent([ | ||
`policy = trustedTypes.createPolicy(${JSON.stringify( | ||
trustedTypes.policyName | ||
)}, policy);` | ||
]), | ||
"}" | ||
] | ||
: []) | ||
]), | ||
"}", | ||
"return policy;" | ||
])};` | ||
]); | ||
} | ||
} | ||
|
||
module.exports = GetTrustedTypesPolicyRuntimeModule; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
it("should pass TrustedScript to eval", function() { | ||
class TrustedScript { | ||
constructor(script) { | ||
this._script = script; | ||
} | ||
}; | ||
|
||
var policy = __webpack_require__.tt(); | ||
policy.createScript = jest.fn((script) => { | ||
expect(typeof script).toEqual("string"); | ||
return new TrustedScript(script); | ||
}); | ||
|
||
const globalEval = eval; | ||
window.module = {}; | ||
window.eval = jest.fn((x) => { | ||
expect(x).toBeInstanceOf(TrustedScript); | ||
return globalEval(x._script); | ||
}); | ||
|
||
require("./test.js"); | ||
expect(window.module.exports).toBeInstanceOf(Object); | ||
expect(window.module.exports.foo).toEqual('bar'); | ||
|
||
const testPattern = "var test = {\\s*foo: \'bar\'\\s*};\\s*module.exports = test;"; | ||
expect(policy.createScript).toBeCalledWith( | ||
expect.stringMatching(testPattern) | ||
); | ||
expect(window.eval).toBeCalledWith( | ||
expect.objectContaining({ | ||
_script: expect.stringMatching(testPattern) | ||
}) | ||
); | ||
}); |
Oops, something went wrong.