Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Configure Mozilla's Eslint Plugin eslint-plugin-no-unsanitized #415

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
/*
Recommendation: If you've reached this point, before bringing a sanitiser/encoding library into
your project, try to refactor your code to simply avoid using unsafe browser APIs.
Use:
* Element.textContent
* Element.appendChild
* Document.createElement
* etc.
instead of:
* Element.innerHTML
* Element.outerHTML
* Element.insertAdjacentHTML
* Range.createContextualFragment
* etc.
https://cheatsheetseries.owasp.org/cheatsheets/DOM_based_XSS_Prevention_Cheat_Sheet.html#guideline-3-use-documentcreateelement-elementsetattributevalue-elementappendchild-and-similar-to-build-dynamic-interfaces

There is a difference between encoding and sanitizing.
Depending on the context, a different one might be needed.

Sanitizers will not fully protect you when reflecting user input into the DOM.
They will mitigate XSS, but will not protect against Content Spoofing, DOM Clobbering, etc.
*/
const ENCODE_SANITIZE_METHODS = [
// define project specific encoding/sanitization methods when using unsafe APIs to mitigate XSS
// example below for https://github.com/cure53/DOMPurify
// 'DOMPurify.sanitize',
];

module.exports = {
root: true,
extends: 'airbnb-base',
Expand All @@ -10,9 +38,19 @@ module.exports = {
sourceType: 'module',
requireConfigFile: false,
},
plugins: ['no-unsanitized'],
rules: {
'import/extensions': ['error', { js: 'always' }], // require js file extensions in imports
'linebreak-style': ['error', 'unix'], // enforce unix linebreaks
'no-param-reassign': [2, { props: false }], // allow modifying properties of param
// Flag usage of unsafe DOM APIs to mitigate XSS
'no-unsanitized/method': [
'error',
{ escape: { methods: [...ENCODE_SANITIZE_METHODS] } },
],
'no-unsanitized/property': [
'error',
{ escape: { methods: [...ENCODE_SANITIZE_METHODS] } },
],
},
};
2 changes: 2 additions & 0 deletions blocks/fragment/fragment.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export async function loadFragment(path) {
const resp = await fetch(`${path}.plain.html`);
if (resp.ok) {
const main = document.createElement('main');

// eslint-disable-next-line no-unsanitized/property
main.innerHTML = await resp.text();

// reset base path for media to fragment base
Expand Down
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"eslint": "8.57.0",
"eslint-config-airbnb-base": "15.0.0",
"eslint-plugin-import": "2.29.1",
"eslint-plugin-no-unsanitized": "^4.1.0",
"stylelint": "16.8.2",
"stylelint-config-standard": "36.0.1"
}
Expand Down
3 changes: 2 additions & 1 deletion scripts/aem.js
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ function buildBlock(blockName, content) {
vals.forEach((val) => {
if (val) {
if (typeof val === 'string') {
colEl.innerHTML += val;
colEl.textContent += val;
} else {
colEl.appendChild(val);
}
Expand All @@ -564,6 +564,7 @@ async function loadBlock(block) {
const decorationComplete = new Promise((resolve) => {
(async () => {
try {
// eslint-disable-next-line no-unsanitized/method
const mod = await import(
`${window.hlx.codeBasePath}/blocks/${blockName}/${blockName}.js`
);
Expand Down