-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sanitize data URL in offscreen document (#150)
- Loading branch information
Showing
9 changed files
with
180 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,6 +59,7 @@ export const TEXT_TEXT_URL = 'TextURL'; | |
export const TEXT_URL_ONLY = 'URLOnly'; | ||
export const THEME_DARK = '[email protected]'; | ||
export const THEME_LIGHT = '[email protected]'; | ||
export const URL_SANITIZE = 'sanitizeURL'; | ||
export const USER_INPUT = 'userInput'; | ||
export const USER_INPUT_DEFAULT = 'Edit content text of the link'; | ||
export const WEBEXT_ID = '[email protected]'; |
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,18 @@ | ||
/** | ||
* sanitize.js | ||
*/ | ||
|
||
/* shared */ | ||
import '../lib/purify/purify.min.js'; | ||
import { sanitizeURL } from '../lib/url/url-sanitizer-wo-dompurify.min.js'; | ||
|
||
/** | ||
* sanitize URL | ||
* @param {string} url - URL | ||
* @param {object} opt - options | ||
* @returns {?string} - sanitized URL | ||
*/ | ||
export const sanitize = async (url, opt) => { | ||
const res = await sanitizeURL(url, opt); | ||
return res || null; | ||
}; |
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,41 @@ | ||
/** | ||
* sanitize.js | ||
*/ | ||
|
||
/* shared */ | ||
import { sanitizeURL } from '../lib/url/url-sanitizer-wo-dompurify.min.js'; | ||
import { isString } from '../mjs/common.js'; | ||
import { URL_SANITIZE } from '../mjs/constant.js'; | ||
|
||
/* api */ | ||
const { offscreen, runtime } = chrome; | ||
|
||
/** | ||
* sanitize URL | ||
* @param {string} url - URL | ||
* @param {object} opt - options | ||
* @returns {?string} - sanitized URL | ||
*/ | ||
export const sanitize = async (url, opt) => { | ||
let res; | ||
if (url && isString(url)) { | ||
const { protocol } = new URL(url); | ||
if (protocol === 'data:') { | ||
await offscreen.createDocument({ | ||
justification: 'Sanitize URL', | ||
reasons: [offscreen.Reason.DOM_PARSER], | ||
url: 'html/offscreen.html' | ||
}); | ||
[res] = await runtime.sendMessage({ | ||
[URL_SANITIZE]: [ | ||
url, | ||
opt | ||
] | ||
}); | ||
await offscreen.closeDocument(); | ||
} else { | ||
res = await sanitizeURL(url, opt); | ||
} | ||
} | ||
return res || null; | ||
}; |
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,88 @@ | ||
/** | ||
* sanitize.test.js | ||
*/ | ||
/* eslint-disable import/order */ | ||
|
||
/* api */ | ||
import { assert } from 'chai'; | ||
import { afterEach, beforeEach, describe, it } from 'mocha'; | ||
import { browser, createJsdom } from './mocha/setup.js'; | ||
|
||
/* test */ | ||
import * as mjs from '../src/mjs/sanitize.js'; | ||
|
||
describe('sanitize', () => { | ||
const globalKeys = [ | ||
'Blob', | ||
'ClipboardItem', | ||
'DOMParser', | ||
'DOMPurify', | ||
'HTMLUnknownElement', | ||
'Node', | ||
'XMLSerializer' | ||
]; | ||
let window, document; | ||
beforeEach(() => { | ||
const dom = createJsdom(); | ||
window = dom && dom.window; | ||
document = window.document; | ||
browser._sandbox.reset(); | ||
browser.i18n.getMessage.callsFake((...args) => args.toString()); | ||
browser.permissions.contains.resolves(true); | ||
browser.storage.local.get.resolves({}); | ||
global.browser = browser; | ||
global.window = window; | ||
global.document = document; | ||
for (const key of globalKeys) { | ||
global[key] = window[key]; | ||
} | ||
}); | ||
afterEach(() => { | ||
window = null; | ||
document = null; | ||
delete global.browser; | ||
delete global.window; | ||
delete global.document; | ||
for (const key of globalKeys) { | ||
delete global[key]; | ||
} | ||
browser._sandbox.reset(); | ||
}); | ||
|
||
describe('sanitize URL', () => { | ||
const func = mjs.sanitize; | ||
|
||
it('should get null', async () => { | ||
const res = await func(); | ||
assert.isNull(res, 'result'); | ||
}); | ||
|
||
it('should get null', async () => { | ||
const res = await func('foo'); | ||
assert.isNull(res, 'result'); | ||
}); | ||
|
||
it('should get result', async () => { | ||
const res = await func('https://example.com/"onclick="alert(1)"', { | ||
remove: true | ||
}); | ||
assert.strictEqual(res, 'https://example.com/', 'result'); | ||
}); | ||
|
||
it('should get result', async () => { | ||
const res = await func('https://example.com/"onclick="alert(1)"', { | ||
remove: true | ||
}); | ||
assert.strictEqual(res, 'https://example.com/', 'result'); | ||
}); | ||
|
||
it('should get result', async () => { | ||
const res = | ||
await func('data:,https://example.com/#<script>alert(1);</script>', { | ||
allow: ['data'], | ||
remove: true | ||
}); | ||
assert.strictEqual(res, 'data:,https://example.com/', 'result'); | ||
}); | ||
}); | ||
}); |