aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Tuomi <jans.tuomi@gmail.com>2022-02-07 21:24:54 +0200
committerJan Tuomi <jans.tuomi@gmail.com>2022-02-07 21:24:54 +0200
commita1fdff7457569c26e749846da16b01e0304c0908 (patch)
treea539f0586f051658b1fff652b558729a7df8e95a
parentfdc97533885d95b2dff02e40014c654f79091b5b (diff)
Add some safeguards
-rw-r--r--src/index.ts9
1 files changed, 6 insertions, 3 deletions
diff --git a/src/index.ts b/src/index.ts
index 5621c8c..88a9a14 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -11,13 +11,16 @@ import { cors } from "@tinyhttp/cors"
const exec = promisify(cb_exec)
const app = new App()
-const PORT = Number(process.env.PORT) || 3000
+const PORT = process.env.PORT ? Number(process.env.PORT) : 3000
console.log(`Serving API on port ${PORT}`)
+const TIMEOUT = process.env.TIMEOUT ? Number(process.env.TIMEOUT) : 5000 // ms
+const MAX_SRC_LENGTH = process.env.MAX_SRC_LENGTH ? Number(process.env.MAX_SRC_LENGTH) : 4000 // characters
+
const interpretFile = async (sourceFile: string) => {
const cmd = `./interpreter ${sourceFile}`
try {
- const result = await exec(cmd)
+ const result = await exec(cmd, { timeout: TIMEOUT, killSignal: "SIGKILL" })
return result.stdout
} catch (err) {
console.error(err)
@@ -33,7 +36,7 @@ app
.use(json())
.post(RunnerApi.SubmitRequest.path, async (req, res) => {
const body: RunnerApi.SubmitRequest.Body | undefined = req.body
- if (!body || !body.source) {
+ if (!body || !body.source || body.source.length > MAX_SRC_LENGTH) {
return res.sendStatus(400)
}