Files
PaperClipAI/server/src/services/chat-github-attachments.ts
T
DottaandPaperclip ed2c22ea8c feat(connections): simplify dedicated GitHub App setup (#15416)
## Thinking Path

> - Paperclip manages agent work and the tools each agent can use.
> - GitHub bots need their own App identity and restricted repository
access.
> - Manual App configuration and repeated setup gates make that
connection hard to finish.
> - GitHub can create an App from a manifest, but people must approve
creation and repository access.
> - Each Paperclip stack should own configuration, credentials,
signature verification, and task admission.
> - This change uses Cloud as an encrypted callback and event gateway,
then completes setup in the stack.
> - GitHub review bots have their own experimental setting. Chat
connector opt-in does not control them.

## Linked Issues or Issue Description

**What existing behavior does this improve?**

Dedicated GitHub App setup, bot settings, and task-scoped GitHub
replies.

**Current behavior**

Users must configure an App, copy credentials, repeat repository
selection, and complete manual verification gates. Review settings are
hard to scan. Mentions in descriptions can be missed, and automatic
completion text can produce an extra GitHub comment.

**Proposed behavior**

Choose an agent and connect GitHub under a personal account or
organization. GitHub asks for creation and installation consent.
Paperclip resumes the same draft, stores secrets locally, imports
access, verifies delivery, and opens Settings. Each accepted request has
one editable App-owned comment for progress and results. Review
assessments produce deterministic checks for the reviewed commit.

**Breaking changes**

GitHub bots require their own default-off experimental opt-in. The
setup-method and delegated-agent screens are removed. Per-person
automatic-run flags remain readable but are replaced by author filters.
Existing connections, direct webhooks, saved restrictions, and manual
existing-App recovery remain supported.

Companion gateway:
https://github.com/paperclipai/paperclip-cloud/pull/688 (merged). The
roadmap and related PRs were checked; no duplicate dedicated-App wizard
was found.

## What Changed

- Add an independent **GitHub review bots** setting across the catalog,
routes, identity admission, and task controls. Existing bots continue
running when the UI setting is off.
- Replace setup with Choose agent and Connect GitHub. Support personal
and organization owners, inline low-trust conversion, saved drafts,
known organization suggestions, automatic completion, and same-App
recovery.
- Keep manifests, OAuth PKCE, credential exchange, vault storage,
installation verification, and original webhook-signature validation in
the stack. Use the Cloud sealed gateway for public callbacks and
outbound local delivery.
- Add resumable owner metadata in migration 0323. Retry a failed
registration renewal through the same durable replacement rather than
creating another App route.
- Support authorized mentions in issue and PR bodies, comments, inline
comments, and review summaries. Preserve attachments, edited-source
identity, duplicate protection, and lifecycle invalidation. Filter
requests explicitly addressed to another connected bot.
- Give agents task-scoped review, comment, and update tools. Update one
working comment through progress and final output. Close coalesced
requests and recover delayed acknowledgements, including deleted earlier
working comments. Preserve automatic replies for legacy connections
without saved review configuration. Preserve partial publication
evidence and quarantine uncertain automatic fallbacks without blocking
Board messages or questions.
- Reorganize Settings and Access with rich slash-skill instructions,
numeric passing scores, clear automation controls, formal-review
tooltips, member and contributor sections, and verified App branding
links. Preserve invalid score drafts and automation choices across tabs.
- Paginate repositories in 20-row batches with search and a true
connection-wide toggle. Batch verification at GitHub's 500-ID token
limit. Keep saved repository overrides while hiding their editor.
- Give each review a detail URL that remains usable beyond the newest
100 records. Synchronize the manager OpenAPI contracts and add recovery,
authorization, UI, and browser regressions.
- Reconcile the current master branch and retain its agent avatar,
feature catalog, and extracted heartbeat preparation code. Preserve a
saved test-drive port and recover from an occupied high port.

## Verification

- Focused UI, independent experimental-setting, and CLI tests: 199
passed.
- `pnpm -r typecheck`, `pnpm build`, and `pnpm check:token-gates`
passed. The broad local Vitest attempt was interrupted after resource
timeouts and stale expectations; the affected regressions passed focused
reruns. The latest-head full CI matrix passed, including all browser
shards, server/chat suites, builds, typechecking, canary packaging, and
Docker context integrity. A Docker Hub 429 required a CI retry; it
passed without changing the code.
- Final review repairs: 127 attachment/event tests and server typecheck
passed. All 13 final integration regressions passed under the normal CI
timeout, covering legacy reply compatibility, deleted coalesced
comments, inline-only review completion, progress edits, and native
final recovery. Restart-safe description/review attachments passed the
attachment suite.
- Final regressions: four reply/lifecycle tests, both native GitHub wait
tests, 171 feature-gate/reply-contract tests, the routed review preview,
and five scheduler/reply regressions passed. The final fixture repair
also passed all six setup-continuation cases and all three complete
chat-test shards in CI, covering terminal receipts and retries under the
normal CI timeout. One loaded-host receipt run exceeded its local
timeout; the exact case passed in an isolated rerun and the complete CI
shard.
- GitHub integration tests cover personal and organization setup, retry
recovery, edited mentions, attachment provenance, coalesced requests,
delayed acknowledgements, partial review writes, old review URLs, and
company boundaries.
- Browser qualification: both GitHub journeys, all five provider
setup/management journeys, and the GitHub uncertain-send recovery
journey passed. GitHub setup, reconnect, and task replies run with chat
disabled; chat enabled alone cannot expose GitHub bots. The disposable
qualification database was removed after testing.
- Earlier live qualification verified App-owned issue replies, native
slash-skill invocation, progress comment edits, and a 5/5 PR review with
a passing current-head check in the dedicated permissions-smoke
repository. These live checks did not use personal credentials as bot
execution authority.

## Risks

- Deploy the merged Cloud gateway protocol before the stack update. Each
callback and encrypted event must retain the original instance and
tenant binding.
- Migration 0323 uses idempotent column additions for test installations
that already applied the earlier draft migration number.
- A verified connection does not prove runtime isolation or a completed
review. Runtime checks remain separate.
- Required merge checks must be configured in GitHub. A passing numeric
score does not formally approve a PR; approval and request-changes tools
remain opt-in.
- Existing-App credentials cannot silently renew an uncertain manifest
exchange. Recovery preserves the existing App, draft, connection, and
agent.
- Repository changes save immediately. Global toggles affect all
currently available repositories, including unloaded rows, without
granting new installation access.
- A failed or uncertain tool write suppresses automatic fallback text;
the governed outbox retains its evidence. Deleted or replaced response
comments are not recreated through a personal credential fallback.
- GitHub logo upload remains an optional human action in GitHub App
settings. Runtime provisioning, delegated setup, screenshot publication,
and production push delivery remain deferred.

## Model Used

OpenAI Codex, based on GPT-6, with tool use and code execution. The
exact runtime model identifier and context size are not exposed in this
session.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub references)
- [x] My branch name describes the change and contains no internal
Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass (focused suites; complete
test matrix is checked in CI)
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [x] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-10-09 17:01:20 -05:00

