Skip to content

Commit 43288cd

Browse files
Facundo FernandezFacundo Fernandez
authored andcommitted
fix: strip quotes from hostnames, add disclosure logging tests
Address CodeRabbit review: - getPresetEndpoints now strips surrounding quotes from YAML host values so disclosure log shows clean hostnames - Add tests: disclosure logging fires with real presets, is suppressed for nonexistent presets, and quoted hostnames are stripped Made-with: Cursor
1 parent 1937f42 commit 43288cd

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

bin/lib/policies.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ function getPresetEndpoints(content) {
5252
const regex = /host:\s*([^\s,}]+)/g;
5353
let match;
5454
while ((match = regex.exec(content)) !== null) {
55-
hosts.push(match[1]);
55+
hosts.push(match[1].replace(/^["']|["']$/g, ""));
5656
}
5757
return hosts;
5858
}

test/policies.test.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
import assert from "node:assert/strict";
5-
import { describe, it, expect } from "vitest";
5+
import { describe, it, expect, vi } from "vitest";
66
import path from "node:path";
77
import policies from "../bin/lib/policies";
88

@@ -67,6 +67,45 @@ describe("policies", () => {
6767
expect(hosts.length > 0).toBeTruthy();
6868
}
6969
});
70+
71+
it("strips surrounding quotes from hostnames", () => {
72+
const yaml = 'host: "example.com"\n host: \'other.com\'';
73+
const hosts = policies.getPresetEndpoints(yaml);
74+
expect(hosts).toEqual(["example.com", "other.com"]);
75+
});
76+
});
77+
78+
describe("applyPreset disclosure logging", () => {
79+
it("logs egress endpoints before applying", () => {
80+
const logSpy = vi.spyOn(console, "log").mockImplementation(() => {});
81+
const errSpy = vi.spyOn(console, "error").mockImplementation(() => {});
82+
const exitSpy = vi.spyOn(process, "exit").mockImplementation(() => { throw new Error("exit"); });
83+
84+
try {
85+
policies.applyPreset("test-sandbox", "npm");
86+
} catch {}
87+
88+
const messages = logSpy.mock.calls.map((c) => c[0]);
89+
expect(messages.some((m) => typeof m === "string" && m.includes("Widening sandbox egress"))).toBe(true);
90+
91+
logSpy.mockRestore();
92+
errSpy.mockRestore();
93+
exitSpy.mockRestore();
94+
});
95+
96+
it("does not log when preset has no endpoints", () => {
97+
const logSpy = vi.spyOn(console, "log").mockImplementation(() => {});
98+
const errSpy = vi.spyOn(console, "error").mockImplementation(() => {});
99+
100+
// loadPreset returns null for nonexistent presets → early return
101+
policies.applyPreset("test-sandbox", "nonexistent");
102+
103+
const messages = logSpy.mock.calls.map((c) => c[0]);
104+
expect(messages.some((m) => typeof m === "string" && m.includes("Widening sandbox egress"))).toBe(false);
105+
106+
logSpy.mockRestore();
107+
errSpy.mockRestore();
108+
});
70109
});
71110

72111
describe("buildPolicySetCommand", () => {

0 commit comments

Comments
 (0)