mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-12-03 09:33:47 +01:00
fix(viewers): keep authors out of PR commits, branches and gist binaries
- Commit messages lose sign-off and co-author trailers, e-mail addresses and the fork owner in merge headlines; without the origin option merge headlines lose branch names too. - Branch names are shown only with the origin option. - Gists keep only images (hidden unless images are displayed); other binaries are never stored or served. Images share an 8 MB budget so the gist document stays under MongoDB's limit, and an unreadable image no longer fails the whole gist. - Review threads match the diff side, and outdated comments go to the end of their file instead of an unrelated line. - The diff parser no longer reads a removed '-- ' line as a file header. - Each diff file renders as its own component from escaped HTML, so large diffs don't re-render on scroll or collapse. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
98bf382fa8
commit
023e3f9a51
16 files changed
+300
-128
No files matched your search
@@ -21,20 +21,65 @@ function makeGist(options) {
|
||||
}
|
||||
|
||||
describe("Gist.content() binary files", function () {
|
||||
it("anonymizes text files and names, and passes binary bytes through unchanged", function () {
|
||||
it("anonymizes text files and names, passes image bytes through, and never serves other binaries", function () {
|
||||
const [md, png, pdf, huge] = makeGist().content().files;
|
||||
expect(md).to.include({ filename: "notes-XXXX-1.md", content: "Written by XXXX-1" });
|
||||
expect(md).to.not.have.property("encoding");
|
||||
expect(png).to.include({ filename: "plot-XXXX-1.png", encoding: "base64", content: "iVBORw0KGgoAlice==" });
|
||||
expect(pdf).to.include({ encoding: "base64", content: "JVBERi0xLjQK" });
|
||||
// A PDF stored by an older version keeps its author metadata: never served.
|
||||
expect(pdf).to.include({ encoding: "base64" });
|
||||
expect(pdf.content).to.be.undefined;
|
||||
expect(huge).to.include({ tooLarge: true, encoding: "base64" });
|
||||
expect(huge.content).to.be.undefined;
|
||||
});
|
||||
|
||||
it("withholds images when images are not displayed, but keeps other binaries", function () {
|
||||
const [, png, pdf] = makeGist({ image: false }).content().files;
|
||||
expect(png.content).to.be.undefined;
|
||||
expect(png.hidden).to.equal(true);
|
||||
expect(pdf.content).to.equal("JVBERi0xLjQK");
|
||||
it("withholds images unless images are displayed, as for repositories", function () {
|
||||
for (const image of [false, undefined]) {
|
||||
const [, png] = makeGist({ image }).content().files;
|
||||
expect(png.content).to.be.undefined;
|
||||
expect(png.hidden).to.equal(true);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("Gist download of binary files", function () {
|
||||
const GitHubUtils = require("../src/core/GitHubUtils");
|
||||
let originalOctokit, originalFetch, fetched;
|
||||
beforeEach(() => { originalOctokit = GitHubUtils.octokit; originalFetch = global.fetch; fetched = []; });
|
||||
afterEach(() => { GitHubUtils.octokit = originalOctokit; global.fetch = originalFetch; });
|
||||
|
||||
async function download(files, respond) {
|
||||
GitHubUtils.octokit = () => ({
|
||||
rest: { gists: { get: async () => ({ data: { files, owner: { login: "alice" } } }) } },
|
||||
paginate: async () => [],
|
||||
});
|
||||
global.fetch = async (url) => { fetched.push(url); return respond(url); };
|
||||
const gist = makeGist();
|
||||
gist._model.set = () => {}; gist._model.markModified = () => {};
|
||||
await gist["downloadContent"]("token");
|
||||
return Object.fromEntries(gist._gistPayload.files.map((f) => [f.filename, f]));
|
||||
}
|
||||
const raw = (name) => `https://gist.githubusercontent.com/alice/abc/raw/1/${name}`;
|
||||
const ok = (bytes) => ({ ok: true, status: 200, arrayBuffer: async () => bytes });
|
||||
|
||||
it("stores images only, within the per-gist budget, and skips unreadable ones", async function () {
|
||||
const MB = 1024 * 1024;
|
||||
const files = {
|
||||
"paper.pdf": { filename: "paper.pdf", size: 10, raw_url: raw("paper.pdf") },
|
||||
"a.png": { filename: "a.png", size: 3 * MB, raw_url: raw("a.png") },
|
||||
"b.png": { filename: "b.png", size: 3 * MB, raw_url: raw("b.png") },
|
||||
"c.png": { filename: "c.png", size: 3 * MB, raw_url: raw("c.png") },
|
||||
"big.png": { filename: "big.png", size: 5 * MB, raw_url: raw("big.png") },
|
||||
"gone.png": { filename: "gone.png", size: 10, raw_url: raw("gone.png") },
|
||||
"elsewhere.png": { filename: "elsewhere.png", size: 10, raw_url: "http://169.254.169.254/x.png" },
|
||||
};
|
||||
const stored = await download(files, (url) => url.endsWith("gone.png") ? { ok: false, status: 404 } : ok(new Uint8Array(8)));
|
||||
expect(fetched.some((u) => u.endsWith("paper.pdf"))).to.equal(false);
|
||||
expect(fetched.some((u) => u.includes("169.254"))).to.equal(false);
|
||||
expect(stored["paper.pdf"]).to.include({ encoding: "base64", content: "" });
|
||||
expect(stored["big.png"].tooLarge).to.equal(true);
|
||||
// 8 MB budget: two 3 MB images fit, the third does not.
|
||||
expect(["a.png", "b.png", "c.png"].filter((n) => stored[n].tooLarge)).to.have.length(1);
|
||||
expect(stored["gone.png"]).to.include({ content: "" });
|
||||
});
|
||||
});
|
||||
@@ -1,6 +1,6 @@
|
||||
const { expect } = require("chai");
|
||||
require("ts-node/register/transpile-only");
|
||||
const { default: PullRequest, reviewThreads } = require("../src/core/PullRequest");
|
||||
const { default: PullRequest, reviewThreads, commitMessage } = require("../src/core/PullRequest");
|
||||
|
||||
const at = (day) => new Date(`2026-03-${String(day).padStart(2, "0")}T10:00:00Z`);
|
||||
|
||||
@@ -38,11 +38,26 @@ function makePullRequest(options) {
|
||||
const all = { title: true, body: true, comments: true, diff: true, username: true, date: true, origin: false };
|
||||
|
||||
describe("PullRequest.content() commits, branches and review threads", function () {
|
||||
it("anonymizes the author and branch names", function () {
|
||||
it("anonymizes the author, and shows branch names only with the origin", function () {
|
||||
const out = makePullRequest(all).content();
|
||||
expect(out.author).to.equal("XXXX-1");
|
||||
expect(out.baseRef).to.equal("main");
|
||||
expect(out.headRef).to.equal("XXXX-1/fix-XXXX-2");
|
||||
expect(out).to.not.have.property("baseRef");
|
||||
expect(out).to.not.have.property("headRef");
|
||||
const withOrigin = makePullRequest({ ...all, origin: true }).content();
|
||||
expect(withOrigin.baseRef).to.equal("main");
|
||||
expect(withOrigin.headRef).to.equal("XXXX-1/fix-XXXX-2");
|
||||
});
|
||||
|
||||
it("strips people from commit messages even without matching terms", function () {
|
||||
const message = "Merge pull request #3 from asmith/fix-x\n\nFix x <asmith@uni.edu>\n\nSigned-off-by: Ann Smith <ann@uni.edu>\nCo-authored-by: Bo <bo@x.org>\n";
|
||||
expect(commitMessage(message, { origin: false })).to.equal("Merge pull request #3\n\nFix x");
|
||||
expect(commitMessage(message, { origin: true })).to.equal("Merge pull request #3 from fix-x\n\nFix x");
|
||||
expect(commitMessage("Merge branch 'main' of github.com:asmith/repo into dev", { origin: true })).to.equal("Merge branch 'main' into dev");
|
||||
expect(commitMessage("Merge branch 'asmith/x'", { origin: false })).to.equal("Merge branch");
|
||||
expect(commitMessage("Plain message", { origin: false })).to.equal("Plain message");
|
||||
const pr = makePullRequest(all);
|
||||
pr.model.pullRequest.commits = [{ message: "Merge pull request #3 from asmith/fix\n\nSigned-off-by: Ann Smith <ann@uni.edu>", author: "asmith", date: at(3) }];
|
||||
expect(pr.content().commits[0].message).to.equal("Merge pull request #3");
|
||||
});
|
||||
|
||||
it("hides the author without the username option", function () {
|
||||
|
||||
@@ -64,6 +64,26 @@ describe("Reviewer page outlines and diffs", function () {
|
||||
expect(files[0].lines[3]).to.include({ kind: "add", newNo: 2 });
|
||||
});
|
||||
|
||||
it("keeps removed '-- ' and added '++ ' lines inside a hunk as content", () => {
|
||||
const files = formatters.parseDiff([
|
||||
"diff --git a/q.sql b/q.sql", "--- a/q.sql", "+++ b/q.sql", "@@ -1,3 +1,3 @@", " select 1;", "--- old comment", "+++ new counter", " end;",
|
||||
"diff --git a/b.txt b/b.txt", "--- a/b.txt", "+++ b/b.txt", "@@ -1 +1 @@", "-x", "+y",
|
||||
].join("\n"));
|
||||
expect(files.map((f) => [f.path, f.additions, f.deletions])).to.deep.equal([["q.sql", 1, 1], ["b.txt", 1, 1]]);
|
||||
expect(files[0].lines.map((l) => l.text)).to.deep.equal(["@@ -1,3 +1,3 @@", "select 1;", "-- old comment", "++ new counter", "end;"]);
|
||||
});
|
||||
|
||||
it("places a LEFT thread on the removed line and a RIGHT one on the new line with the same number", () => {
|
||||
const state = { details: {
|
||||
diff: "--- a/a.py\n+++ b/a.py\n@@ -1,2 +1,2 @@\n-gone\n+added\n keep\n",
|
||||
reviewComments: [{ path: "a.py", line: 1, side: "LEFT", comments: [{ body: "old" }] }, { path: "a.py", line: 1, side: "RIGHT", comments: [{ body: "new" }] }],
|
||||
}, fmt: { date: (d) => d } };
|
||||
const scope = inScope(() => pages.setupPullRequestPage(state));
|
||||
const [file] = state.prFiles();
|
||||
expect(file.lines.filter((l) => l.threads).map((l) => [l.kind, l.threads[0].comments[0].body])).to.deep.equal([["remove", "old"], ["add", "new"]]);
|
||||
scope.stop();
|
||||
});
|
||||
|
||||
it("renders the legacy diff HTML from the same parser", () => {
|
||||
const html = formatters.diff("--- a/x\n+++ b/x\n@@ -1 +1 @@\n-a<b\n+b\n");
|
||||
expect(html).to.include('diff-file-status-modified').and.include("a<b");
|
||||
|
||||
+9
-7
@@ -3077,9 +3077,10 @@ describe("Vue 3 UI", function () {
|
||||
{ filename: "plot.svg", content: "<svg xmlns='http://www.w3.org/2000/svg' width='4' height='4'/>" },
|
||||
{ filename: "train.py", language: "Python", content: "print(1)\n" },
|
||||
{ filename: "fig.png", encoding: "base64", size: 70, content: png },
|
||||
{ filename: "talk.mp4", encoding: "base64", size: 4, content: "AAAAAA==" },
|
||||
{ filename: "beep.wav", encoding: "base64", size: 4, content: "AAAAAA==" },
|
||||
{ filename: "model.bin", encoding: "base64", size: 4, content: "AAECAw==" },
|
||||
// The API never sends bytes for binaries other than images.
|
||||
{ filename: "talk.mp4", encoding: "base64", size: 4 },
|
||||
{ filename: "beep.wav", encoding: "base64", size: 4 },
|
||||
{ filename: "model.bin", encoding: "base64", size: 4 },
|
||||
{ filename: "big.parquet", encoding: "base64", size: 400000000, tooLarge: true },
|
||||
{ filename: "photo.jpg", encoding: "base64", size: 10, hidden: true },
|
||||
{ filename: "empty.txt", content: "" },
|
||||
@@ -3101,10 +3102,11 @@ describe("Vue 3 UI", function () {
|
||||
expect(body(3).querySelector("img.image-content").getAttribute("src")).to.match(/^blob:/);
|
||||
expect(body(4).querySelector("pre code")).to.not.equal(null);
|
||||
expect(body(5).querySelector("img.image-content")).to.not.equal(null);
|
||||
expect(body(6).querySelector("video.video-content")).to.not.equal(null);
|
||||
expect(body(7).querySelector("audio")).to.not.equal(null);
|
||||
expect(body(8).querySelector(".file-state-title").textContent).to.equal("No preview for this file");
|
||||
expect(body(8).querySelector("a[download]").getAttribute("download")).to.equal("model.bin");
|
||||
for (const i of [6, 7, 8]) {
|
||||
expect(body(i).querySelector(".file-state-title").textContent).to.equal("This file isn't shared");
|
||||
expect(body(i).querySelector("a[download]")).to.equal(null);
|
||||
expect(doc.querySelectorAll(".gist-file-card")[i].querySelector(".gist-file-actions")).to.equal(null);
|
||||
}
|
||||
expect(body(9).querySelector(".file-state-title").textContent).to.equal("This file is too large to preview");
|
||||
expect(body(10).querySelector(".file-state-title").textContent).to.equal("Image not shown");
|
||||
expect(body(11).querySelector(".file-state-title").textContent).to.equal("This file is empty");
|
||||
|
||||
Reference in new issue
Block a user