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

reject path-unsafe owner/repo in the GitHub URL parser

[._.]/ Adam Eivy2e7193e53f51
diff --git a/client/src/lib/githubRepoUrl.js b/client/src/lib/githubRepoUrl.js
index 8592bb2971..e2e6c6989f 100644
--- a/client/src/lib/githubRepoUrl.js
+++ b/client/src/lib/githubRepoUrl.js
@@ -12,15 +12,37 @@
* `server/lib/githubRepoUrl.mirror.test.js`.
*/
+// The owner/repo pair is a PATH OPERAND, not just a label: githubCloner clones
+// into `join(reposDir, owner, repo)` and the resulting `localPath` is later
+// handed to an agent as the directory to scan/study. So both segments are
+// matched against the character sets GitHub actually allows, NOT "anything but
+// a slash" — the loose form parsed `https://github.com/../evil` as owner `..`,
+// which resolves OUTSIDE the managed clone root.
+// owner: a GitHub login — alphanumeric with internal hyphens, no dots
+// repo: alphanumerics plus `_`, `.`, `-`
+const OWNER = '[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?';
+const REPO = '[A-Za-z0-9_.-]+';
+
// SSH remote: [email protected]:owner/repo(.git)
-const SSH_REPO_RE = /^git@github\.com:([^/]+)\/([^/.]+)(?:\.git)?$/i;
+const SSH_REPO_RE = new RegExp(`^git@github\\.com:(${OWNER})/(${REPO})$`, 'i');
+
+// Any scheme (or none), optional userinfo, optional www: github.com/owner/repo[.git][/...]
+// Anchored at the host so `https://evil.com/github.com/o/r` is NOT read as a
+// GitHub repo. `REPO` cannot cross `/`, `?`, or `#`, so a deep link like
+// `github.com/o/r/tree/main` still resolves to `o/r`.
+const HTTPS_REPO_RE = new RegExp(
+ `^(?:[A-Za-z][A-Za-z0-9+.-]*://)?(?:[^/@\\s]+@)?(?:www\\.)?github\\.com/(${OWNER})/(${REPO})`,
+ 'i',
+);
-// HTTPS or scheme-less: [https://]github.com/owner/repo[.git][/...]
-const HTTPS_REPO_RE = /(?:https?:\/\/)?github\.com\/([^/]+)\/([^/?#]+)/i;
+// `REPO`'s character class admits the dot segments `.` and `..`, which would
+// escape (or collapse to) the clone root the same way a bad owner does. The
+// owner pattern already rejects them by requiring a leading alphanumeric.
+const DOT_SEGMENTS = new Set(['.', '..']);
/**
* Parse a GitHub URL into `{ owner, repo, isGitHub }`, or null when the URL
- * isn't a GitHub repository.
+ * isn't a GitHub repository (or names a path-unsafe owner/repo).
*
* @param {string} url
* @returns {{ owner: string, repo: string, isGitHub: true } | null}
@@ -29,13 +51,14 @@ export function parseGitHubUrl(url) {
if (!url) return null;
const normalized = String(url).trim();
- const ssh = normalized.match(SSH_REPO_RE);
- if (ssh) return { owner: ssh[1], repo: ssh[2], isGitHub: true };
+ const match = normalized.match(SSH_REPO_RE) || normalized.match(HTTPS_REPO_RE);
+ if (!match) return null;
- const https = normalized.match(HTTPS_REPO_RE);
- if (https) return { owner: https[1], repo: https[2].replace(/\.git$/, ''), isGitHub: true };
+ const owner = match[1];
+ const repo = match[2].replace(/\.git$/i, '');
+ if (!repo || DOT_SEGMENTS.has(repo)) return null;
- return null;
+ return { owner, repo, isGitHub: true };
}
/**
diff --git a/server/lib/githubRepoUrl.js b/server/lib/githubRepoUrl.js
index 013a86e068..1c5466a1e0 100644
--- a/server/lib/githubRepoUrl.js
+++ b/server/lib/githubRepoUrl.js
@@ -12,15 +12,37 @@
* `server/lib/githubRepoUrl.mirror.test.js`. Port any change to both.
*/
+// The owner/repo pair is a PATH OPERAND, not just a label: githubCloner clones
+// into `join(reposDir, owner, repo)` and the resulting `localPath` is later
+// handed to an agent as the directory to scan/study. So both segments are
+// matched against the character sets GitHub actually allows, NOT "anything but
+// a slash" — the loose form parsed `https://github.com/../evil` as owner `..`,
+// which resolves OUTSIDE the managed clone root.
+// owner: a GitHub login — alphanumeric with internal hyphens, no dots
+// repo: alphanumerics plus `_`, `.`, `-`
+const OWNER = '[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?';
+const REPO = '[A-Za-z0-9_.-]+';
+
// SSH remote: [email protected]:owner/repo(.git)
-const SSH_REPO_RE = /^git@github\.com:([^/]+)\/([^/.]+)(?:\.git)?$/i;
+const SSH_REPO_RE = new RegExp(`^git@github\\.com:(${OWNER})/(${REPO})$`, 'i');
+
+// Any scheme (or none), optional userinfo, optional www: github.com/owner/repo[.git][/...]
+// Anchored at the host so `https://evil.com/github.com/o/r` is NOT read as a
+// GitHub repo. `REPO` cannot cross `/`, `?`, or `#`, so a deep link like
+// `github.com/o/r/tree/main` still resolves to `o/r`.
+const HTTPS_REPO_RE = new RegExp(
+ `^(?:[A-Za-z][A-Za-z0-9+.-]*://)?(?:[^/@\\s]+@)?(?:www\\.)?github\\.com/(${OWNER})/(${REPO})`,
+ 'i',
+);
-// HTTPS or scheme-less: [https://]github.com/owner/repo[.git][/...]
-const HTTPS_REPO_RE = /(?:https?:\/\/)?github\.com\/([^/]+)\/([^/?#]+)/i;
+// `REPO`'s character class admits the dot segments `.` and `..`, which would
+// escape (or collapse to) the clone root the same way a bad owner does. The
+// owner pattern already rejects them by requiring a leading alphanumeric.
+const DOT_SEGMENTS = new Set(['.', '..']);
/**
* Parse a GitHub URL into `{ owner, repo, isGitHub }`, or null when the URL
- * isn't a GitHub repository.
+ * isn't a GitHub repository (or names a path-unsafe owner/repo).
*
* @param {string} url
* @returns {{ owner: string, repo: string, isGitHub: true } | null}
@@ -29,13 +51,14 @@ export function parseGitHubUrl(url) {
if (!url) return null;
const normalized = String(url).trim();
- const ssh = normalized.match(SSH_REPO_RE);
- if (ssh) return { owner: ssh[1], repo: ssh[2], isGitHub: true };
+ const match = normalized.match(SSH_REPO_RE) || normalized.match(HTTPS_REPO_RE);
+ if (!match) return null;
- const https = normalized.match(HTTPS_REPO_RE);
- if (https) return { owner: https[1], repo: https[2].replace(/\.git$/, ''), isGitHub: true };
+ const owner = match[1];
+ const repo = match[2].replace(/\.git$/i, '');
+ if (!repo || DOT_SEGMENTS.has(repo)) return null;
- return null;
+ return { owner, repo, isGitHub: true };
}
/**
diff --git a/server/lib/githubRepoUrl.mirror.test.js b/server/lib/githubRepoUrl.mirror.test.js
index 4f0ae7cde1..319f8541c2 100644
--- a/server/lib/githubRepoUrl.mirror.test.js
+++ b/server/lib/githubRepoUrl.mirror.test.js
@@ -22,8 +22,11 @@ const SERVER_PATH = resolve(__dirname, 'githubRepoUrl.js');
const CLIENT_PATH = resolve(__dirname, '../../client/src/lib/githubRepoUrl.js');
const MIRRORED_NAMES = [
+ 'OWNER',
+ 'REPO',
'SSH_REPO_RE',
'HTTPS_REPO_RE',
+ 'DOT_SEGMENTS',
'parseGitHubUrl',
'isGitHubRepoUrl',
];
diff --git a/server/lib/githubRepoUrl.test.js b/server/lib/githubRepoUrl.test.js
new file mode 100644
index 0000000000..b67b223a7b
--- /dev/null
+++ b/server/lib/githubRepoUrl.test.js
@@ -0,0 +1,102 @@
+import { describe, it, expect } from 'vitest';
+import { join } from 'path';
+import { parseGitHubUrl, isGitHubRepoUrl } from './githubRepoUrl.js';
+
+const REPOS_ROOT = '/data/repos';
+const clonePath = (url) => {
+ const parsed = parseGitHubUrl(url);
+ return parsed ? join(REPOS_ROOT, parsed.owner, parsed.repo) : null;
+};
+
+describe('parseGitHubUrl', () => {
+ it('parses the shapes a user actually pastes', () => {
+ for (const url of [
+ 'https://github.com/example-owner/example-repo',
+ 'https://github.com/example-owner/example-repo.git',
+ 'http://github.com/example-owner/example-repo',
+ 'https://www.github.com/example-owner/example-repo',
+ 'github.com/example-owner/example-repo',
+ '[email protected]:example-owner/example-repo.git',
+ '[email protected]:example-owner/example-repo',
+ ]) {
+ expect(parseGitHubUrl(url), url).toEqual({
+ owner: 'example-owner',
+ repo: 'example-repo',
+ isGitHub: true,
+ });
+ }
+ });
+
+ it('resolves a deep link back to the repo it belongs to', () => {
+ expect(parseGitHubUrl('https://github.com/example-owner/example-repo/tree/main/src'))
+ .toMatchObject({ owner: 'example-owner', repo: 'example-repo' });
+ expect(parseGitHubUrl('https://github.com/example-owner/example-repo?tab=readme'))
+ .toMatchObject({ repo: 'example-repo' });
+ expect(parseGitHubUrl('https://github.com/example-owner/example-repo#install'))
+ .toMatchObject({ repo: 'example-repo' });
+ });
+
+ it('keeps a dot inside a repo name but strips only a trailing .git', () => {
+ expect(parseGitHubUrl('https://github.com/example-owner/my.config.repo'))
+ .toMatchObject({ repo: 'my.config.repo' });
+ expect(parseGitHubUrl('https://github.com/example-owner/my.repo.git'))
+ .toMatchObject({ repo: 'my.repo' });
+ });
+
+ it('is not a GitHub repo without both segments', () => {
+ expect(parseGitHubUrl('https://github.com/example-owner')).toBeNull();
+ expect(parseGitHubUrl('https://github.com/settings')).toBeNull();
+ expect(parseGitHubUrl('https://example.com/example-owner/example-repo')).toBeNull();
+ expect(parseGitHubUrl('')).toBeNull();
+ expect(parseGitHubUrl(null)).toBeNull();
+ });
+
+ // The parsed pair is a PATH OPERAND — githubCloner clones into
+ // join(reposDir, owner, repo), and that localPath is later handed to an agent
+ // as the directory to scan/study. A dot segment escapes (or collapses to) the
+ // managed clone root.
+ describe('path safety', () => {
+ it('refuses a dot segment in either position', () => {
+ for (const url of [
+ 'https://github.com/../evil',
+ 'github.com/../evil',
+ '[email protected]:../evil',
+ 'https://github.com/example-owner/..',
+ 'https://github.com/example-owner/.',
+ 'https://github.com/../../etc/passwd',
+ ]) {
+ expect(parseGitHubUrl(url), url).toBeNull();
+ }
+ });
+
+ it('refuses percent-encoded and separator characters in the segments', () => {
+ for (const url of [
+ 'https://github.com/%2e%2e/evil',
+ 'https://github.com/example-owner/%2e%2e',
+ 'https://github.com/a b/c',
+ ]) {
+ expect(parseGitHubUrl(url), url).toBeNull();
+ }
+ });
+
+ it('never yields a clone path outside the repos root', () => {
+ expect(clonePath('https://github.com/../evil')).toBeNull();
+ expect(clonePath('https://github.com/example-owner/..')).toBeNull();
+ expect(clonePath('https://github.com/example-owner/example-repo'))
+ .toBe('/data/repos/example-owner/example-repo');
+ });
+
+ it('does not read a foreign host that merely mentions github.com', () => {
+ expect(parseGitHubUrl('https://evil.example.com/github.com/example-owner/example-repo')).toBeNull();
+ expect(parseGitHubUrl('https://notgithub.com/example-owner/example-repo')).toBeNull();
+ });
+ });
+});
+
+describe('isGitHubRepoUrl', () => {
+ it('agrees with parseGitHubUrl', () => {
+ expect(isGitHubRepoUrl('https://github.com/example-owner/example-repo')).toBe(true);
+ expect(isGitHubRepoUrl('https://github.com/../evil')).toBe(false);
+ expect(isGitHubRepoUrl('https://example.com')).toBe(false);
+ });
+});