This is the endpoint on the backend
const getTest = (req, res) => {
try {
fs.writeFileSync("modul/test/test.js", JSON.parse(req.body.code) + `n module.exports = {start};`);
res.status(200).send("File written successfully");
} catch (error) {
console.error('Error writing to file:', error);
res.status(500).send('Error writing to file');
return;
}
const buildCommand = 'docker build -t sandboxtest .';
exec(buildCommand, (buildError, buildStdout, buildStderr) => {
if (buildError) {
res.status(500).send('Error building Docker image');
return;
}
const dockerCommand = 'docker run --name sandboxtest -v /modul/test/testcontroller.js:/sandboxtest/code sandboxtest';
exec(dockerCommand, (error, stdout, stderr) => {
if (error) {
res.status(500).send('Error executing Docker command');
return;
}
exec('docker logs -f sandboxtest > modul/test/output.txt | docker rm sandboxtest', (rmError, rmStdout, rmStderr) => {
if (rmError) {
console.error(`Error removing Docker container: ${rmError}`);
res.status(500).send('Error removing Docker container');
return;
}
console.log('Docker container removed successfully');
const data = fs.readFileSync('modul/test/output.txt', 'utf-8');
console.log("Data: ", data);
res.status(200).json(data);
});
});
});
This is the endpoint on the front-end
try {
const response = await fetch('http://localhost:5000/test', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ code: JSON.stringify(code) })
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
console.log(data);
// outputModifier(JSON.parse(data));
} catch (e) {
console.log('Error:', e);
}
so when I make a request from the front end and do NOT write to the file, everything works fine
Web-Page with logs the results
But when I write the code from the frontend to a file to create a docker-image, I get an error
Web-Page error
The task is to run the code on a virtual machine so that it is not possible to send malicious code from the frontend. But there is this problem…
If you know how to solve it or any other options, please tell me
I tried to add async, await, Promises
Sanya Demenkov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.