Skip to content
Merged
Show file tree
Hide file tree
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: 21 additions & 2 deletions bin/lib/policies.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ function parseCurrentPolicy(raw) {
return raw.slice(sep + 3).trim();
}

/**
* Build the openshell policy set command with properly quoted arguments.
*/
function buildPolicySetCommand(policyFile, sandboxName) {
return `openshell policy set --policy "${policyFile}" --wait "${sandboxName}"`;
}

/**
* Build the openshell policy get command with properly quoted arguments.
*/
function buildPolicyGetCommand(sandboxName) {
return `openshell policy get --full "${sandboxName}" 2>/dev/null`;
}

function applyPreset(sandboxName, presetName) {
const presetContent = loadPreset(presetName);
if (!presetContent) {
Expand All @@ -86,7 +100,7 @@ function applyPreset(sandboxName, presetName) {
let rawPolicy = "";
try {
rawPolicy = runCapture(
`openshell policy get --full "${sandboxName}" 2>/dev/null`,
buildPolicyGetCommand(sandboxName),
{ ignoreError: true }
);
} catch {}
Expand Down Expand Up @@ -146,7 +160,8 @@ function applyPreset(sandboxName, presetName) {
fs.writeFileSync(tmpFile, merged, "utf-8");

try {
run(`openshell policy set --policy "${tmpFile}" --wait "${sandboxName}"`);
run(buildPolicySetCommand(tmpFile, sandboxName));

console.log(` Applied preset: ${presetName}`);
} finally {
fs.unlinkSync(tmpFile);
Expand Down Expand Up @@ -175,6 +190,10 @@ module.exports = {
listPresets,
loadPreset,
getPresetEndpoints,
extractPresetEntries,
parseCurrentPolicy,
buildPolicySetCommand,
buildPolicyGetCommand,
applyPreset,
getAppliedPresets,
};
26 changes: 26 additions & 0 deletions test/policies.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,32 @@ describe("policies", () => {
});
});

describe("buildPolicySetCommand", () => {
it("quotes sandbox name to prevent argument splitting", () => {
const cmd = policies.buildPolicySetCommand("/tmp/policy.yaml", "my-assistant");
assert.equal(cmd, 'openshell policy set --policy "/tmp/policy.yaml" --wait "my-assistant"');
});

it("handles sandbox names with spaces", () => {
const cmd = policies.buildPolicySetCommand("/tmp/policy.yaml", "my sandbox");
assert.ok(cmd.includes('"my sandbox"'), "sandbox name must be quoted");
});

it("places --wait before the sandbox name", () => {
const cmd = policies.buildPolicySetCommand("/tmp/policy.yaml", "test-box");
const waitIdx = cmd.indexOf("--wait");
const nameIdx = cmd.indexOf('"test-box"');
assert.ok(waitIdx < nameIdx, "--wait must come before sandbox name");
});
});

describe("buildPolicyGetCommand", () => {
it("quotes sandbox name", () => {
const cmd = policies.buildPolicyGetCommand("my-assistant");
assert.equal(cmd, 'openshell policy get --full "my-assistant" 2>/dev/null');
});
});

describe("preset YAML schema", () => {
it("no preset has rules at NetworkPolicyRuleDef level", () => {
// rules must be inside endpoints, not as sibling of endpoints/binaries
Expand Down