Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions src/agent/systemd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,29 @@ export function generateServiceFile(options: InstallOptions = {}): string {
const port = options.port || DEFAULT_AGENT_PORT;
const configDir = options.configDir || DEFAULT_CONFIG_DIR;

const nodePath = process.execPath;
const agentPath = path.resolve(__dirname, 'index.js');

const envLines = [
`Environment=PERRY_PORT=${port}`,
`Environment=PERRY_CONFIG_DIR=${configDir}`,
`Environment=NODE_ENV=production`,
`Environment=SHELL=/bin/bash`,
];
const perryPath = process.execPath;

const execArgs = ['agent', 'run'];
if (port !== DEFAULT_AGENT_PORT) {
execArgs.push('--port', String(port));
}
if (configDir !== DEFAULT_CONFIG_DIR) {
execArgs.push('--config-dir', configDir);
}
if (options.noHostAccess) {
envLines.push(`Environment=PERRY_NO_HOST_ACCESS=true`);
execArgs.push('--no-host-access');
}

const envLines = [`Environment=NODE_ENV=production`, `Environment=SHELL=/bin/bash`];

return `[Unit]
Description=${SERVICE_DESCRIPTION}
After=network.target docker.service
Wants=docker.service

[Service]
Type=simple
ExecStart=${nodePath} ${agentPath}
ExecStart=${perryPath} ${execArgs.join(' ')}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: The ExecStart command for the systemd service is built without quoting arguments. This will cause paths with spaces to be parsed incorrectly by systemd, leading to service failure.
Severity: MEDIUM

Suggested Fix

The arguments in execArgs should be individually quoted before being joined to form the ExecStart string. For example, by mapping over the array: execArgs.map(arg => "${arg}").join(' '). This ensures that systemd treats each argument, including paths with spaces, as a single unit.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: src/agent/systemd.ts#L51

Potential issue: The code at `src/agent/systemd.ts:51` constructs the `ExecStart` line
for a systemd service by joining arguments with a space. However, it does not quote the
arguments. If a user provides a path with spaces for the `--config-dir` option (e.g.,
`/home/user/my config`), the resulting `ExecStart` line will be parsed incorrectly by
systemd. Systemd will split the path at the space, passing an incomplete path
(`/home/user/my`) to the `perry` agent. This will cause the service to start with the
wrong configuration or fail to start entirely.

Did we get this right? 👍 / 👎 to inform future reviews.

Restart=on-failure
RestartSec=5
${envLines.join('\n')}
Expand Down
Loading