Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review: strict GitHub PR URL parsing, drop env-dependent gh test

Adam Eivy742e9bbe1429
diff --git a/server/services/git.js b/server/services/git.js
index 0da354073a..fecedd7404 100644
--- a/server/services/git.js
+++ b/server/services/git.js
@@ -503,16 +503,13 @@ export function parsePullRequestUrl(url) {
const segments = parsed.pathname.split('/').filter(Boolean);
// GitHub: /<owner>/<repo>/pull/<number>[/<more>]
- // Find the last `pull/<n>` pair so trailing /files, /commits etc. are tolerated.
- for (let i = segments.length - 2; i >= 1; i--) {
- if (segments[i] === 'pull') {
- const number = Number(segments[i + 1]);
- if (Number.isInteger(number) && number > 0) {
- const project = segments.slice(0, i);
- if (project.length >= 2) {
- return { host, owner: project[0], repo: project[project.length - 1], number };
- }
- }
+ // GitHub PR URLs are STRICTLY two segments before `pull` — anything else
+ // (e.g. /owner/repo/extra/pull/1) is invalid and would silently mis-parse
+ // if we just took the last segment as `repo`. Require segments[2] === 'pull'.
+ if (segments.length >= 4 && segments[2] === 'pull') {
+ const number = Number(segments[3]);
+ if (Number.isInteger(number) && number > 0) {
+ return { host, owner: segments[0], repo: segments[1], number };
}
}
diff --git a/server/services/git.test.js b/server/services/git.test.js
index cf63355fa7..0b7c2b12e4 100644
--- a/server/services/git.test.js
+++ b/server/services/git.test.js
@@ -164,6 +164,14 @@ describe('parsePullRequestUrl', () => {
expect(parsePullRequestUrl('https://github.com/atomantic/PortOS/pull/notanumber')).toBeNull();
expect(parsePullRequestUrl('https://github.com/atomantic/PortOS/pull/0')).toBeNull();
});
+
+ it('rejects GitHub URLs with extra path segments before /pull/ (no silent mis-parse)', () => {
+ // GitHub PR URLs are strictly /<owner>/<repo>/pull/<n>. Anything with extra
+ // segments before `pull` (e.g. /owner/repo/extra/pull/1) is invalid; we must
+ // return null rather than silently picking the wrong segment as `repo`.
+ expect(parsePullRequestUrl('https://github.com/owner/repo/extra/pull/1')).toBeNull();
+ expect(parsePullRequestUrl('https://github.com/owner/repo/extra/more/pull/1')).toBeNull();
+ });
});
describe('requestCopilotReview', () => {
@@ -172,14 +180,6 @@ describe('requestCopilotReview', () => {
expect(result).toEqual({ success: false, error: expect.stringContaining('unparseable PR URL') });
});
- it('does not throw on a parseable URL when gh is unavailable (graceful failure)', async () => {
- // We can't easily mock spawn here, but we can confirm the function returns
- // a structured failure rather than throwing when invoked end-to-end.
- const result = await requestCopilotReview('/nonexistent-path-for-test', 'https://github.com/test/repo/pull/1');
- expect(result).toHaveProperty('success');
- if (!result.success) expect(typeof result.error).toBe('string');
- });
-
it('returns { success: true, skipped: true } when the PR URL host is a non-GitHub forge', async () => {
// The current implementation short-circuits on the PR URL's host (via
// detectForgeCli(parsed.host)) before consulting the repo's origin remote.
@@ -188,6 +188,14 @@ describe('requestCopilotReview', () => {
const result = await requestCopilotReview('/nonexistent-path-for-test', 'https://gitlab.com/group/proj/-/merge_requests/1');
expect(result).toEqual({ success: true, skipped: true });
});
+
+ // The success/failure paths invoke spawn() against the real `gh` binary.
+ // Module-level mocking of `child_process` is fragile here: git.js captures
+ // `spawn` at load time and `resolveForgeForRepo` itself shells out to git
+ // and gh, making the mock surface bigger than the assertion is worth.
+ // The mocked-spawn coverage is provided in cleanupAgentWorktree.test.js,
+ // which mocks `./git.js` wholesale — here we just verify the parser/skip
+ // contract so the request never reaches a real network call.
});
describe('extractAgentSummary', () => {