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: "" }); }); });