Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.
fix(github): confine github_api requests to api.github.com
SeashoreShicc5b879bcc6b
diff --git a/src/utils/github.test.ts b/src/utils/github.test.tsnew file mode 100644index 00000000..a2d9f2ac--- /dev/null+++ b/src/utils/github.test.ts@@ -0,0 +1,23 @@+import { describe, expect, it } from 'vitest';+import { resolveGitHubApiUrl } from './github.js';++describe('resolveGitHubApiUrl', () => {+ it('prefixes relative API paths with the GitHub API base', () => {+ expect(resolveGitHubApiUrl('/repos/o/r/issues')).toBe('https://api.github.com/repos/o/r/issues');+ });++ it('allows absolute api.github.com URLs', () => {+ expect(resolveGitHubApiUrl('https://api.github.com/repos/o/r')).toBe('https://api.github.com/repos/o/r');+ });++ it('rejects absolute URLs to other hosts so the token cannot leak', () => {+ expect(() => resolveGitHubApiUrl('https://evil.example/steal')).toThrow(/restricted to api\.github\.com/);+ expect(() => resolveGitHubApiUrl('http://127.0.0.1:9/x')).toThrow(/restricted to api\.github\.com/);+ // userinfo tricks must not bypass the host check+ expect(() => resolveGitHubApiUrl('https://[email protected]/')).toThrow(/restricted to api\.github\.com/);+ });++ it('rejects inputs that are neither an allowed URL nor an API path', () => {+ expect(() => resolveGitHubApiUrl('repos/o/r')).toThrow(/must start with/);+ });+});diff --git a/src/utils/github.ts b/src/utils/github.tsindex 43bc4e90..df87b0ab 100644--- a/src/utils/github.ts+++ b/src/utils/github.ts@@ -23,13 +23,43 @@ interface GitHubRequestOptions {headers?: Record<string, string>;}+const GITHUB_API_HOST = 'api.github.com';++/**+ * Resolve a `github_api` request target, confining it to GitHub so the bearer+ * token is never attached to a caller-chosen host (#81). Relative API paths+ * and absolute `https://api.github.com/...` URLs are allowed; any other+ * absolute URL is rejected.+ */+export function resolveGitHubApiUrl(path: string): string {+ const trimmed = path.trim();+ if (/^https?:\/\//i.test(trimmed)) {+ let url: URL;+ try {+ url = new URL(trimmed);+ } catch {+ throw new Error(`Invalid GitHub API URL: ${path}`);+ }+ if (url.hostname.toLowerCase() !== GITHUB_API_HOST) {+ throw new Error(+ `github_api is restricted to ${GITHUB_API_HOST}; refusing to send the GitHub token to ${url.hostname}`,+ );+ }+ return url.toString();+ }+ if (!trimmed.startsWith('/')) {+ throw new Error('github_api path must start with "/" (e.g. /repos/owner/repo/issues).');+ }+ return `${GITHUB_API}${trimmed}`;+}+export async function githubRequest(path: string, options: GitHubRequestOptions = {}): Promise<any> {const token = getGitHubToken();if (!token) {throw new Error('GITHUB_TOKEN not configured. Run mercury doctor to set it up.');}- const url = path.startsWith('http') ? path : `${GITHUB_API}${path}`;+ const url = resolveGitHubApiUrl(path);const headers: Record<string, string> = {'Authorization': `Bearer ${token}`,'Accept': 'application/vnd.github.v3+json',