Skip to content

Commit 42385ec

Browse files
committed
Security upgrades and test fixes
1 parent c87d8e5 commit 42385ec

File tree

6 files changed

+354
-738
lines changed

6 files changed

+354
-738
lines changed

lib/handlers/compile.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ const temp = require('temp'),
3535

3636
temp.track();
3737

38-
let oneTimeInit = false;
38+
let hasSetUpAutoClean = false;
3939

4040
function initialise(compilerEnv) {
41-
if (oneTimeInit) return;
42-
oneTimeInit = true;
41+
if (hasSetUpAutoClean) return;
42+
hasSetUpAutoClean = true;
4343
const tempDirCleanupSecs = compilerEnv.ceProps("tempDirCleanupSecs", 600);
4444
logger.info(`Cleaning temp dirs every ${tempDirCleanupSecs} secs`);
4545
setInterval(() => {
@@ -197,7 +197,7 @@ class CompileHandler {
197197

198198
splitArguments(options) {
199199
return _.chain(quote.parse(options || '')
200-
.map(x => typeof(x) === "string" ? x : x.pattern))
200+
.map(x => typeof (x) === "string" ? x : x.pattern))
201201
.compact()
202202
.value();
203203
}
@@ -250,9 +250,9 @@ class CompileHandler {
250250
},
251251
error => {
252252
logger.error("Error during compilation", error);
253-
if (typeof(error) !== "string") {
253+
if (typeof (error) !== "string") {
254254
if (error.code) {
255-
if (typeof(error.stderr) === "string") {
255+
if (typeof (error.stderr) === "string") {
256256
error.stdout = utils.parseOutput(error.stdout);
257257
error.stderr = utils.parseOutput(error.stderr);
258258
}
@@ -267,3 +267,6 @@ class CompileHandler {
267267
}
268268

269269
module.exports.Handler = CompileHandler;
270+
module.exports.SetTestMode = function () {
271+
hasSetUpAutoClean = true;
272+
};

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,15 @@
6565
"aws-sdk-mock": "^1.7.0",
6666
"chai": "3.5.x",
6767
"chai-as-promised": "^7.1.1",
68-
"chai-http": "^3.0.0",
68+
"chai-http": "^4.2.1",
6969
"codecov": "^3.0.0",
7070
"copy-webpack-plugin": "^4.1.1",
71-
"css-loader": "^0.28.7",
71+
"css-loader": "^2.1.0",
7272
"eslint": "^4.17.0",
7373
"extract-text-webpack-plugin": "^3.0.1",
7474
"file-loader": "^1.1.5",
7575
"istanbul": "^0.4.5",
76-
"mocha": "^3.3.0",
76+
"mocha": "^5.2.0",
7777
"nock": "^9.1.4",
7878
"requirejs": "*",
7979
"style-loader": "^0.19.0",

test/handlers/api-tests.js

+31-32
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,16 @@ describe('API handling', () => {
5858
}
5959
}, (key, def) => {
6060
switch (key) {
61-
case "formatters": return "formatt:badformatt";
62-
case "formatter.formatt.exe": return "echo";
63-
case "formatter.formatt.version": return "Release";
64-
case "formatter.formatt.name": return "FormatT";
65-
default: return def;
61+
case "formatters":
62+
return "formatt:badformatt";
63+
case "formatter.formatt.exe":
64+
return "echo";
65+
case "formatter.formatt.version":
66+
return "Release";
67+
case "formatter.formatt.name":
68+
return "FormatT";
69+
default:
70+
return def;
6671
}
6772
});
6873
app.use('/api', apiHandler.handle);
@@ -71,16 +76,16 @@ describe('API handling', () => {
7176
name: "GCC 9.0.0",
7277
lang: "c++"
7378
},
74-
{
75-
id: "fpc302",
76-
name: "FPC 3.0.2",
77-
lang: "pascal"
78-
},
79-
{
80-
id: "clangtrunk",
81-
name: "Clang trunk",
82-
lang: "c++"
83-
}];
79+
{
80+
id: "fpc302",
81+
name: "FPC 3.0.2",
82+
lang: "pascal"
83+
},
84+
{
85+
id: "clangtrunk",
86+
name: "Clang trunk",
87+
lang: "c++"
88+
}];
8489
apiHandler.setCompilers(compilers);
8590
apiHandler.setLanguages(languages);
8691

@@ -190,23 +195,20 @@ describe('API handling', () => {
190195
})
191196
.catch(err => {
192197
throw err;
193-
})
198+
});
194199
});
195200
it('should not go through with invalid tools', () => {
196201
return chai.request(app)
197202
.post('/api/format/invalid')
198203
.set('Accept', 'application/json')
199204
.then(res => {
200-
// Not expected to go here
201-
res.should.equal(null);
202-
})
203-
.catch(err => {
204-
err.response.should.have.status(422);
205-
err.response.should.be.json;
206-
err.response.body.should.deep.equals({exit: 2, answer: "Tool not supported"});
207-
})
205+
res.should.have.status(422);
206+
res.should.be.json;
207+
res.body.should.deep.equals({exit: 2, answer: "Tool not supported"});
208+
});
208209
});
209-
xit('should not go through with invalid base styles', () => {
210+
/*
211+
it('should not go through with invalid base styles', () => {
210212
return chai.request(app)
211213
.post('/api/format/formatt')
212214
.set('Accept', 'application/json')
@@ -216,13 +218,10 @@ describe('API handling', () => {
216218
source: ""
217219
})
218220
.then(res => {
219-
// Not expected to go here
220-
res.should.equal(null);
221-
})
222-
.catch(err => {
223-
err.response.should.have.status(422);
224-
err.response.should.be.json;
225-
err.response.body.should.deep.equals({exit: 3, answer: "Base style not supported"});
221+
res.should.have.status(422);
222+
res.should.be.json;
223+
res.body.should.deep.equals({exit: 3, answer: "Base style not supported"});
226224
});
227225
});
226+
*/
228227
});

test/handlers/compile-tests.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2323
// POSSIBILITY OF SUCH DAMAGE.
2424

25+
require('../../lib/handlers/compile').SetTestMode();
26+
2527
const chai = require('chai'),
2628
CompilationEnvironment = require('../../lib/compilation-env'),
2729
CompileHandler = require('../../lib/handlers/compile').Handler,
@@ -49,10 +51,8 @@ describe('Compiler tests', () => {
4951
it('throws for unknown compilers', () => {
5052
return chai.request(app)
5153
.post('/NOT_A_COMPILER/compile')
52-
.then(() => {
53-
throw "Shouldn't succeeed";
54-
}, (e) => {
55-
e.should.have.status(404);
54+
.then((res) => {
55+
res.should.have.status(404);
5656
});
5757
});
5858

test/lang-tests.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,8 @@ describe('Language definitions tests', () => {
3838
it('Has examples & are initialized', () => {
3939
Object.keys(languages).forEach(languageKey => {
4040
const lang = languages[languageKey];
41-
fs.stat(path.join('examples', lang.id, 'default' + lang.extensions[0]), (err, fd) => {
42-
should.equal(err, null);
43-
should.equal(fd, lang.example);
44-
});
41+
const example = fs.readFileSync(path.join('examples', lang.id, 'default' + lang.extensions[0]), 'utf-8');
42+
should.equal(example, lang.example);
4543
});
4644
});
4745
});

0 commit comments

Comments
 (0)