mattermost-desktop/test/specs/security_test.js

121 lines
3.2 KiB
JavaScript
Raw Normal View History

'use strict';
const path = require('path');
const fs = require('fs');
const http = require('http');
const env = require('../modules/environment');
2016-09-25 07:14:01 -07:00
describe('application', function desc() {
2017-09-22 08:41:42 -07:00
this.timeout(30000);
const serverPort = 8181;
2016-09-25 07:14:01 -07:00
const testURL = `http://localhost:${serverPort}`;
const config = {
version: 1,
teams: [{
name: 'example_1',
url: testURL
}, {
name: 'example_2',
url: testURL
}]
};
2016-09-25 07:14:01 -07:00
before(() => {
this.server = http.createServer((req, res) => {
res.writeHead(200, {
'Content-Type': 'text/html'
});
res.end(fs.readFileSync(path.resolve(env.sourceRootDir, 'test/modules/test.html'), 'utf-8'));
}).listen(serverPort, '127.0.0.1');
});
2016-09-25 07:14:01 -07:00
beforeEach(() => {
fs.writeFileSync(env.configFilePath, JSON.stringify(config));
this.app = env.getSpectronApp();
return this.app.start();
});
2016-09-25 07:14:01 -07:00
afterEach(() => {
if (this.app && this.app.isRunning()) {
2016-09-25 07:14:01 -07:00
return this.app.stop();
}
2016-09-25 07:14:01 -07:00
return true;
});
2016-09-25 07:14:01 -07:00
after((done) => {
this.server.close(done);
2016-09-25 07:14:01 -07:00
});
2016-09-25 07:14:01 -07:00
it('should NOT be able to call Node.js API in webview', () => {
env.addClientCommands(this.app.client);
// webview is handled as a window by chromedriver.
2016-09-25 07:14:01 -07:00
return this.app.client.
2018-02-02 05:45:54 -08:00
windowByIndex(1).isNodeEnabled().then((enabled) => {
enabled.should.be.false;
}).
windowByIndex(2).isNodeEnabled().then((enabled) => {
enabled.should.be.false;
}).
windowByIndex(0).
2016-09-25 07:14:01 -07:00
getAttribute('webview', 'nodeintegration').then((nodeintegration) => {
// nodeintegration is an array of string
nodeintegration.forEach((n) => {
n.should.equal('false');
});
});
});
2016-09-25 07:14:01 -07:00
it('should NOT be able to call Node.js API in a new window', () => {
env.addClientCommands(this.app.client);
2016-06-25 06:09:37 -07:00
const client = this.app.client;
2016-09-25 07:14:01 -07:00
return this.app.client.
windowByIndex(1). // in the first webview
execute(() => {
open_window();
2016-09-25 07:14:01 -07:00
}).
waitUntil(() => {
2016-06-25 06:09:37 -07:00
return client.windowHandles().then((handles) => {
return handles.value.length === 4;
});
2016-09-25 07:14:01 -07:00
}, 5000, 'expected a new window').
2018-02-02 05:45:54 -08:00
windowByIndex(3).isNodeEnabled().then((enabled) => {
enabled.should.be.false;
});
});
2016-09-25 07:14:01 -07:00
it('should NOT be able to call eval() in any window', () => {
env.addClientCommands(this.app.client);
const tryEval = (index) => {
2016-09-25 07:14:01 -07:00
return this.app.client.
windowByIndex(index).
execute(() => {
return eval('1 + 1');
}).then((result) => {
throw new Error(`Promise was unexpectedly fulfilled (result: ${result})`);
2018-02-02 05:45:54 -08:00
}, (error) => {
(error !== null).should.be.true;
});
};
const tryEvalInSettingsPage = () => {
2016-09-25 07:14:01 -07:00
return this.app.client.
windowByIndex(0).
loadSettingsPage().
execute(() => {
return eval('1 + 1');
}).then((result) => {
throw new Error(`Promise was unexpectedly fulfilled (result: ${result})`);
2018-02-02 05:45:54 -08:00
}, (error) => {
(error !== null).should.be.true;
});
};
return Promise.all([
tryEval(0),
tryEvalInSettingsPage()
]);
});
});