* Create tools that operate within a Docker container
( sessionId: string, modelProvider: ModelProvider, )
| 46 | * Create tools that operate within a Docker container |
| 47 | */ |
| 48 | async function createDockerTools( |
| 49 | sessionId: string, |
| 50 | modelProvider: ModelProvider, |
| 51 | ) { |
| 52 | const docker = await getDockerModule(); |
| 53 | |
| 54 | // Create command executor for bash tool |
| 55 | const executeCommand = async (command: string) => { |
| 56 | logger.info("Docker bash tool executing command", { sessionId, command }); |
| 57 | |
| 58 | const allowedCommands = [ |
| 59 | "ls", |
| 60 | "cat", |
| 61 | "find", |
| 62 | "git", |
| 63 | "mkdir", |
| 64 | "cd", |
| 65 | "npm", |
| 66 | "pnpm", |
| 67 | "gh", |
| 68 | ]; |
| 69 | const forbiddenGitConfigPatterns = [ |
| 70 | "git config user.email", |
| 71 | "git config --global user.email", |
| 72 | "git config user.name", |
| 73 | "git config --global user.name", |
| 74 | ]; |
| 75 | const baseCommand = command.trim().split(/\s+/)[0]; |
| 76 | |
| 77 | if (!allowedCommands.includes(baseCommand)) { |
| 78 | const errorMsg = `Command '${baseCommand}' is not allowed. Only ${allowedCommands.join(", ")} are permitted.`; |
| 79 | logger.error("Docker bash tool command not allowed", { |
| 80 | sessionId, |
| 81 | command, |
| 82 | baseCommand, |
| 83 | allowedCommands, |
| 84 | }); |
| 85 | throw new Error(errorMsg); |
| 86 | } |
| 87 | |
| 88 | if ( |
| 89 | forbiddenGitConfigPatterns.some((pattern) => command.includes(pattern)) |
| 90 | ) { |
| 91 | const errorMsg = |
| 92 | "Commands that modify git user.name or user.email are forbidden. Use the existing repository configuration."; |
| 93 | logger.error("Docker bash tool command forbidden", { |
| 94 | sessionId, |
| 95 | command, |
| 96 | forbiddenPatterns: forbiddenGitConfigPatterns, |
| 97 | }); |
| 98 | throw new Error(errorMsg); |
| 99 | } |
| 100 | |
| 101 | try { |
| 102 | const { stdout, stderr, exitCode } = await docker.execCommand(sessionId, [ |
| 103 | "/bin/sh", |
| 104 | "-c", |
| 105 | command, |
no test coverage detected