feat: execute files check via docker cli

WARNING: possible command injection vunerability.
This commit is contained in:
Lin, Hung-Shin authored and wancat committed 2023-10-29 17:41:38 +08:00
1 parent 38f3dbcd65
commit 50b1fe5124
4 files changed
+17 -53

No files matched your search

-6
View File
@@ -36,12 +36,6 @@ function waitForEnter() {
})
}
function waitTime(time) {
return new Promise((resolve) => {
setTimeout(resolve, time)
})
}
function printLine(content, delay, color) {
const printContent = `${colorize(content, color)}${RETURN_SYMBOL}`
return new Promise((resolve) => {
+16
View File
@@ -0,0 +1,16 @@
import { exec } from 'child_process'
import { buildFileCheckCmd } from '@linux-odyssey/utils'
// eslint-disable-next-line import/prefer-default-export
export function checkFiles(id, files) {
return new Promise((resolve) => {
const cmd = buildFileCheckCmd(files)
// WARNING: This is not secure!
exec(`docker exec ${id} ${cmd}`, (error, stdout, stderr) => {
console.log('stdout', stdout)
console.log('stderr', stderr)
resolve(!error)
})
})
}
-46
View File
@@ -1,6 +1,4 @@
import { PassThrough } from 'stream'
import Docker from 'dockerode'
import { buildFileCheckCmd } from '@linux-odyssey/utils'
import config from '../config.js'
const engine = new Docker()
@@ -79,47 +77,3 @@ export async function deleteContainer(id) {
}
await container.remove()
}
// Utility function to convert a stream to a string
function streamToString(stream) {
const chunks = []
return new Promise((resolve, reject) => {
stream.on('data', (chunk) => chunks.push(chunk))
stream.on('error', reject)
stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')))
setTimeout(() => resolve(Buffer.concat(chunks).toString('utf8')), 100)
})
}
export async function checkFiles(id, files) {
const container = engine.getContainer(id)
if (!container) {
throw new Error('Container not found')
}
const cmd = buildFileCheckCmd(files)
console.log(cmd)
const exec = await container.exec({
AttachStdout: true,
AttachStderr: true,
Cmd: ['sh', '-c', cmd],
})
const execStream = await exec.start()
const outStream = new PassThrough()
const errStream = new PassThrough()
console.log('exec started')
exec.modem.demuxStream(execStream, outStream, errStream)
const [output, errorOutput] = await Promise.all([
streamToString(outStream),
streamToString(errStream),
])
console.log('output', output, 'errorOutput', errorOutput)
const execOutput = await exec.inspect()
console.log('exit code', execOutput.ExitCode)
console.log('exit code === 0', execOutput.ExitCode === 0)
return execOutput.ExitCode === 0
}
+1 -1
View File
@@ -3,7 +3,7 @@ import { FileGraph } from '@linux-odyssey/file-graph'
import { Quest } from '@linux-odyssey/models'
import { pushToSession } from '../api/socket.js'
import SessionHandler from './sessionHandler.js'
import { checkFiles } from '../containers/docker.js'
import { checkFiles } from '../containers/cli.js'
// 檢查 pattern 是否符合 input
const checkMatch = (pattern, input) => {