fix: allow accessing file:// when web security is disabled (#28489)

* fix: allow accessing file:// when web security is disabled

* test: fix webview tests on web security

* chore: remove unused attributes

* chore: cleanup RegisterURLLoaderFactories method
This commit is contained in:
Cheng Zhao 2021-04-07 10:46:23 +09:00 committed by GitHub
parent fe0da255b6
commit e454bded3c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 100 additions and 54 deletions

View file

@ -33,6 +33,28 @@ describe('<webview> tag', function () {
return event.message;
};
async function loadFileInWebView (webview, attributes = {}) {
const thisFile = url.format({
pathname: __filename.replace(/\\/g, '/'),
protocol: 'file',
slashes: true
});
const src = `<script>
function loadFile() {
return new Promise((resolve) => {
fetch('${thisFile}').then(
() => resolve('loaded'),
() => resolve('failed')
)
});
}
console.log('ok');
</script>`;
attributes.src = `data:text/html;base64,${btoa(unescape(encodeURIComponent(src)))}`;
await startLoadingWebViewAndWaitForMessage(webview, attributes);
return await webview.executeJavaScript('loadFile()');
}
beforeEach(() => {
webview = new WebView();
});
@ -321,27 +343,13 @@ describe('<webview> tag', function () {
describe('disablewebsecurity attribute', () => {
it('does not disable web security when not set', async () => {
const jqueryPath = path.join(__dirname, '/static/jquery-2.0.3.min.js');
const src = `<script src='file://${jqueryPath}'></script> <script>console.log('ok');</script>`;
const encoded = btoa(unescape(encodeURIComponent(src)));
const message = await startLoadingWebViewAndWaitForMessage(webview, {
src: `data:text/html;base64,${encoded}`
});
expect(message).to.be.a('string');
expect(message).to.contain('Not allowed to load local resource');
const result = await loadFileInWebView(webview);
expect(result).to.equal('failed');
});
it('disables web security when set', async () => {
const jqueryPath = path.join(__dirname, '/static/jquery-2.0.3.min.js');
const src = `<script src='file://${jqueryPath}'></script> <script>console.log('ok');</script>`;
const encoded = btoa(unescape(encodeURIComponent(src)));
const message = await startLoadingWebViewAndWaitForMessage(webview, {
disablewebsecurity: '',
src: `data:text/html;base64,${encoded}`
});
expect(message).to.equal('ok');
const result = await loadFileInWebView(webview, { disablewebsecurity: '' });
expect(result).to.equal('loaded');
});
it('does not break node integration', async () => {
@ -483,16 +491,10 @@ describe('<webview> tag', function () {
});
it('can disables web security and enable nodeintegration', async () => {
const jqueryPath = path.join(__dirname, '/static/jquery-2.0.3.min.js');
const src = `<script src='file://${jqueryPath}'></script> <script>console.log(typeof require);</script>`;
const encoded = btoa(unescape(encodeURIComponent(src)));
const message = await startLoadingWebViewAndWaitForMessage(webview, {
src: `data:text/html;base64,${encoded}`,
webpreferences: 'webSecurity=no, nodeIntegration=yes, contextIsolation=no'
});
expect(message).to.equal('function');
const result = await loadFileInWebView(webview, { webpreferences: 'webSecurity=no, nodeIntegration=yes, contextIsolation=no' });
expect(result).to.equal('loaded');
const type = await webview.executeJavaScript('typeof require');
expect(type).to.equal('function');
});
});