From dad9161dc2f8467b247b65e472ce2965f7e71408 Mon Sep 17 00:00:00 2001 From: Jeff Posnick Date: Mon, 13 Jan 2020 11:14:50 -0500 Subject: [PATCH] Switch to allow/denylist --- .../src/lib/populate-sw-template.js | 8 ++-- .../lib/write-sw-using-default-template.js | 8 ++-- .../src/options/partials/generate.js | 8 +++- .../src/templates/sw-template.js | 6 +-- .../workbox-routing/src/NavigationRoute.ts | 44 +++++++++---------- test/workbox-build/node/generate-sw.js | 39 ++++++++++++---- test/workbox-build/node/get-manifest.js | 2 +- test/workbox-build/node/inject-manifest.js | 2 +- .../node/lib/populate-sw-template.js | 20 ++++----- .../sw/test-NavigationRoute.mjs | 18 ++++---- .../node/generate-sw.js | 10 ++--- .../node/inject-manifest.js | 10 ++--- 12 files changed, 101 insertions(+), 74 deletions(-) diff --git a/packages/workbox-build/src/lib/populate-sw-template.js b/packages/workbox-build/src/lib/populate-sw-template.js index ac1a3fb94..e57eae4bc 100644 --- a/packages/workbox-build/src/lib/populate-sw-template.js +++ b/packages/workbox-build/src/lib/populate-sw-template.js @@ -24,8 +24,8 @@ module.exports = ({ importScripts, manifestEntries = [], navigateFallback, - navigateFallbackBlacklist, - navigateFallbackWhitelist, + navigateFallbackDenylist, + navigateFallbackAllowlist, navigationPreload, offlineGoogleAnalytics, runtimeCaching = [], @@ -78,8 +78,8 @@ module.exports = ({ importScripts, manifestEntries, navigateFallback, - navigateFallbackBlacklist, - navigateFallbackWhitelist, + navigateFallbackDenylist, + navigateFallbackAllowlist, navigationPreload, offlineAnalyticsConfigString, precacheOptionsString, diff --git a/packages/workbox-build/src/lib/write-sw-using-default-template.js b/packages/workbox-build/src/lib/write-sw-using-default-template.js index cb7c20044..31570b50f 100644 --- a/packages/workbox-build/src/lib/write-sw-using-default-template.js +++ b/packages/workbox-build/src/lib/write-sw-using-default-template.js @@ -26,8 +26,8 @@ module.exports = async ({ manifestEntries, mode, navigateFallback, - navigateFallbackBlacklist, - navigateFallbackWhitelist, + navigateFallbackDenylist, + navigateFallbackAllowlist, navigationPreload, offlineGoogleAnalytics, runtimeCaching, @@ -53,8 +53,8 @@ module.exports = async ({ importScripts, manifestEntries, navigateFallback, - navigateFallbackBlacklist, - navigateFallbackWhitelist, + navigateFallbackDenylist, + navigateFallbackAllowlist, navigationPreload, offlineGoogleAnalytics, runtimeCaching, diff --git a/packages/workbox-build/src/options/partials/generate.js b/packages/workbox-build/src/options/partials/generate.js index 910725919..f1df2e18c 100644 --- a/packages/workbox-build/src/options/partials/generate.js +++ b/packages/workbox-build/src/options/partials/generate.js @@ -23,8 +23,12 @@ module.exports = { importScripts: joi.array().items(joi.string()), inlineWorkboxRuntime: joi.boolean().default(defaults.inlineWorkboxRuntime), navigateFallback: joi.string().default(defaults.navigateFallback), - navigateFallbackBlacklist: joi.array().items(regExpObject), - navigateFallbackWhitelist: joi.array().items(regExpObject), + navigateFallbackAllowlist: joi.array().items(regExpObject), + navigateFallbackBlacklist: joi.forbidden().error(new Error( + 'navigateFallbackBlacklist has been renamed navigateFallbackDenylist.')), + navigateFallbackDenylist: joi.array().items(regExpObject), + navigateFallbackWhitelist: joi.forbidden().error(new Error( + 'navigateFallbackWhitelist has been renamed navigateFallbackAllowlist.')), navigationPreload: joi.boolean().default(defaults.navigationPreload), offlineGoogleAnalytics: joi.alternatives().try(joi.boolean(), joi.object()) .default(defaults.offlineGoogleAnalytics), diff --git a/packages/workbox-build/src/templates/sw-template.js b/packages/workbox-build/src/templates/sw-template.js index 3fbf0060f..3fcf9bdf5 100644 --- a/packages/workbox-build/src/templates/sw-template.js +++ b/packages/workbox-build/src/templates/sw-template.js @@ -47,9 +47,9 @@ self.addEventListener('message', (event) => { */ <%= use('workbox-precaching', 'precacheAndRoute') %>(<%= JSON.stringify(manifestEntries, null, 2) %>, <%= precacheOptionsString %>); <% if (cleanupOutdatedCaches) { %><%= use('workbox-precaching', 'cleanupOutdatedCaches') %>();<% } %> -<% if (navigateFallback) { %><%= use('workbox-routing', 'registerRoute') %>(new <%= use('workbox-routing', 'NavigationRoute') %>(<%= use('workbox-precaching', 'createHandlerBoundToURL') %>(<%= JSON.stringify(navigateFallback) %>)<% if (navigateFallbackWhitelist || navigateFallbackBlacklist) { %>, { - <% if (navigateFallbackWhitelist) { %>whitelist: [<%= navigateFallbackWhitelist %>],<% } %> - <% if (navigateFallbackBlacklist) { %>blacklist: [<%= navigateFallbackBlacklist %>],<% } %> +<% if (navigateFallback) { %><%= use('workbox-routing', 'registerRoute') %>(new <%= use('workbox-routing', 'NavigationRoute') %>(<%= use('workbox-precaching', 'createHandlerBoundToURL') %>(<%= JSON.stringify(navigateFallback) %>)<% if (navigateFallbackAllowlist || navigateFallbackDenylist) { %>, { + <% if (navigateFallbackAllowlist) { %>allowlist: [<%= navigateFallbackAllowlist %>],<% } %> + <% if (navigateFallbackDenylist) { %>denylist: [<%= navigateFallbackDenylist %>],<% } %> }<% } %>));<% } %> <% } %> diff --git a/packages/workbox-routing/src/NavigationRoute.ts b/packages/workbox-routing/src/NavigationRoute.ts index 2701b00a7..0c90efa14 100644 --- a/packages/workbox-routing/src/NavigationRoute.ts +++ b/packages/workbox-routing/src/NavigationRoute.ts @@ -13,8 +13,8 @@ import {Handler, MatchCallbackOptions} from './_types.js'; import './_version.js'; export interface NavigationRouteMatchOptions { - whitelist?: RegExp[], - blacklist?: RegExp[], + allowlist?: RegExp[], + denylist?: RegExp[], } /** @@ -27,20 +27,20 @@ export interface NavigationRouteMatchOptions { * is set to `navigate`. * * You can optionally only apply this route to a subset of navigation requests - * by using one or both of the `blacklist` and `whitelist` parameters. + * by using one or both of the `denylist` and `allowlist` parameters. * * @memberof workbox.routing * @extends workbox.routing.Route */ class NavigationRoute extends Route { - private _whitelist: RegExp[]; - private _blacklist: RegExp[]; + private _allowlist: RegExp[]; + private _denylist: RegExp[]; /** - * If both `blacklist` and `whiltelist` are provided, the `blacklist` will + * If both `denylist` and `allowlist` are provided, the `denylist` will * take precedence and the request will not match this route. * - * The regular expressions in `whitelist` and `blacklist` + * The regular expressions in `allowlist` and `denylist` * are matched against the concatenated * [`pathname`]{@link https://developer.mozilla.org/en-US/docs/Web/API/HTMLHyperlinkElementUtils/pathname} * and [`search`]{@link https://developer.mozilla.org/en-US/docs/Web/API/HTMLHyperlinkElementUtils/search} @@ -49,33 +49,33 @@ class NavigationRoute extends Route { * @param {workbox.routing.Route~handlerCallback} handler A callback * function that returns a Promise resulting in a Response. * @param {Object} options - * @param {Array} [options.blacklist] If any of these patterns match, - * the route will not handle the request (even if a whitelist RegExp matches). - * @param {Array} [options.whitelist=[/./]] If any of these patterns + * @param {Array} [options.denylist] If any of these patterns match, + * the route will not handle the request (even if a allowlist RegExp matches). + * @param {Array} [options.allowlist=[/./]] If any of these patterns * match the URL's pathname and search parameter, the route will handle the - * request (assuming the blacklist doesn't match). + * request (assuming the denylist doesn't match). */ constructor(handler: Handler, - {whitelist = [/./], blacklist = []}: NavigationRouteMatchOptions = {}) { + {allowlist = [/./], denylist = []}: NavigationRouteMatchOptions = {}) { if (process.env.NODE_ENV !== 'production') { - assert!.isArrayOfClass(whitelist, RegExp, { + assert!.isArrayOfClass(allowlist, RegExp, { moduleName: 'workbox-routing', className: 'NavigationRoute', funcName: 'constructor', - paramName: 'options.whitelist', + paramName: 'options.allowlist', }); - assert!.isArrayOfClass(blacklist, RegExp, { + assert!.isArrayOfClass(denylist, RegExp, { moduleName: 'workbox-routing', className: 'NavigationRoute', funcName: 'constructor', - paramName: 'options.blacklist', + paramName: 'options.denylist', }); } super((options: MatchCallbackOptions) => this._match(options), handler); - this._whitelist = whitelist; - this._blacklist = blacklist; + this._allowlist = allowlist; + this._denylist = denylist; } /** @@ -95,18 +95,18 @@ class NavigationRoute extends Route { const pathnameAndSearch = url.pathname + url.search; - for (const regExp of this._blacklist) { + for (const regExp of this._denylist) { if (regExp.test(pathnameAndSearch)) { if (process.env.NODE_ENV !== 'production') { logger.log(`The navigation route ${pathnameAndSearch} is not ` + - `being used, since the URL matches this blacklist pattern: ` + + `being used, since the URL matches this denylist pattern: ` + `${regExp}`); } return false; } } - if (this._whitelist.some((regExp) => regExp.test(pathnameAndSearch))) { + if (this._allowlist.some((regExp) => regExp.test(pathnameAndSearch))) { if (process.env.NODE_ENV !== 'production') { logger.debug(`The navigation route ${pathnameAndSearch} ` + `is being used.`); @@ -117,7 +117,7 @@ class NavigationRoute extends Route { if (process.env.NODE_ENV !== 'production') { logger.log(`The navigation route ${pathnameAndSearch} is not ` + `being used, since the URL being navigated to doesn't ` + - `match the whitelist.`); + `match the allowlist.`); } return false; } diff --git a/test/workbox-build/node/generate-sw.js b/test/workbox-build/node/generate-sw.js index 1f40330b1..ec7850c49 100644 --- a/test/workbox-build/node/generate-sw.js +++ b/test/workbox-build/node/generate-sw.js @@ -48,8 +48,8 @@ describe(`[workbox-build] generate-sw.js (End to End)`, function() { 'mode', 'modifyURLPrefix', 'navigateFallback', - 'navigateFallbackBlacklist', - 'navigateFallbackWhitelist', + 'navigateFallbackDenylist', + 'navigateFallbackAllowlist', 'navigationPreload', 'offlineGoogleAnalytics', 'runtimeCaching', @@ -395,12 +395,12 @@ describe(`[workbox-build] generate-sw.js (End to End)`, function() { const outputDir = tempy.directory(); const swDest = upath.join(outputDir, 'sw.js'); const navigateFallback = 'test.html'; - const navigateFallbackBlacklist = [/test1/, /test2/]; - const navigateFallbackWhitelist = [/test3/, /test4/]; + const navigateFallbackDenylist = [/test1/, /test2/]; + const navigateFallbackAllowlist = [/test3/, /test4/]; const options = Object.assign({}, BASE_OPTIONS, { navigateFallback, - navigateFallbackBlacklist, - navigateFallbackWhitelist, + navigateFallbackDenylist, + navigateFallbackAllowlist, swDest, }); @@ -435,8 +435,8 @@ describe(`[workbox-build] generate-sw.js (End to End)`, function() { }], {}]], registerRoute: [[{name: 'NavigationRoute'}]], NavigationRoute: [['/urlWithCacheKey', { - blacklist: navigateFallbackBlacklist, - whitelist: navigateFallbackWhitelist, + denylist: navigateFallbackDenylist, + allowlist: navigateFallbackAllowlist, }]], }}); }); @@ -1092,6 +1092,29 @@ describe(`[workbox-build] generate-sw.js (End to End)`, function() { }); describe(`[workbox-build] removed options`, function() { + // These were removed in v5. + const navigateFallbackOptions = { + navigateFallbackBlacklist: [], + navigateFallbackWhitelist: [], + }; + + for (const [option, value] of Object.entries(navigateFallbackOptions)) { + it(`should fail validation when ${option} is used`, async function() { + const options = Object.assign({}, BASE_OPTIONS, { + [option]: value, + }); + + try { + await generateSW(options); + throw new Error('Unexpected success.'); + } catch (error) { + // They fail by throwing an Error with a custom message, + // not a ValidationError. + expect(error.message).to.include(option); + } + }); + } + // These were deprecated in v4, and formally removed in v5. const oldOptionsToValue = { dontCacheBustUrlsMatching: /ignored/, diff --git a/test/workbox-build/node/get-manifest.js b/test/workbox-build/node/get-manifest.js index a5c0ba020..76f6d5a04 100644 --- a/test/workbox-build/node/get-manifest.js +++ b/test/workbox-build/node/get-manifest.js @@ -39,7 +39,7 @@ describe(`[workbox-build] get-manifest.js (End to End)`, function() { 'importWorkboxFrom', 'injectionPointRegexp', 'navigateFallback', - 'navigateFallbackWhitelist', + 'navigateFallbackAllowlist', 'runtimeCaching', 'skipWaiting', 'swSrc', diff --git a/test/workbox-build/node/inject-manifest.js b/test/workbox-build/node/inject-manifest.js index 808ec9d11..c23e03219 100644 --- a/test/workbox-build/node/inject-manifest.js +++ b/test/workbox-build/node/inject-manifest.js @@ -49,7 +49,7 @@ describe(`[workbox-build] inject-manifest.js (End to End)`, function() { 'importScripts', 'importWorkboxFrom', 'navigateFallback', - 'navigateFallbackWhitelist', + 'navigateFallbackAllowlist', 'runtimeCaching', 'skipWaiting', ]; diff --git a/test/workbox-build/node/lib/populate-sw-template.js b/test/workbox-build/node/lib/populate-sw-template.js index 55e238573..9a1880840 100644 --- a/test/workbox-build/node/lib/populate-sw-template.js +++ b/test/workbox-build/node/lib/populate-sw-template.js @@ -75,8 +75,8 @@ describe(`[workbox-build] lib/populate-sw-template.js`, function() { disableDevLogs: undefined, importScripts: undefined, navigateFallback: undefined, - navigateFallbackBlacklist: undefined, - navigateFallbackWhitelist: undefined, + navigateFallbackDenylist: undefined, + navigateFallbackAllowlist: undefined, navigationPreload: undefined, offlineAnalyticsConfigString: undefined, precacheOptionsString, @@ -96,8 +96,8 @@ describe(`[workbox-build] lib/populate-sw-template.js`, function() { const importScripts = ['test.js']; const manifestEntries = [{url: '/path/to/index.html', revision: '1234'}]; const navigateFallback = '/shell.html'; - const navigateFallbackBlacklist = [/another-test/]; - const navigateFallbackWhitelist = [/test/]; + const navigateFallbackDenylist = [/another-test/]; + const navigateFallbackAllowlist = [/test/]; const navigationPreload = true; const offlineGoogleAnalytics = true; const offlineAnalyticsConfigString = '{}'; @@ -130,8 +130,8 @@ describe(`[workbox-build] lib/populate-sw-template.js`, function() { importScripts, manifestEntries, navigateFallback, - navigateFallbackBlacklist, - navigateFallbackWhitelist, + navigateFallbackDenylist, + navigateFallbackAllowlist, navigationPreload, offlineGoogleAnalytics, runtimeCaching, @@ -152,8 +152,8 @@ describe(`[workbox-build] lib/populate-sw-template.js`, function() { importScripts, manifestEntries, navigateFallback, - navigateFallbackBlacklist, - navigateFallbackWhitelist, + navigateFallbackDenylist, + navigateFallbackAllowlist, navigationPreload, offlineAnalyticsConfigString, runtimeCaching: runtimeCachingPlaceholder, @@ -202,8 +202,8 @@ describe(`[workbox-build] lib/populate-sw-template.js`, function() { disableDevLogs: undefined, importScripts: undefined, navigateFallback: undefined, - navigateFallbackBlacklist: undefined, - navigateFallbackWhitelist: undefined, + navigateFallbackDenylist: undefined, + navigateFallbackAllowlist: undefined, navigationPreload: undefined, offlineAnalyticsConfigString, precacheOptionsString, diff --git a/test/workbox-routing/sw/test-NavigationRoute.mjs b/test/workbox-routing/sw/test-NavigationRoute.mjs index d009c3909..977147560 100644 --- a/test/workbox-routing/sw/test-NavigationRoute.mjs +++ b/test/workbox-routing/sw/test-NavigationRoute.mjs @@ -82,37 +82,37 @@ describe(`NavigationRoute`, function() { }); }); - it(`should not include urls in blacklist that completely match`, function() { + it(`should not include urls in denylist that completely match`, function() { const url = new URL('/testing/path.html', self.location); const request = new Request(url); Object.defineProperty(request, 'mode', {value: 'navigate'}); const navigationRoute = new NavigationRoute(handler, { - blacklist: [/\/testing\/.*/], + denylist: [/\/testing\/.*/], }); expect(navigationRoute.match({request, url})).to.equal(false); }); - it(`should blacklist urls with search params that result in partial match with regex`, function() { + it(`should denylist urls with search params that result in partial match with regex`, function() { const url = new URL('/testing/path.html?test=hello', self.location); const request = new Request(url); Object.defineProperty(request, 'mode', {value: 'navigate'}); const navigationRoute = new NavigationRoute(handler, { - blacklist: [/\/testing\/path.html/], + denylist: [/\/testing\/path.html/], }); expect(navigationRoute.match({request, url})).to.equal(false); }); - it(`should only match urls in custom whitelist`, function() { + it(`should only match urls in custom allowlist`, function() { let url = new URL('/testing/path.html?test=hello', self.location); let request = new Request(url); Object.defineProperty(request, 'mode', {value: 'navigate'}); const navigationRoute = new NavigationRoute(handler, { - whitelist: [/\/testing\/path.html/], + allowlist: [/\/testing\/path.html/], }); expect(navigationRoute.match({request, url})).to.equal(true); @@ -124,14 +124,14 @@ describe(`NavigationRoute`, function() { expect(navigationRoute.match({request, url})).to.equal(false); }); - it(`should take blacklist as priority`, function() { + it(`should take denylist as priority`, function() { let url = new URL('/testing/path.html?test=hello', self.location); let request = new Request(url); Object.defineProperty(request, 'mode', {value: 'navigate'}); const navigationRoute = new NavigationRoute(handler, { - whitelist: [/\/testing\/.*/], - blacklist: [/\/testing\/path.html/], + allowlist: [/\/testing\/.*/], + denylist: [/\/testing\/path.html/], }); expect(navigationRoute.match({request, url})).to.equal(false); diff --git a/test/workbox-webpack-plugin/node/generate-sw.js b/test/workbox-webpack-plugin/node/generate-sw.js index 7fd3ab34d..bc745b5cc 100644 --- a/test/workbox-webpack-plugin/node/generate-sw.js +++ b/test/workbox-webpack-plugin/node/generate-sw.js @@ -227,7 +227,7 @@ describe(`[workbox-webpack-plugin] GenerateSW (End to End)`, function() { }); }); - it(`should honor the 'chunks' whitelist config`, function(done) { + it(`should honor the 'chunks' allowlist config`, function(done) { const outputDir = tempy.directory(); const config = { mode: 'production', @@ -276,7 +276,7 @@ describe(`[workbox-webpack-plugin] GenerateSW (End to End)`, function() { }); }); - it(`should honor the 'chunks' whitelist config, including children created via SplitChunksPlugin`, function(done) { + it(`should honor the 'chunks' allowlist config, including children created via SplitChunksPlugin`, function(done) { const outputDir = tempy.directory(); const config = { mode: 'production', @@ -329,7 +329,7 @@ describe(`[workbox-webpack-plugin] GenerateSW (End to End)`, function() { }); }); - it(`should honor the 'excludeChunks' blacklist config`, function(done) { + it(`should honor the 'excludeChunks' denylist config`, function(done) { const outputDir = tempy.directory(); const config = { mode: 'production', @@ -378,7 +378,7 @@ describe(`[workbox-webpack-plugin] GenerateSW (End to End)`, function() { }); }); - it(`should honor setting both the 'chunks' and 'excludeChunks', with the blacklist taking precedence`, function(done) { + it(`should honor setting both the 'chunks' and 'excludeChunks', with the denylist taking precedence`, function(done) { const outputDir = tempy.directory(); const config = { mode: 'production', @@ -651,7 +651,7 @@ describe(`[workbox-webpack-plugin] GenerateSW (End to End)`, function() { }); }); - it(`should allow developers to whitelist via include`, function(done) { + it(`should allow developers to allowlist via include`, function(done) { const outputDir = tempy.directory(); const config = { mode: 'production', diff --git a/test/workbox-webpack-plugin/node/inject-manifest.js b/test/workbox-webpack-plugin/node/inject-manifest.js index 70ec5c23f..d617a88fe 100644 --- a/test/workbox-webpack-plugin/node/inject-manifest.js +++ b/test/workbox-webpack-plugin/node/inject-manifest.js @@ -113,7 +113,7 @@ describe(`[workbox-webpack-plugin] InjectManifest (End to End)`, function() { }); }); - it(`should honor the 'chunks' whitelist config`, function(done) { + it(`should honor the 'chunks' allowlist config`, function(done) { const outputDir = tempy.directory(); const config = { mode: 'production', @@ -167,7 +167,7 @@ describe(`[workbox-webpack-plugin] InjectManifest (End to End)`, function() { }); }); - it(`should honor the 'chunks' whitelist config, including children created via SplitChunksPlugin`, function(done) { + it(`should honor the 'chunks' allowlist config, including children created via SplitChunksPlugin`, function(done) { const outputDir = tempy.directory(); const config = { mode: 'production', @@ -225,7 +225,7 @@ describe(`[workbox-webpack-plugin] InjectManifest (End to End)`, function() { }); }); - it(`should honor the 'excludeChunks' blacklist config`, function(done) { + it(`should honor the 'excludeChunks' denylist config`, function(done) { const outputDir = tempy.directory(); const config = { mode: 'production', @@ -279,7 +279,7 @@ describe(`[workbox-webpack-plugin] InjectManifest (End to End)`, function() { }); }); - it(`should honor setting both the 'chunks' and 'excludeChunks', with the blacklist taking precedence`, function(done) { + it(`should honor setting both the 'chunks' and 'excludeChunks', with the denylist taking precedence`, function(done) { const outputDir = tempy.directory(); const config = { mode: 'production', @@ -734,7 +734,7 @@ describe(`[workbox-webpack-plugin] InjectManifest (End to End)`, function() { }); }); - it(`should allow developers to whitelist via include`, function(done) { + it(`should allow developers to allowlist via include`, function(done) { const outputDir = tempy.directory(); const config = { mode: 'production',