mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-11-18 01:54:23 +01:00
feat(ui): browse and check every file in the anonymize preview (#950)
* feat(ui): browse and check every file in the anonymize preview
The "What reviewers will see" window now redacts the README GitHub actually
shows and lets authors open any file of the commit.
- README: resolved from the file list like GitHub (.github/, root, docs/,
any case, Markdown first) and only that path is tested against the
"Leave files unredacted" rules. Before the list loads, only root names are
assumed, so leaving docs/ unredacted no longer unredacts the root README.
The file bar shows the README's real name.
- New GET /api/repo/:owner/:repo/file?commit=&path= (same access checks as
/tree): raw text, no-store, path traversal rejected, 1 MB cap
(file_too_big, 413), binary files reported as { binary: true }.
/tree also lists binary files (binaryFiles) for the preview's tree.
- Tree rows open files (keyboard accessible, explorer active rail). Text is
redacted through /api/anonymize-preview by the same batcher as the README;
files left unredacted are sent without terms and tagged "Left unredacted"
(opens the dialog). Markdown renders like the README, code is highlighted
with Prism, images/PDF/binary/too-large files get a short note. Loading
and error (Retry) states reuse rp-state.
- File and folder names are redacted through /api/anonymize-preview.
- Caption counts the open file; "<name> left unredacted" when preserved.
- Phones: the Files button opens the tree as a sheet.
- dev-mock: a jsmith/robust-repair source with tree, file and
anonymize-preview endpoints and fixtures carrying the mock's terms.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* refactor(ui): reuse the explorer in the anonymize preview
Share the explorer file renderer and tree with the author preview. Apply
preserved-file rules on the server and keep the reviewer rendering and
redaction slots consistent across both views.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix(ui): keep the reviewer page unchanged by the preview reuse
The preview reuses tree.js and the shared file viewer; its differences now
apply only inside the anonymize preview.
- The narrow-pane rule that hid the file meta and Symbols applied to the
reviewer page too (visible with a widened sidebar); it is scoped to
.reviewer-preview. The token capture moves from body to the page that
holds the preview.
- tree.js: aria-expanded/aria-current and tree-only scrolling apply only
when the tree is embedded (selectedPath set); the reviewer page keeps
scrollIntoView and its original markup.
- file-viewer: the extra menu-closing click listener, the #L hash writes and
the Ctrl+F/G and Alt+Z shortcuts are limited to the preview (shortcuts
only while its code has focus), so the form keeps the browser's Find.
- Preview: the tree's file list is cached between renders, name batches are
chunked under the JSON body limit, /api/anonymize-preview accepts up to
3 MB so files up to the 1 MB preview limit can be redacted, and on phones
the caption's "<name> left unredacted" opens the dialog (the header tag
squeezed the path out).
- dev-mock: long and nested names for the tree.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix(ui): restore the redaction number on the right and level the slots
The redesign centered the term number in every redaction slot; it sits on
the right edge again, in code, Markdown, the tree and the breadcrumb. Tree,
breadcrumb and paragraph slots also sat about 2px below the text; they now
center on the line's letters.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix: address preview navigation and path review findings
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d508ab1754
commit
476f25e264
20 files changed
+1794
-941
No files matched your search
@@ -1,12 +1,14 @@
|
||||
import { octokit } from "./GitHubUtils";
|
||||
import AnonymousError from "./AnonymousError";
|
||||
import { classifyByName } from "./anonymize-utils";
|
||||
import { classifyByName, isTextFile } from "./anonymize-utils";
|
||||
|
||||
/** Enough for the picker; larger repositories fall back to patterns. */
|
||||
export const SOURCE_FILE_LIST_LIMIT = 20000;
|
||||
|
||||
export interface SourceFileList {
|
||||
files: string[];
|
||||
/** Known binary files (images, PDFs, archives): listed in the preview's tree, never redacted. */
|
||||
binaryFiles: string[];
|
||||
truncated: boolean;
|
||||
}
|
||||
|
||||
@@ -43,11 +45,76 @@ export async function sourceFileList(opt: {
|
||||
throw error;
|
||||
}
|
||||
const files: string[] = [];
|
||||
const binaryFiles: string[] = [];
|
||||
for (const entry of data.tree) {
|
||||
if (entry.type !== "blob" || !entry.path) continue;
|
||||
if (entry.path === ".gitmodules" || classifyByName(entry.path) === false) continue;
|
||||
if (files.length === SOURCE_FILE_LIST_LIMIT) return { files: files.sort(), truncated: true };
|
||||
files.push(entry.path);
|
||||
if (entry.type !== "blob" || !entry.path || entry.path === ".gitmodules") continue;
|
||||
const list = classifyByName(entry.path) === false ? binaryFiles : files;
|
||||
if (files.length + binaryFiles.length === SOURCE_FILE_LIST_LIMIT) {
|
||||
return { files: files.sort(), binaryFiles: binaryFiles.sort(), truncated: true };
|
||||
}
|
||||
list.push(entry.path);
|
||||
}
|
||||
return { files: files.sort(), truncated: !!data.truncated };
|
||||
return { files: files.sort(), binaryFiles: binaryFiles.sort(), truncated: !!data.truncated };
|
||||
}
|
||||
|
||||
/** Largest file the anonymize preview reads from GitHub (the contents API's inline limit). */
|
||||
export const SOURCE_FILE_PREVIEW_LIMIT = 1024 * 1024;
|
||||
|
||||
/**
|
||||
* A repository-relative path as the tree API returns it: no leading or
|
||||
* trailing slash, no empty, "." or ".." segment, no backslash or control
|
||||
* character.
|
||||
*/
|
||||
export function isSafeSourcePath(path: unknown): path is string {
|
||||
if (typeof path !== "string" || !path || path.length > 4096) return false;
|
||||
// eslint-disable-next-line no-control-regex
|
||||
if (/[\\\u0000-\u001f]/.test(path)) return false;
|
||||
return path.split("/").every((part) => part !== "" && part !== "." && part !== "..");
|
||||
}
|
||||
|
||||
export type SourceFileContent =
|
||||
| { binary: false; content: string; size: number }
|
||||
| { binary: true; size: number };
|
||||
|
||||
/**
|
||||
* One file of a commit, for the anonymize preview's file viewer. The text is
|
||||
* returned as is: the caller redacts it through /api/anonymize-preview, like
|
||||
* the README. Binary files are reported, not sent.
|
||||
*/
|
||||
export async function sourceFileContent(opt: {
|
||||
owner: string;
|
||||
repo: string;
|
||||
commit: string;
|
||||
path: string;
|
||||
accessToken: string;
|
||||
}): Promise<SourceFileContent> {
|
||||
if (!isSafeSourcePath(opt.path)) throw new AnonymousError("invalid_path", { httpStatus: 400 });
|
||||
let data;
|
||||
try {
|
||||
({ data } = await octokit(opt.accessToken).repos.getContent({
|
||||
owner: opt.owner,
|
||||
repo: opt.repo,
|
||||
path: opt.path,
|
||||
ref: opt.commit,
|
||||
mediaType: { format: "object" },
|
||||
}));
|
||||
} catch (error) {
|
||||
const status = (error as { status?: number }).status;
|
||||
if (status === 404 || status === 422) {
|
||||
throw new AnonymousError("file_not_found", { httpStatus: 404, cause: error as Error });
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
const file = data as { type?: string; size?: number; content?: string; encoding?: string };
|
||||
if (Array.isArray(data) || file.type !== "file") {
|
||||
throw new AnonymousError("file_not_found", { httpStatus: 404 });
|
||||
}
|
||||
const size = file.size || 0;
|
||||
if (classifyByName(opt.path) === false) return { binary: true, size };
|
||||
if (size > SOURCE_FILE_PREVIEW_LIMIT || (size > 0 && file.encoding === "none")) {
|
||||
throw new AnonymousError("file_too_big", { httpStatus: 413 });
|
||||
}
|
||||
const buffer = Buffer.from(file.content || "", file.encoding === "base64" ? "base64" : "utf8");
|
||||
if (buffer.length && !isTextFile(opt.path, buffer)) return { binary: true, size };
|
||||
return { binary: false, content: buffer.toString("utf8"), size };
|
||||
}
|
||||
+5
-1
@@ -126,7 +126,11 @@ export default async function start() {
|
||||
app.use("/service", createReviewCapabilities(process.env.REVIEW_SERVICE_KEYS));
|
||||
app.use("/github/app/webhook", githubAppWebhook);
|
||||
app.use("/api/billing/webhook", billingWebhook);
|
||||
app.use(express.json());
|
||||
// The anonymize preview posts whole source files (up to the 1 MB preview
|
||||
// limit, plus JSON escaping); every other route keeps the default limit.
|
||||
const previewJson = express.json({ limit: "3mb" });
|
||||
const defaultJson = express.json();
|
||||
app.use((req, res, next) => (req.path === "/api/anonymize-preview" ? previewJson : defaultJson)(req, res, next));
|
||||
// Preserve the empty body used by API validation when no JSON was parsed.
|
||||
app.use((req, _res, next) => {
|
||||
req.body ??= {};
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import * as express from "express";
|
||||
import { isPreservedPath, normalizePreserveFiles } from "../../core/preserve-files";
|
||||
import { isSafeSourcePath } from "../../core/source-file-list";
|
||||
import { ContentAnonimizer } from "../../core/anonymize-utils";
|
||||
import { handleError } from "./route-utils";
|
||||
import { ensureAuthenticated } from "./connection";
|
||||
@@ -21,6 +23,8 @@ router.post("/", async (req: express.Request, res: express.Response) => {
|
||||
contents?: unknown;
|
||||
options?: {
|
||||
terms?: string[];
|
||||
filePath?: string;
|
||||
preserveFiles?: string[];
|
||||
image?: boolean;
|
||||
link?: boolean;
|
||||
repoName?: string;
|
||||
@@ -44,11 +48,20 @@ router.post("/", async (req: express.Request, res: express.Response) => {
|
||||
}
|
||||
|
||||
const opt = body.options || {};
|
||||
const preserveFiles = normalizePreserveFiles(opt.preserveFiles);
|
||||
if (!preserveFiles) return res.status(400).json({ error: "invalid_preserve_files_format" });
|
||||
if (opt.filePath !== undefined && (typeof opt.filePath !== "string" || !isSafeSourcePath(opt.filePath))) {
|
||||
return res.status(400).json({ error: "invalid_path" });
|
||||
}
|
||||
// Identical to Anonymize: preserving a file disables term replacement only.
|
||||
// Image/link options and self-link rewriting still apply.
|
||||
const terms = isPreservedPath(opt.filePath, preserveFiles) ? [] : opt.terms;
|
||||
res.set("Cache-Control", "private, no-store");
|
||||
// Construct one anonymizer per snippet so the wasAnonymized flag is per
|
||||
// input. ContentAnonimizer is cheap to instantiate.
|
||||
const outputs = inputs.map((content) => {
|
||||
const a = new ContentAnonimizer({
|
||||
terms: Array.isArray(opt.terms) ? opt.terms : [],
|
||||
terms: Array.isArray(terms) ? terms : [],
|
||||
image: opt.image,
|
||||
link: opt.link,
|
||||
repoName: opt.repoName,
|
||||
|
||||
@@ -31,7 +31,7 @@ import { RepositoryStatus } from "../../core/types";
|
||||
import { octokit, getRedisGateResetAt, getToken } from "../../core/GitHubUtils";
|
||||
import { createLogger } from "../../core/logger";
|
||||
import { normalizePreserveFiles } from "../../core/preserve-files";
|
||||
import { sourceFileList } from "../../core/source-file-list";
|
||||
import { sourceFileContent, sourceFileList, isSafeSourcePath } from "../../core/source-file-list";
|
||||
|
||||
const logger = createLogger("route:repo");
|
||||
|
||||
@@ -380,6 +380,34 @@ router.get(
|
||||
}
|
||||
);
|
||||
|
||||
// One file of a source commit, for the anonymize preview's file viewer. The
|
||||
// raw text is returned; the preview redacts it through /api/anonymize-preview.
|
||||
// Binary files are reported as { binary: true, size } instead of being sent.
|
||||
router.get(
|
||||
"/:owner/:repo/file",
|
||||
async (req, res) => {
|
||||
try {
|
||||
const commit = typeof req.query.commit === "string" ? req.query.commit : "";
|
||||
if (!/^[0-9a-f]{6,40}$/i.test(commit)) throw appError("invalid_commit_format", 400);
|
||||
if (!isSafeSourcePath(req.query.path)) throw appError("invalid_path", 400);
|
||||
const token = await previewToken(req);
|
||||
const file = await sourceFileContent({
|
||||
owner: req.params.owner,
|
||||
repo: req.params.repo,
|
||||
commit,
|
||||
path: req.query.path,
|
||||
accessToken: token,
|
||||
});
|
||||
res.set("Cache-Control", "private, no-store");
|
||||
res.set("X-Content-Type-Options", "nosniff");
|
||||
if (file.binary) return res.json({ binary: true, size: file.size });
|
||||
res.type("text/plain; charset=utf-8").send(file.content);
|
||||
} catch (error) {
|
||||
handleError(error, res, req);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// get repository information
|
||||
router.get("/:repoId/", async (req, res) => {
|
||||
try {
|
||||
|
||||
Reference in new issue
Block a user