889 lines
30 KiB
TypeScript

import type { Attachment, Message } from "chat";
import { createHash } from "node:crypto";
import { JSDOM } from "jsdom";
import {
isAllowedContentType,
MAX_ATTACHMENT_BYTES,
normalizeContentType,
normalizeUploadAttachmentContentType,
} from "../attachment-types.js";
import { guardedRemoteHttpFetch } from "./remote-http-fetch.js";
const MAX_URL_LENGTH = 2048;
const DOWNLOAD_TIMEOUT_MS = 20_000;
export const GITHUB_ATTACHMENT_BATCH_TIMEOUT_MS = 60_000;
const MAX_ATTACHMENTS = 20;
const MAX_REFERENCES = 10_000;
const REDIRECT_STATUSES = new Set([301, 302, 303, 307, 308]);
const CDN_HOSTS = new Set([
"user-images.githubusercontent.com",
"private-user-images.githubusercontent.com",
"github-production-user-asset-6210df.s3.amazonaws.com",
"github-production-repository-file-5c1aeb.s3.amazonaws.com",
]);
const MIME_EXTENSIONS: Record<string, string> = {
"image/png": ".png",
"image/jpeg": ".jpg",
"image/jpg": ".jpg",
"image/gif": ".gif",
"image/webp": ".webp",
"application/pdf": ".pdf",
"text/plain": ".txt",
"text/markdown": ".md",
"text/csv": ".csv",
"application/json": ".json",
"application/zip": ".zip",
"video/mp4": ".mp4",
"video/webm": ".webm",
"audio/mpeg": ".mp3",
};
type GitHubAttachmentSourceKind = "issue_description" | "pull_request_description" | "review_summary";
const BODY_SOURCE_KINDS: GitHubAttachmentSourceKind[] = ["issue_description", "pull_request_description", "review_summary"];
/** Provenance is the admitted source, not ownership of GitHub's anonymized upload. */
export interface GitHubPublicAttachmentLocator {
kind: "github_public_attachment";
url: string;
sourceThreadId: string;
sourceMessageId: string;
/** Old four-field descriptors remain anonymous-only. */
version?: 2 | 3;
sourceBodySha256?: string;
sourceKind?: GitHubAttachmentSourceKind;
sourceId?: string;
}
export interface GitHubAttachmentCommentRequest {
url: string;
accept: string;
}
export type GitHubAttachmentCommentResolver = (
request: GitHubAttachmentCommentRequest,
signal: AbortSignal,
) => Promise<unknown>;
export function isGitHubAttachmentCommentRequest(
request: GitHubAttachmentCommentRequest,
): boolean {
const match =
/^https:\/\/api\.github\.com\/repos\/[a-z0-9][a-z0-9-]{0,38}\/([a-z0-9_.-]{1,100})\/(issues|pulls)\/(comments\/[1-9][0-9]{0,24}|[1-9][0-9]{0,24}(?:\/reviews\/[1-9][0-9]{0,24})?)$/i.exec(
request.url,
);
return Boolean(
match &&
![".", ".."].includes(match[1]!) &&
!(match[2] === "issues" && match[3]!.includes("/reviews/")) &&
request.accept ===
(match[2] === "pulls" && (match[3]!.startsWith("comments/") || match[3]!.includes("/reviews/"))
? "application/vnd.github-commitcomment.full+json"
: "application/vnd.github.full+json"),
);
}
const handles = new WeakMap<Attachment, GitHubPublicAttachmentLocator>();
const limitOmissions = new WeakMap<Message, number>();
/** Informational only: this count cannot authorize or identify a downloadable file. */
export function githubAttachmentLimitOmissions(message: Message): number {
return limitOmissions.get(message) ?? 0;
}
export function restoreGitHubAttachmentLimitOmissions(
message: Message,
value: unknown,
): void {
limitOmissions.delete(message);
if (
typeof value === "number" &&
Number.isInteger(value) &&
value > 0 &&
value <= MAX_REFERENCES
)
limitOmissions.set(message, value);
}
const GITHUB_ATTACHMENT_DIAGNOSTIC_CODES = [
"github_attachment_not_public",
"github_attachment_invalid_response",
"github_attachment_unsafe_redirect",
"github_attachment_too_large",
"github_attachment_empty",
"github_attachment_unsupported_type",
"github_attachment_download_failed",
"github_attachment_source_mismatch",
"github_attachment_canonical_authority_unavailable",
"github_attachment_canonical_api_request_failed",
"github_attachment_canonical_api_access_denied",
"github_attachment_canonical_api_status_unexpected",
"github_attachment_canonical_api_invalid_response",
"github_attachment_canonical_api_too_large",
"github_attachment_canonical_response_unavailable",
"github_attachment_canonical_source_mismatch",
"github_attachment_canonical_body_mismatch",
"github_attachment_canonical_html_unavailable",
"github_attachment_canonical_file_unsupported",
"github_attachment_canonical_image_count_invalid",
"github_attachment_canonical_target_denied",
"github_attachment_canonical_mapping_ambiguous",
"github_attachment_canonical_signed_anchor_only",
"github_attachment_canonical_image_without_source_anchor",
"github_attachment_canonical_anchor_missing",
] as const;
type GitHubAttachmentDiagnosticCode =
(typeof GITHUB_ATTACHMENT_DIAGNOSTIC_CODES)[number];
/** Read no request/response data; SDK wrappers may retain a closed error as cause. */
export function githubAttachmentDiagnosticCode(
error: unknown,
): GitHubAttachmentDiagnosticCode | null {
for (let depth = 0; depth < 4 && error instanceof Error; depth++) {
if (
GITHUB_ATTACHMENT_DIAGNOSTIC_CODES.includes(
error.message as GitHubAttachmentDiagnosticCode,
)
)
return error.message as GitHubAttachmentDiagnosticCode;
error = error.cause;
}
return null;
}
export class GitHubAttachmentUnavailableError extends Error {
constructor(readonly code: GitHubAttachmentDiagnosticCode) {
// Closed codes only: URLs, signed redirects and provider response bodies never escape.
super(code);
this.name = "GitHubAttachmentUnavailableError";
}
}
export function canonicalGitHubAttachmentUrl(value: unknown): string | null {
if (typeof value !== "string" || value.length > MAX_URL_LENGTH) return null;
try {
const url = new URL(value);
if (
url.origin !== "https://github.com" ||
url.username ||
url.password ||
url.search ||
url.hash
)
return null;
if (
!/^\/user-attachments\/(?:assets\/[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}|files\/[1-9][0-9]*\/[^/]+)$/i.test(
url.pathname,
)
)
return null;
if (
/%(?:2f|5c|00|0[ad])/i.test(url.pathname) ||
url.pathname.includes("\\")
)
return null;
return url.href;
} catch {
return null;
}
}
function validThread(value: unknown): value is string {
return (
typeof value === "string" &&
value.length <= 512 &&
/^github:[a-z0-9_.-]+\/[a-z0-9_.-]+:(?:issue:)?[1-9][0-9]*(?::rc:[1-9][0-9]*)?$/i.test(
value,
)
);
}
export function validateGitHubAttachmentLocator(
value: unknown,
): GitHubPublicAttachmentLocator | null {
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
const row = value as Record<string, unknown>;
if (
![
"kind,sourceMessageId,sourceThreadId,url",
"kind,sourceBodySha256,sourceMessageId,sourceThreadId,url,version",
"kind,sourceBodySha256,sourceId,sourceKind,sourceMessageId,sourceThreadId,url,version",
].includes(Object.keys(row).sort().join(",")) ||
(("version" in row || "sourceBodySha256" in row) &&
(![2, 3].includes(row.version as number) ||
typeof row.sourceBodySha256 !== "string" ||
!/^[a-f0-9]{64}$/.test(row.sourceBodySha256))) ||
row.kind !== "github_public_attachment" ||
(row.version !== 3 && ("sourceKind" in row || "sourceId" in row)) ||
!validThread(row.sourceThreadId) ||
typeof row.sourceMessageId !== "string" ||
!(row.version === 3
? typeof row.sourceId === "string" && /^[1-9][0-9]{0,24}$/.test(row.sourceId) &&
BODY_SOURCE_KINDS.includes(row.sourceKind as GitHubAttachmentSourceKind) &&
(/^mention-event:[a-z0-9_-]{1,200}$/i.test(row.sourceMessageId) ||
(row.sourceKind === "review_summary" && row.sourceMessageId === `review:${row.sourceId}`))
: /^[1-9][0-9]{0,24}$/.test(row.sourceMessageId))
)
return null;
const url = canonicalGitHubAttachmentUrl(row.url);
return url
? {
kind: "github_public_attachment",
url,
sourceThreadId: row.sourceThreadId,
sourceMessageId: row.sourceMessageId,
...(row.version === 2 || row.version === 3
? {
version: row.version,
sourceBodySha256: row.sourceBodySha256 as string,
...(row.version === 3 ? { sourceKind: row.sourceKind as GitHubAttachmentSourceKind, sourceId: row.sourceId as string } : {}),
}
: {}),
}
: null;
}
function safeName(value: unknown): string | undefined {
if (typeof value !== "string") return undefined;
const name = value
.replace(/[\u0000-\u001f\u007f/\\]/g, "_")
.trim()
.slice(0, 200);
return name && name !== "." && name !== ".." ? name : undefined;
}
export function githubAttachmentLocator(
attachment: Attachment,
): GitHubPublicAttachmentLocator | null {
return handles.get(attachment) ?? null;
}
export function rehydrateGitHubPublicAttachment(
value: unknown,
source: { threadId: string; messageId: string },
): Attachment | null {
const locator = validateGitHubAttachmentLocator(value);
if (
!locator ||
locator.sourceThreadId !== source.threadId ||
locator.sourceMessageId !== source.messageId
)
return null;
const url = new URL(locator.url);
let name: string | undefined;
if (url.pathname.startsWith("/user-attachments/files/")) {
try {
name = safeName(decodeURIComponent(url.pathname.split("/").at(-1)!));
} catch {
return null;
}
}
const attachment: Attachment = {
type: "file",
name: name ?? `github-attachment-${url.pathname.split("/").at(-1)}`,
};
handles.set(attachment, locator);
return attachment;
}
/** Parse references only; network I/O happens later, after Paperclip's admission fence. */
export function githubPublicAttachmentsFromMessage(
message: Message,
): Attachment[] {
limitOmissions.delete(message);
const raw = message.raw as Record<string, unknown> | null;
if (
!raw ||
!validThread(message.threadId)
)
return [];
const bodySource = BODY_SOURCE_KINDS.includes(raw.type as GitHubAttachmentSourceKind);
const comment = (bodySource ? raw.source : raw.comment) as Record<string, unknown> | undefined;
const repository = raw.repository as Record<string, unknown> | undefined;
if (
!comment ||
!repository ||
!/^[1-9][0-9]{0,24}$/.test(String(comment.id)) ||
(typeof comment.id === "number" && !Number.isSafeInteger(comment.id)) ||
(!bodySource && String(comment.id) !== message.id) ||
typeof comment.body !== "string" ||
comment.body.length > 200_000
)
return [];
const thread =
/^github:([^:]+):(?:(issue):)?([1-9][0-9]*)(?::rc:([1-9][0-9]*))?$/i.exec(
message.threadId,
);
if (
!thread ||
typeof repository.full_name !== "string" ||
thread[1].toLowerCase() !== repository.full_name.toLowerCase() ||
Number(thread[3]) !== raw.prNumber
)
return [];
if (bodySource) {
if (thread[4] || Boolean(thread[2]) !== (raw.type === "issue_description") ||
(raw.type !== "review_summary" && comment.number !== Number(thread[3]))) return [];
} else if (raw.type === "review_comment") {
if (thread[2] || thread[4] !== String(comment.in_reply_to_id ?? comment.id))
return [];
} else if (
raw.type !== "issue_comment" ||
thread[4] ||
Boolean(thread[2]) !== (raw.threadType === "issue")
)
return [];
const urls = new Set<string>();
const definitions = new Map<string, unknown>();
const references: string[] = [];
const stack: unknown[] = [message.formatted];
let visited = 0;
const sourceBody = comment.body;
const add = (value: unknown) => {
if (typeof value !== "string" || !sourceBody.includes(value)) return;
const url = canonicalGitHubAttachmentUrl(value);
if (url && urls.size < MAX_REFERENCES) urls.add(url);
};
while (stack.length && visited++ < 10_000) {
const node = stack.pop() as Record<string, unknown> | null;
if (!node || typeof node !== "object") continue;
if (node.type === "link" || node.type === "image") add(node.url);
if (node.type === "definition" && typeof node.identifier === "string")
definitions.set(node.identifier.toLowerCase(), node.url);
if (
(node.type === "imageReference" || node.type === "linkReference") &&
typeof node.identifier === "string"
)
references.push(node.identifier.toLowerCase());
if (node.type === "html" && typeof node.value === "string") {
const html = node.value.replace(/<!--[\s\S]*?(?:-->|$)/g, "");
for (const match of html.matchAll(
/<img\b[^>]{0,8192}?\bsrc\s*=\s*(?:"([^"]*)"|'([^']*)')[^>]*>/gi,
))
add(match[1] ?? match[2]);
}
if (Array.isArray(node.children))
for (let index = node.children.length - 1; index >= 0; index--)
stack.push(node.children[index]);
}
for (const id of references) add(definitions.get(id));
restoreGitHubAttachmentLimitOmissions(
message,
Math.max(0, urls.size - MAX_ATTACHMENTS),
);
const sourceBodySha256 = createHash("sha256")
.update(comment.body)
.digest("hex");
return [...urls]
.slice(0, MAX_ATTACHMENTS)
.map((url) =>
rehydrateGitHubPublicAttachment(
{
kind: "github_public_attachment",
url,
sourceThreadId: message.threadId,
sourceMessageId: message.id,
version: bodySource ? 3 : 2,
sourceBodySha256,
...(bodySource ? { sourceKind: raw.type, sourceId: String(comment.id) } : {}),
},
{ threadId: message.threadId, messageId: message.id },
)!,
)
.filter(Boolean);
}
function allowedRedirect(value: string, original: string): URL | null {
try {
const url = new URL(value, original);
if (
url.protocol !== "https:" ||
url.port ||
url.username ||
url.password ||
url.hash ||
url.href.length > 8192
)
return null;
if (url.hostname === "github.com")
return canonicalGitHubAttachmentUrl(url.href) ? url : null;
if (!CDN_HOSTS.has(url.hostname) || url.pathname === "/") return null;
return url;
} catch {
return null;
}
}
/** Exact documented source route; no caller-supplied API origin or query. */
export function githubAttachmentCommentRequest(
attachment: Attachment,
): GitHubAttachmentCommentRequest | null {
const locator = handles.get(attachment);
if (![2, 3].includes(locator?.version ?? 0) || !locator?.sourceBodySha256) return null;
const thread =
/^github:([^/:]+)\/([^:]+):(?:(issue):)?([1-9][0-9]*)(?::rc:([1-9][0-9]*))?$/i.exec(
locator.sourceThreadId,
);
if (
!thread ||
!/^[a-z0-9][a-z0-9-]{0,38}$/i.test(thread[1]!) ||
!/^[a-z0-9_.-]{1,100}$/i.test(thread[2]!) ||
[".", ".."].includes(thread[2]!)
)
return null;
if (locator.version === 3) {
if (thread[5] || Boolean(thread[3]) !== (locator.sourceKind === "issue_description")) return null;
return {
url: `https://api.github.com/repos/${thread[1]}/${thread[2]}/${thread[3] ? "issues" : "pulls"}/${thread[4]}${locator.sourceKind === "review_summary" ? `/reviews/${locator.sourceId}` : ""}`,
accept: locator.sourceKind === "review_summary" ? "application/vnd.github-commitcomment.full+json" : "application/vnd.github.full+json",
};
}
return {
url: `https://api.github.com/repos/${thread[1]}/${thread[2]}/${thread[5] ? "pulls" : "issues"}/comments/${locator.sourceMessageId}`,
accept: thread[5]
? "application/vnd.github-commitcomment.full+json"
: "application/vnd.github.full+json",
};
}
const MAX_COMMENT_RESPONSE_BYTES = 1_048_576;
/** Octokit's authenticated request may go only to this one fixed API route. */
export function githubAttachmentCommentFetch(
expected: GitHubAttachmentCommentRequest,
signal: AbortSignal,
): typeof fetch {
return async (input, init) => {
if (
!isGitHubAttachmentCommentRequest(expected) ||
typeof input !== "string" ||
input !== expected.url ||
init?.method !== "GET"
)
throw new GitHubAttachmentUnavailableError(
"github_attachment_source_mismatch",
);
signal.throwIfAborted();
const headers = new Headers(init.headers);
if (headers.get("accept") !== expected.accept || headers.has("cookie"))
throw new GitHubAttachmentUnavailableError(
"github_attachment_source_mismatch",
);
const response = await guardedRemoteHttpFetch(
expected.url,
{
...init,
method: "GET",
headers,
credentials: "omit",
redirect: "manual",
signal,
},
{
allowPrivateNetwork: false,
connectTimeoutMs: 5000,
responseTimeoutMs: DOWNLOAD_TIMEOUT_MS,
error: () =>
new GitHubAttachmentUnavailableError(
"github_attachment_canonical_api_request_failed",
),
},
);
if (
response.status !== 200 ||
!response.body ||
!/^application\/json(?:;|$)/i.test(
response.headers.get("content-type") ?? "",
) ||
Number(response.headers.get("content-length") ?? 0) >
MAX_COMMENT_RESPONSE_BYTES
) {
await response.body?.cancel();
throw new GitHubAttachmentUnavailableError(
[401, 403, 404].includes(response.status)
? "github_attachment_canonical_api_access_denied"
: response.status !== 200
? "github_attachment_canonical_api_status_unexpected"
: Number(response.headers.get("content-length") ?? 0) >
MAX_COMMENT_RESPONSE_BYTES
? "github_attachment_canonical_api_too_large"
: "github_attachment_canonical_api_invalid_response",
);
}
const reader = response.body.getReader();
const chunks: Uint8Array[] = [];
let size = 0;
const cancel = () => {
void reader.cancel().catch(() => undefined);
};
signal.addEventListener("abort", cancel, { once: true });
try {
for (;;) {
signal.throwIfAborted();
const next = await reader.read();
signal.throwIfAborted();
if (next.done) break;
size += next.value.byteLength;
if (size > MAX_COMMENT_RESPONSE_BYTES)
throw new GitHubAttachmentUnavailableError(
"github_attachment_canonical_api_too_large",
);
chunks.push(next.value);
}
} finally {
signal.removeEventListener("abort", cancel);
await reader.cancel().catch(() => undefined);
}
return new Response(Buffer.concat(chunks), {
status: 200,
headers: { "content-type": "application/json" },
});
};
}
/**
* The authenticated rendering is evidence only for this exact unchanged source.
* We support GitHub's image anchor mapping, not arbitrary HTML URL extraction.
* No signed URL is returned to the model or added to durable descriptors.
*/
export function resolveGitHubCommentAttachmentTarget(
attachment: Attachment,
value: unknown,
): URL | null {
try {
return resolveCanonicalAttachmentTargetOrThrow(attachment, value);
} catch (error) {
if (error instanceof GitHubAttachmentUnavailableError) return null;
throw error;
}
}
function resolveCanonicalAttachmentTargetOrThrow(
attachment: Attachment,
value: unknown,
): URL {
const locator = handles.get(attachment);
const request = githubAttachmentCommentRequest(attachment);
if (
!locator ||
!request ||
!value ||
typeof value !== "object" ||
Array.isArray(value)
)
throw new GitHubAttachmentUnavailableError(
"github_attachment_canonical_response_unavailable",
);
const row = value as Record<string, unknown>;
const thread =
/^github:([^:]+):(?:(issue):)?([1-9][0-9]*)(?::rc:([1-9][0-9]*))?$/i.exec(
locator.sourceThreadId,
)!;
if (
String(row.id) !== (locator.sourceId ?? locator.sourceMessageId) ||
(locator.sourceKind === "review_summary" && row.url === undefined ? false :
typeof row.url !== "string" || row.url.toLowerCase() !== request.url.toLowerCase())
)
throw new GitHubAttachmentUnavailableError(
"github_attachment_canonical_source_mismatch",
);
if (
typeof row.body !== "string" ||
row.body.length > 200_000 ||
createHash("sha256").update(row.body).digest("hex") !==
locator.sourceBodySha256
)
throw new GitHubAttachmentUnavailableError(
"github_attachment_canonical_body_mismatch",
);
if (typeof row.body_html !== "string" || row.body_html.length > 600_000)
throw new GitHubAttachmentUnavailableError(
"github_attachment_canonical_html_unavailable",
);
if (locator.version === 3) {
if (locator.sourceKind === "review_summary" ? row.pull_request_url !==
`https://api.github.com/repos/${thread[1]}/pulls/${thread[3]}` :
row.number !== Number(thread[3]) || (locator.sourceKind === "issue_description" && row.pull_request !== undefined))
throw new GitHubAttachmentUnavailableError("github_attachment_canonical_source_mismatch");
} else if (thread[4]) {
if (
row.pull_request_url !==
`https://api.github.com/repos/${thread[1]}/pulls/${thread[3]}` ||
String(row.in_reply_to_id ?? row.id) !== thread[4]
)
throw new GitHubAttachmentUnavailableError(
"github_attachment_canonical_source_mismatch",
);
} else if (
row.issue_url !==
`https://api.github.com/repos/${thread[1]}/issues/${thread[3]}`
)
throw new GitHubAttachmentUnavailableError(
"github_attachment_canonical_source_mismatch",
);
const assetId = /\/assets\/([a-f0-9-]+)$/i.exec(locator.url)?.[1];
// Generic private files have no documented signed-download representation.
if (!assetId)
throw new GitHubAttachmentUnavailableError(
"github_attachment_canonical_file_unsupported",
);
const sourceBody = row.body;
const imagePath = new RegExp(
`^/[1-9][0-9]*/[1-9][0-9]*-${assetId}\\.(?:png|jpe?g|gif|webp)$`,
"i",
);
const sameAssetImage = (src: string): URL | null => {
const target = allowedRedirect(src, locator.url);
return target?.hostname === "private-user-images.githubusercontent.com" &&
imagePath.test(target.pathname)
? target
: null;
};
const signedImage = (src: string): URL | null => {
const target = sameAssetImage(src);
return target &&
[...target.searchParams.keys()].join(",") === "jwt" &&
/^[a-z0-9_-]+\.[a-z0-9_-]+\.[a-z0-9_-]+$/i.test(
target.searchParams.get("jwt") ?? "",
) &&
!sourceBody.includes(src)
? target
: null;
};
const fragment = JSDOM.fragment(row.body_html);
const candidates: URL[] = [];
for (const anchor of fragment.querySelectorAll("a[href]")) {
const href = anchor.getAttribute("href")!;
const images = anchor.querySelectorAll("img[src]");
if (
href !== locator.url &&
!sameAssetImage(href) &&
![...images].some((image) => sameAssetImage(image.getAttribute("src")!))
)
continue;
if (images.length !== 1)
throw new GitHubAttachmentUnavailableError(
"github_attachment_canonical_image_count_invalid",
);
const src = images[0]!.getAttribute("src")!;
const target = signedImage(src);
// The second form was observed in the exact App-rendered live comment.
// Both the original-anchor and signed-anchor forms enter one candidate set
// so duplicated or mixed renderings cannot silently choose a target.
if (
!target ||
(href !== locator.url && (href !== src || !signedImage(href)))
)
throw new GitHubAttachmentUnavailableError(
"github_attachment_canonical_target_denied",
);
candidates.push(target);
}
const sameAssetImages = [...fragment.querySelectorAll("img[src]")].filter(
(image) =>
image.getAttribute("src") === locator.url ||
sameAssetImage(image.getAttribute("src")!),
);
if (candidates.length > 1 || sameAssetImages.length > 1)
throw new GitHubAttachmentUnavailableError(
"github_attachment_canonical_mapping_ambiguous",
);
if (candidates.length === 1 && sameAssetImages.length === 1)
return candidates[0]!;
if (
[...fragment.querySelectorAll("img[src]")].some((image) =>
signedImage(image.getAttribute("src")!),
)
)
throw new GitHubAttachmentUnavailableError(
"github_attachment_canonical_image_without_source_anchor",
);
throw new GitHubAttachmentUnavailableError(
"github_attachment_canonical_anchor_missing",
);
}
function imageSignatureMatches(body: Buffer, mime: string): boolean {
if (mime === "image/png")
return body
.subarray(0, 8)
.equals(Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]));
if (mime === "image/jpeg" || mime === "image/jpg")
return body[0] === 255 && body[1] === 216 && body[2] === 255;
if (mime === "image/gif")
return /^(GIF87a|GIF89a)$/.test(body.subarray(0, 6).toString("ascii"));
if (mime === "image/webp")
return (
body.subarray(0, 4).toString("ascii") === "RIFF" &&
body.subarray(8, 12).toString("ascii") === "WEBP"
);
return !mime.startsWith("image/");
}
/** Download hosts never receive provider credentials, including after canonical resolution. */
export async function prepareGitHubPublicAttachment(
attachment: Attachment,
batchSignal?: AbortSignal,
resolveComment?: GitHubAttachmentCommentResolver,
): Promise<Attachment> {
const locator = handles.get(attachment);
if (!locator)
throw new GitHubAttachmentUnavailableError(
"github_attachment_source_mismatch",
);
const downloadSignal = AbortSignal.timeout(DOWNLOAD_TIMEOUT_MS);
const signal = batchSignal
? AbortSignal.any([downloadSignal, batchSignal])
: downloadSignal;
try {
let url = new URL(locator.url);
let resolvedCanonicalComment = false;
for (let redirects = 0; redirects <= 3; redirects++) {
signal.throwIfAborted();
const response = await guardedRemoteHttpFetch(
url,
{
method: "GET",
redirect: "manual",
credentials: "omit",
signal,
headers: { accept: "*/*", "user-agent": "Paperclip/ChatAttachments" },
},
{
allowPrivateNetwork: false,
connectTimeoutMs: 5000,
responseTimeoutMs: DOWNLOAD_TIMEOUT_MS,
error: () =>
new GitHubAttachmentUnavailableError(
"github_attachment_download_failed",
),
},
);
if (REDIRECT_STATUSES.has(response.status)) {
const target = allowedRedirect(
response.headers.get("location") ?? "",
url.href,
);
await response.body?.cancel();
if (!target || redirects === 3)
throw new GitHubAttachmentUnavailableError(
"github_attachment_unsafe_redirect",
);
url = target;
continue;
}
const rejectResponse = async (
code: GitHubAttachmentUnavailableError["code"],
): Promise<never> => {
await response.body?.cancel();
throw new GitHubAttachmentUnavailableError(code);
};
if (
response.status === 401 ||
response.status === 403 ||
response.status === 404
) {
const commentRequest = githubAttachmentCommentRequest(attachment);
if (!resolvedCanonicalComment && resolveComment && commentRequest) {
await response.body?.cancel();
resolvedCanonicalComment = true;
signal.throwIfAborted();
const canonical = await resolveComment(commentRequest, signal);
signal.throwIfAborted();
const target = resolveCanonicalAttachmentTargetOrThrow(
attachment,
canonical,
);
url = target;
continue;
}
return await rejectResponse("github_attachment_not_public");
}
if (response.status !== 200 || !response.body)
return await rejectResponse("github_attachment_invalid_response");
const mimeType = normalizeUploadAttachmentContentType({
contentType: normalizeContentType(response.headers.get("content-type")),
originalFilename: attachment.name,
isAllowedContentType,
});
// A login/error document is never a successfully downloaded attachment.
if (mimeType === "text/html" || !isAllowedContentType(mimeType))
return await rejectResponse("github_attachment_unsupported_type");
const declared = response.headers.get("content-length");
if (
declared &&
(!/^\d+$/.test(declared) || Number(declared) > MAX_ATTACHMENT_BYTES)
)
return await rejectResponse("github_attachment_too_large");
const reader = response.body.getReader();
const chunks: Buffer[] = [];
let size = 0;
const cancel = () => {
void reader.cancel().catch(() => undefined);
};
signal.addEventListener("abort", cancel, { once: true });
try {
for (;;) {
signal.throwIfAborted();
const next = await reader.read();
signal.throwIfAborted();
if (next.done) break;
size += next.value.byteLength;
if (size > MAX_ATTACHMENT_BYTES)
throw new GitHubAttachmentUnavailableError(
"github_attachment_too_large",
);
chunks.push(Buffer.from(next.value));
}
} finally {
signal.removeEventListener("abort", cancel);
await reader.cancel().catch(() => undefined);
}
if (!size)
throw new GitHubAttachmentUnavailableError("github_attachment_empty");
// Content-Length describes compressed bytes when Content-Encoding is present.
if (
declared &&
!response.headers.get("content-encoding") &&
size !== Number(declared)
)
throw new GitHubAttachmentUnavailableError(
"github_attachment_invalid_response",
);
const body = Buffer.concat(chunks, size);
if (
!imageSignatureMatches(body, mimeType) ||
/^\s*(?:<!doctype\s+html|<html\b)/i.test(
body.subarray(0, 512).toString("utf8"),
)
)
throw new GitHubAttachmentUnavailableError(
"github_attachment_invalid_response",
);
const name = attachment.name?.startsWith("github-attachment-")
? `${attachment.name}${MIME_EXTENSIONS[mimeType] ?? ""}`
: attachment.name;
return {
type: mimeType.startsWith("image/")
? "image"
: mimeType.startsWith("audio/")
? "audio"
: mimeType.startsWith("video/")
? "video"
: "file",
name,
mimeType,
size,
fetchData: async () => body,
};
}
throw new GitHubAttachmentUnavailableError(
"github_attachment_unsafe_redirect",
);
} catch (error) {
if (error instanceof GitHubAttachmentUnavailableError) throw error;
throw new GitHubAttachmentUnavailableError(
"github_attachment_download_failed",
);
}
}