index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. "use strict";
  2. /**
  3. * Copyright (c) 2015-present, Waysact Pty Ltd
  4. *
  5. * This source code is licensed under the MIT license found in the
  6. * LICENSE file in the root directory of this source tree.
  7. */
  8. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  9. if (k2 === undefined) k2 = k;
  10. Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
  11. }) : (function(o, m, k, k2) {
  12. if (k2 === undefined) k2 = k;
  13. o[k2] = m[k];
  14. }));
  15. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  16. Object.defineProperty(o, "default", { enumerable: true, value: v });
  17. }) : function(o, v) {
  18. o["default"] = v;
  19. });
  20. var __importStar = (this && this.__importStar) || function (mod) {
  21. if (mod && mod.__esModule) return mod;
  22. var result = {};
  23. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  24. __setModuleDefault(result, mod);
  25. return result;
  26. };
  27. Object.defineProperty(exports, "__esModule", { value: true });
  28. exports.SubresourceIntegrityPlugin = void 0;
  29. const crypto_1 = require("crypto");
  30. const webpack_1 = require("webpack");
  31. const plugin_1 = require("./plugin");
  32. const reporter_1 = require("./reporter");
  33. const util_1 = require("./util");
  34. const thisPluginName = "webpack-subresource-integrity";
  35. // https://www.w3.org/TR/2016/REC-SRI-20160623/#cryptographic-hash-functions
  36. const standardHashFuncNames = ["sha256", "sha384", "sha512"];
  37. let getHtmlWebpackPluginHooks = null;
  38. class AddLazySriRuntimeModule extends webpack_1.RuntimeModule {
  39. constructor(sriHashes, chunkName) {
  40. super(`webpack-subresource-integrity lazy hashes for direct children of chunk ${chunkName}`);
  41. this.sriHashes = sriHashes;
  42. }
  43. generate() {
  44. return webpack_1.Template.asString([
  45. `Object.assign(${util_1.sriHashVariableReference}, ${JSON.stringify(this.sriHashes)});`,
  46. ]);
  47. }
  48. }
  49. /**
  50. * The webpack-subresource-integrity plugin.
  51. *
  52. * @public
  53. */
  54. class SubresourceIntegrityPlugin {
  55. /**
  56. * Create a new instance.
  57. *
  58. * @public
  59. */
  60. constructor(options = {}) {
  61. /**
  62. * @internal
  63. */
  64. this.setup = (compilation) => {
  65. const reporter = new reporter_1.Reporter(compilation, thisPluginName);
  66. if (!this.validateOptions(compilation, reporter) ||
  67. !this.isEnabled(compilation)) {
  68. return;
  69. }
  70. const plugin = new plugin_1.Plugin(compilation, this.options, reporter);
  71. if (typeof compilation.outputOptions.chunkLoading === "string" &&
  72. ["require", "async-node"].includes(compilation.outputOptions.chunkLoading)) {
  73. reporter.warnOnce("This plugin is not useful for non-web targets.");
  74. return;
  75. }
  76. compilation.hooks.beforeRuntimeRequirements.tap(thisPluginName, () => {
  77. plugin.beforeRuntimeRequirements();
  78. });
  79. compilation.hooks.processAssets.tap({
  80. name: thisPluginName,
  81. stage: compilation.compiler.webpack.Compilation
  82. .PROCESS_ASSETS_STAGE_OPTIMIZE_INLINE,
  83. }, (records) => {
  84. return plugin.processAssets(records);
  85. });
  86. compilation.hooks.afterProcessAssets.tap(thisPluginName, (records) => {
  87. for (const chunk of compilation.chunks.values()) {
  88. for (const chunkFile of chunk.files) {
  89. if (chunkFile in records &&
  90. records[chunkFile].source().includes(util_1.placeholderPrefix)) {
  91. reporter.errorOnce(`Asset ${chunkFile} contains unresolved integrity placeholders`);
  92. }
  93. }
  94. }
  95. });
  96. compilation.compiler.webpack.optimize.RealContentHashPlugin.getCompilationHooks(compilation).updateHash.tap(thisPluginName, (input, oldHash) => {
  97. // FIXME: remove type hack pending https://github.com/webpack/webpack/pull/12642#issuecomment-784744910
  98. return plugin.updateHash(input, oldHash);
  99. });
  100. if (getHtmlWebpackPluginHooks) {
  101. getHtmlWebpackPluginHooks(compilation).beforeAssetTagGeneration.tapPromise(thisPluginName, async (pluginArgs) => {
  102. plugin.handleHwpPluginArgs(pluginArgs);
  103. return pluginArgs;
  104. });
  105. getHtmlWebpackPluginHooks(compilation).alterAssetTagGroups.tapPromise({
  106. name: thisPluginName,
  107. stage: 10000,
  108. }, async (data) => {
  109. plugin.handleHwpBodyTags(data);
  110. return data;
  111. });
  112. }
  113. const { mainTemplate } = compilation;
  114. mainTemplate.hooks.jsonpScript.tap(thisPluginName, (source) => plugin.addAttribute("script", source));
  115. mainTemplate.hooks.linkPreload.tap(thisPluginName, (source) => plugin.addAttribute("link", source));
  116. mainTemplate.hooks.localVars.tap(thisPluginName, (source, chunk) => {
  117. const allChunks = this.options.hashLoading === "lazy"
  118. ? plugin.getChildChunksToAddToChunkManifest(chunk)
  119. : util_1.findChunks(chunk);
  120. const includedChunks = chunk.getChunkMaps(false).hash;
  121. if (Object.keys(includedChunks).length > 0) {
  122. return compilation.compiler.webpack.Template.asString([
  123. source,
  124. `${util_1.sriHashVariableReference} = ` +
  125. JSON.stringify(util_1.generateSriHashPlaceholders(Array.from(allChunks).filter((depChunk) => depChunk.id !== null &&
  126. includedChunks[depChunk.id.toString()]), this.options.hashFuncNames)) +
  127. ";",
  128. ]);
  129. }
  130. return source;
  131. });
  132. if (this.options.hashLoading === "lazy") {
  133. compilation.hooks.additionalChunkRuntimeRequirements.tap(thisPluginName, (chunk) => {
  134. var _a;
  135. const childChunks = plugin.getChildChunksToAddToChunkManifest(chunk);
  136. if (childChunks.size > 0 && !chunk.hasRuntime()) {
  137. compilation.addRuntimeModule(chunk, new AddLazySriRuntimeModule(util_1.generateSriHashPlaceholders(childChunks, this.options.hashFuncNames), (_a = chunk.name) !== null && _a !== void 0 ? _a : chunk.id));
  138. }
  139. });
  140. }
  141. };
  142. /**
  143. * @internal
  144. */
  145. this.validateOptions = (compilation, reporter) => {
  146. if (this.isEnabled(compilation) &&
  147. !compilation.compiler.options.output.crossOriginLoading) {
  148. reporter.warnOnce('SRI requires a cross-origin policy, defaulting to "anonymous". ' +
  149. "Set webpack option output.crossOriginLoading to a value other than false " +
  150. "to make this warning go away. " +
  151. "See https://w3c.github.io/webappsec-subresource-integrity/#cross-origin-data-leakage");
  152. }
  153. return (this.validateHashFuncNames(reporter) && this.validateHashLoading(reporter));
  154. };
  155. /**
  156. * @internal
  157. */
  158. this.validateHashFuncNames = (reporter) => {
  159. if (!Array.isArray(this.options.hashFuncNames)) {
  160. reporter.error("options.hashFuncNames must be an array of hash function names, " +
  161. "instead got '" +
  162. this.options.hashFuncNames +
  163. "'.");
  164. return false;
  165. }
  166. else if (this.options.hashFuncNames.length === 0) {
  167. reporter.error("Must specify at least one hash function name.");
  168. return false;
  169. }
  170. else if (!this.options.hashFuncNames.every(this.validateHashFuncName.bind(this, reporter))) {
  171. return false;
  172. }
  173. else {
  174. this.warnStandardHashFunc(reporter);
  175. return true;
  176. }
  177. };
  178. /**
  179. * @internal
  180. */
  181. this.validateHashLoading = (reporter) => {
  182. const supportedHashLoadingOptions = Object.freeze(["eager", "lazy"]);
  183. if (supportedHashLoadingOptions.includes(this.options.hashLoading)) {
  184. return true;
  185. }
  186. const optionsStr = supportedHashLoadingOptions
  187. .map((opt) => `'${opt}'`)
  188. .join(", ");
  189. reporter.error(`options.hashLoading must be one of ${optionsStr}, instead got '${this.options.hashLoading}'`);
  190. return false;
  191. };
  192. /**
  193. * @internal
  194. */
  195. this.warnStandardHashFunc = (reporter) => {
  196. let foundStandardHashFunc = false;
  197. for (let i = 0; i < this.options.hashFuncNames.length; i += 1) {
  198. if (standardHashFuncNames.indexOf(this.options.hashFuncNames[i]) >= 0) {
  199. foundStandardHashFunc = true;
  200. }
  201. }
  202. if (!foundStandardHashFunc) {
  203. reporter.warnOnce("It is recommended that at least one hash function is part of the set " +
  204. "for which support is mandated by the specification. " +
  205. "These are: " +
  206. standardHashFuncNames.join(", ") +
  207. ". " +
  208. "See http://www.w3.org/TR/SRI/#cryptographic-hash-functions for more information.");
  209. }
  210. };
  211. /**
  212. * @internal
  213. */
  214. this.validateHashFuncName = (reporter, hashFuncName) => {
  215. if (typeof hashFuncName !== "string" &&
  216. !(hashFuncName instanceof String)) {
  217. reporter.error("options.hashFuncNames must be an array of hash function names, " +
  218. "but contained " +
  219. hashFuncName +
  220. ".");
  221. return false;
  222. }
  223. try {
  224. crypto_1.createHash(hashFuncName);
  225. }
  226. catch (error) {
  227. reporter.error("Cannot use hash function '" + hashFuncName + "': " + error.message);
  228. return false;
  229. }
  230. return true;
  231. };
  232. if (typeof options !== "object") {
  233. throw new Error("webpack-subresource-integrity: argument must be an object");
  234. }
  235. this.options = {
  236. hashFuncNames: ["sha384"],
  237. enabled: "auto",
  238. hashLoading: "eager",
  239. ...options,
  240. };
  241. }
  242. /**
  243. * @internal
  244. */
  245. isEnabled(compilation) {
  246. if (this.options.enabled === "auto") {
  247. return compilation.options.mode !== "development";
  248. }
  249. return this.options.enabled;
  250. }
  251. apply(compiler) {
  252. compiler.hooks.beforeCompile.tapPromise(thisPluginName, async () => {
  253. try {
  254. getHtmlWebpackPluginHooks = (await Promise.resolve().then(() => __importStar(require("html-webpack-plugin"))))
  255. .default.getHooks;
  256. }
  257. catch (e) {
  258. if (e.code !== "MODULE_NOT_FOUND") {
  259. throw e;
  260. }
  261. }
  262. });
  263. compiler.hooks.afterPlugins.tap(thisPluginName, (compiler) => {
  264. compiler.hooks.thisCompilation.tap({
  265. name: thisPluginName,
  266. stage: -10000,
  267. }, (compilation) => {
  268. this.setup(compilation);
  269. });
  270. compiler.hooks.compilation.tap(thisPluginName, (compilation) => {
  271. compilation.hooks.statsFactory.tap(thisPluginName, (statsFactory) => {
  272. statsFactory.hooks.extract
  273. .for("asset")
  274. .tap(thisPluginName, (object, asset) => {
  275. var _a;
  276. const contenthash = (_a = asset.info) === null || _a === void 0 ? void 0 : _a.contenthash;
  277. if (contenthash) {
  278. const shaHashes = (Array.isArray(contenthash) ? contenthash : [contenthash]).filter((hash) => String(hash).match(/^sha[0-9]+-/));
  279. if (shaHashes.length > 0) {
  280. object.integrity =
  281. shaHashes.join(" ");
  282. }
  283. }
  284. });
  285. });
  286. });
  287. });
  288. }
  289. }
  290. exports.SubresourceIntegrityPlugin = SubresourceIntegrityPlugin;
  291. //# sourceMappingURL=index.js.map