Skip to content

Commit 78b313c

Browse files
committed
fixing bugs
2 parents 67cba09 + 1c6c6c3 commit 78b313c

File tree

3 files changed

+69
-16
lines changed

3 files changed

+69
-16
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,14 @@ RCE is a Remote Code Executor, as the name suggests Is a Docker-based sandbox en
2424
# build docker image
2525
docker-compose build
2626

27+
# create volume
28+
docker volume create my_vol
29+
2730
# run server in development mode
2831
docker-compose up server
2932
```
3033

34+
`
3135
This will build the required docker image and run the server in a docker container which will be listening at http://localhost:3000.
3236

3337
### For development and adding features
@@ -44,7 +48,8 @@ Runs the application is development mode. Should not be used in production
4448
npm run dev
4549
```
4650

47-
This is for development of the application.
51+
This is for
52+
development of the application.
4853

4954
#### Run in _production_ mode:
5055

server/api/services/code.service.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,32 @@ import fs from "fs";
22
import path from "path";
33
import util from "util";
44
import { execFile, spawn, exec } from "child_process";
5+
import ValidationService from "./validation.service";
56
const ROOT_DIR = `${process.cwd()}`;
67
const SOURCE_DIR = path.join(ROOT_DIR, "executor");
78
const TARGET_DIR = `/app/codes`;
89
const IMAGE_NAME = "executor:1.0";
910
const VOL_NAME = `my_vol`;
11+
// const VOL_NAME = SOURCE_DIR;
1012

1113
class CodeService {
1214
async execute(code, input, lang, id) {
1315
try {
1416
!input ? (input = "") : null;
17+
1518
//validating code
1619
// await this.validateCode(code, input, lang, id);
20+
const { isValid, message } = await ValidationService.execute(
21+
code,
22+
input,
23+
lang,
24+
id
25+
);
26+
if (!isValid) {
27+
throw {
28+
message,
29+
};
30+
}
1731

1832
//writing the code,input files
1933
const { file, inputFile } = await this.writeFile(code, lang, input, id);
@@ -44,20 +58,6 @@ class CodeService {
4458
}
4559
}
4660

47-
async validateCode(code, input, lang, id) {
48-
switch (lang) {
49-
case "javascript": {
50-
let words = ["require(", "exports.", "module.exports"];
51-
// prevent imports
52-
var valid = !words.some((el) => {
53-
return code.includes(el);
54-
});
55-
if (!valid) throw { message: "You have unacceptable libs imported" };
56-
}
57-
}
58-
// throw { message: "You librabry is not accepted : " + lib };
59-
}
60-
6161
async writeFile(code, lang, input, id) {
6262
let fileName = `${id}code`;
6363
switch (lang) {
@@ -115,7 +115,7 @@ class CodeService {
115115

116116
const runCode = `docker exec ${containerName} sh -c "${command}"`;
117117

118-
const runContainer = `docker run -it -d --name ${containerName} --mount source=${VOL_NAME},target=${TARGET_DIR} ${IMAGE_NAME}`;
118+
const runContainer = `docker run -it -d --name ${containerName} -v ${VOL_NAME}:${TARGET_DIR} ${IMAGE_NAME}`;
119119

120120
return { runCode, runContainer };
121121
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class ValidationService {
2+
async execute(code, input, lang, id) {
3+
switch (lang) {
4+
case "javascript": {
5+
let words = ["require(", "exports.", "module.exports"];
6+
// prevent imports
7+
var valid = !words.some((el) => {
8+
return code.includes(el);
9+
});
10+
return {
11+
isValid: valid,
12+
message: "You have unacceptable libs imported",
13+
};
14+
}
15+
case "python": {
16+
let reg1 = RegExp(
17+
/\bimport\W+(?:\w+\W+){0,}(?:os|subprocess|importlib)\b/g
18+
);
19+
words = ["open("];
20+
21+
if (code.match(reg1)) {
22+
return {
23+
isValid: false,
24+
message: "You have unacceptable libs imported",
25+
};
26+
} else if (
27+
words.every((el) => code.toLowerCase().includes(el.toLowerCase()))
28+
) {
29+
return {
30+
isValid: false,
31+
message: "You have unacceptable libs imported",
32+
};
33+
}
34+
return {
35+
isValid: true,
36+
};
37+
}
38+
default: {
39+
return {
40+
isValid: false,
41+
message: "Please select a valid language",
42+
};
43+
}
44+
}
45+
}
46+
}
47+
48+
export default new ValidationService();

0 commit comments

Comments
 (0)