-
-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
57e806d
commit d381d19
Showing
395 changed files
with
3,964 additions
and
9,598 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
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
26 changes: 26 additions & 0 deletions
26
packages/browser-extension-manifest-fields/fields/scripts-fields/service_worker.ts
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,26 @@ | ||
import path from 'path' | ||
import {type ManifestData} from '../../types' | ||
|
||
export default function serviceWorker( | ||
manifestPath: string, | ||
manifest: ManifestData | ||
) { | ||
if (!manifest || !manifest.background) { | ||
return undefined | ||
} | ||
|
||
const serviceWorker = manifest.background.service_worker | ||
|
||
if (serviceWorker) { | ||
const serviceWorker = manifest.background.service_worker | ||
|
||
const serviceWorkerAbsolutePath = path.join( | ||
path.dirname(manifestPath), | ||
serviceWorker | ||
) | ||
|
||
return serviceWorkerAbsolutePath | ||
} | ||
|
||
return undefined | ||
} |
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,70 @@ | ||
import webpack from 'webpack' | ||
import { | ||
handleMultipleAssetsError | ||
// handleCantResolveError | ||
} from './src/compilationErrorHandlers' | ||
import { | ||
handleInsecureCSPValue, | ||
handleWrongWebResourceFormatError | ||
} from './src/browserRuntimeErrorHandlers' | ||
import {CommonErrorsPluginInterface} from './types' | ||
|
||
export default class CommonErrorsPlugin { | ||
public readonly manifestPath: string | ||
|
||
constructor(options: CommonErrorsPluginInterface) { | ||
this.manifestPath = options.manifestPath | ||
} | ||
|
||
private handleCompilations(compilation: webpack.Compilation) { | ||
compilation.errors.forEach((error, index) => { | ||
const multipleAssetsError = handleMultipleAssetsError(error) | ||
// const cantResolveError = handleCantResolveError(error) | ||
|
||
if (multipleAssetsError) { | ||
compilation.errors[index] = multipleAssetsError | ||
} | ||
|
||
// if (cantResolveError) { | ||
// compilation.errors[index] = cantResolveError | ||
// } | ||
}) | ||
} | ||
|
||
private handleBrowserRuntimeErrors(compilation: webpack.Compilation) { | ||
const insecureCSPValueError = handleInsecureCSPValue(this.manifestPath) | ||
const wrongWebResourceFormatError = handleWrongWebResourceFormatError( | ||
this.manifestPath | ||
) | ||
|
||
if (insecureCSPValueError) { | ||
compilation.errors.push(insecureCSPValueError) | ||
} | ||
|
||
if (wrongWebResourceFormatError) { | ||
compilation.errors.push(wrongWebResourceFormatError) | ||
} | ||
} | ||
|
||
apply(compiler: webpack.Compiler) { | ||
compiler.hooks.compilation.tap( | ||
'HandleCommonErrorsPlugin', | ||
(compilation) => { | ||
compilation.hooks.afterSeal.tapAsync( | ||
'HandleCommonErrorsPlugin', | ||
(done) => { | ||
// Handle errors related to compilation such | ||
// as multiple assets with the same name, | ||
// or missing dependencies. | ||
this.handleCompilations(compilation) | ||
|
||
// Handle errors related to the browser | ||
// runtime such as insecure CSP values. | ||
this.handleBrowserRuntimeErrors(compilation) | ||
done() | ||
} | ||
) | ||
} | ||
) | ||
} | ||
} |
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,56 @@ | ||
{ | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/cezaraugusto/webpack-browser-extension-common-errors-plugin.git" | ||
}, | ||
"engines": { | ||
"node": ">=16" | ||
}, | ||
"name": "webpack-browser-extension-common-errors-plugin", | ||
"version": "0.0.0", | ||
"description": "webpack plugin to handle common errors from browser extensions", | ||
"main": "./dist/module.js", | ||
"types": "./dist/module.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"author": { | ||
"name": "Cezar Augusto", | ||
"email": "boss@cezaraugusto.net", | ||
"url": "https://cezaraugusto.com" | ||
}, | ||
"scripts": { | ||
"watch": "yarn compile --watch", | ||
"compile": "tsup-node ./module.ts --format cjs --dts --target=node16 --minify", | ||
"lint": "eslint \"./**/*.ts*\"", | ||
"test": "echo \"Note: no test specified\" && exit 0" | ||
}, | ||
"keywords": [ | ||
"webpack", | ||
"plugin", | ||
"browser", | ||
"web", | ||
"extension", | ||
"web-ext", | ||
"manifest", | ||
"manifest.json" | ||
], | ||
"peerDependencies": { | ||
"webpack": "^5.0.0" | ||
}, | ||
"dependencies": { | ||
"browser-extension-manifest-fields": "*", | ||
"content-security-policy-parser": "^0.4.1" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^7.32.0", | ||
"eslint-config-extension-create": "*", | ||
"jasmine": "^4.0.2", | ||
"rimraf": "^3.0.2", | ||
"webpack": "^5.9.0", | ||
"webpack-cli": "^4.2.0", | ||
"tsconfig": "*", | ||
"tsup": "^7.1.0" | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
packages/common-errors-plugin/src/browserRuntimeErrorHandlers.ts
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,67 @@ | ||
import webpack from 'webpack' | ||
import parseCSP from 'content-security-policy-parser' | ||
|
||
export function handleInsecureCSPValue( | ||
manifestPath: string | ||
): webpack.WebpackError | null { | ||
const manifest = require(manifestPath) | ||
const manifestCSP = manifest.content_security_policy | ||
const extensionPagesCSP = manifest.content_security_policy?.extension_pages | ||
|
||
const checkCSP = (cspString: string) => { | ||
const parsedCSP = parseCSP(cspString) | ||
|
||
if ( | ||
parsedCSP['script-src'] && | ||
parsedCSP['script-src'].includes("'unsafe-eval'") | ||
) { | ||
return `[manifest.json]: Insecure CSP value "'unsafe-eval'" in directive 'script-src'.` | ||
} | ||
} | ||
|
||
if (manifest.manifest_version === 3) { | ||
const mainCSPError = manifestCSP ? checkCSP(manifestCSP) : null | ||
const extensionPagesCSPError = extensionPagesCSP | ||
? checkCSP(extensionPagesCSP) | ||
: null | ||
|
||
if (mainCSPError) return new webpack.WebpackError(mainCSPError) | ||
if (extensionPagesCSPError) | ||
return new webpack.WebpackError(extensionPagesCSPError) | ||
} | ||
|
||
return null | ||
} | ||
|
||
export function handleWrongWebResourceFormatError( | ||
manifestPath: string | ||
): webpack.WebpackError | null { | ||
const manifest = require(manifestPath) | ||
const webResources = manifest.web_accessible_resources | ||
|
||
if (webResources) { | ||
const mv2Format = webResources.some((resource: string) => { | ||
return typeof resource === 'string' | ||
}) | ||
|
||
const mv3Format = webResources.some((resource: any) => { | ||
return ( | ||
typeof resource === 'object' || resource.resources || resource.matches | ||
) | ||
}) | ||
|
||
if (manifest.manifest_version === 2 && !mv2Format) { | ||
return new webpack.WebpackError( | ||
`[manifest.json]: web_accessible_resources must be a string array in Manifest V2` | ||
) | ||
} | ||
|
||
if (manifest.manifest_version === 3 && !mv3Format) { | ||
return new webpack.WebpackError( | ||
`[manifest.json]: web_accessible_resources must be an array of objects in Manifest V3` | ||
) | ||
} | ||
} | ||
|
||
return null | ||
} |
Oops, something went wrong.