Files
anonymous_github/test/gist-binary-content.test.js
T
Thomas DurieuxandClaude Opus 5.5 023e3f9a51 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>
2026-11-29 12:33:11 +00:00

86 lines
4.3 KiB
JavaScript

const { expect } = require("chai");
require("ts-node/register/transpile-only");
const { default: Gist } = require("../src/core/Gist");
function makeGist(options) {
return new Gist({
gistId: "g-test",
owner: "000000000000000000000001",
source: { gistId: "abc" },
options: { terms: ["Alice"], content: true, title: true, image: true, ...options },
gist: {
description: "By Alice",
files: [
{ filename: "notes-Alice.md", content: "Written by Alice", language: "Markdown", size: 16 },
{ filename: "plot-Alice.png", content: "iVBORw0KGgoAlice==", encoding: "base64", size: 12, type: "image/png" },
{ filename: "paper.pdf", content: "JVBERi0xLjQK", encoding: "base64", size: 9, type: "application/pdf" },
{ filename: "huge.bin", content: "", encoding: "base64", size: 99999999, tooLarge: true },
],
},
});
}
describe("Gist.content() binary files", 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==" });
// 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 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: "" });
});
});