From a972fe57e448d6fef7bf37a31c6467330a7612d3 Mon Sep 17 00:00:00 2001 From: Oleksandr Krasko <0m3r.mail@gmail.com> Date: Wed, 1 Sep 2021 23:28:10 +0300 Subject: [PATCH] Added possibility to allow trusted vendors to change code outside their namespace (#3156) (#3266) * Added possibility to allow trusted vendors to change code outside their namespace (#3156) * Read trusted vendors from root project package.json * Checking if package.json exists (Wrong context in some tests) * Rename trusted_vendors to trusted-vendors * use process.cwd instead of hard context * Run prettier Co-authored-by: Lars Roettig Co-authored-by: Tommy Wiebell --- .../lib/WebpackTools/ModuleTransformConfig.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/packages/pwa-buildpack/lib/WebpackTools/ModuleTransformConfig.js b/packages/pwa-buildpack/lib/WebpackTools/ModuleTransformConfig.js index 84b947013c..f56e6ce423 100644 --- a/packages/pwa-buildpack/lib/WebpackTools/ModuleTransformConfig.js +++ b/packages/pwa-buildpack/lib/WebpackTools/ModuleTransformConfig.js @@ -1,4 +1,5 @@ const path = require('path'); +const fs = require('fs'); const buildpackName = require('../../package.json').name; /** @@ -154,6 +155,7 @@ class ModuleTransformConfig { if ( !this._isLocal(requestor) && // Local project can modify anything !this._isBuiltin(requestor) && // Buildpack itself can modify anything + !this._isTrustedExtensionVendor(requestor) && // Trusted extension vendors can modify anything !fileToTransform.startsWith(requestor) ) { throw this._traceableError( @@ -167,6 +169,22 @@ class ModuleTransformConfig { _isLocal(requestor) { return requestor === this._localProjectName; } + _isTrustedExtensionVendor(requestor) { + const vendors = this._getTrustedExtensionVendors(); + const requestorVendor = requestor.split('/')[0]; + return requestorVendor.length > 0 && vendors.includes(requestorVendor); + } + _getTrustedExtensionVendors() { + const configPath = path.resolve(process.cwd(), 'package.json'); + if (!fs.existsSync(configPath)) { + return []; + } + const config = require(configPath)['pwa-studio']; + const configSectionName = 'trusted-vendors'; + return config && config[configSectionName] + ? config[configSectionName] + : []; + } _traceableError(msg) { const capturedError = new Error(`ModuleTransformConfig: ${msg}`); Error.captureStackTrace(capturedError, ModuleTransformConfig);