mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-09-15 07:05:26 +02:00
fix: omit source content length for rewritten file responses
This commit is contained in:
@@ -477,10 +477,6 @@ export default class AnonymizedFile {
|
|||||||
if (!mime && data.isText) {
|
if (!mime && data.isText) {
|
||||||
res.contentType("text/plain");
|
res.contentType("text/plain");
|
||||||
}
|
}
|
||||||
if (!data.wasAnonimized && this._file?.size) {
|
|
||||||
// the text files may be anonymized and therefore the size may be different
|
|
||||||
res.header("Content-Length", this._file?.size.toString());
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
const content = await this.content();
|
const content = await this.content();
|
||||||
function handleStreamError(error: Error) {
|
function handleStreamError(error: Error) {
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
const { setTimeout, setImmediate } = require("timers");
|
||||||
|
const { expect } = require("chai");
|
||||||
|
require("ts-node/register/transpile-only");
|
||||||
|
const { Readable } = require("stream");
|
||||||
|
const { once } = require("events");
|
||||||
|
const config = require("../src/config").default;
|
||||||
|
const db = require("../src/server/database");
|
||||||
|
const gh = require("../src/core/GitHubUtils");
|
||||||
|
const RepoModel = require("../src/core/model/anonymizedRepositories/anonymizedRepositories.model").default;
|
||||||
|
const Repository = require("../src/core/Repository").default;
|
||||||
|
const GitHubStream = require("../src/core/source/GitHubStream").default;
|
||||||
|
const { ContentAnonimizer, AnonymizeTransformer, anonymizePath } = require("../src/core/anonymize-utils");
|
||||||
|
|
||||||
|
async function collect(stream) {
|
||||||
|
const chunks = [];
|
||||||
|
for await (const chunk of stream) chunks.push(Buffer.from(chunk));
|
||||||
|
return Buffer.concat(chunks).toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("production regressions", function () {
|
||||||
|
const restores = [];
|
||||||
|
function stub(object, key, value) {
|
||||||
|
const old = object[key]; restores.push(() => { object[key] = old; }); object[key] = value;
|
||||||
|
}
|
||||||
|
afterEach(() => { while (restores.length) restores.pop()(); });
|
||||||
|
it("omits an upstream length when later text is rewritten", async function () {
|
||||||
|
const File = require("../src/core/AnonymizedFile").default;
|
||||||
|
stub(config, "STREAMER_ENTRYPOINT", "");
|
||||||
|
const repo = new Repository(new RepoModel({ repoId: "repo", options: { terms: ["Alice"], image: true, link: true }, source: {} }));
|
||||||
|
const file = new File({ repository: repo, anonymizedPath: "file.txt" });
|
||||||
|
const input = "z".repeat(9000) + " Alice";
|
||||||
|
file._file = { name: "file.txt", path: "", size: input.length };
|
||||||
|
file.content = async () => Readable.from([Buffer.from(input.slice(0, 5000)), Buffer.from(input.slice(5000))]);
|
||||||
|
const res = new (require("stream").PassThrough)();
|
||||||
|
const headers = {};
|
||||||
|
res.header = (key, value) => { headers[key] = value; return res; };
|
||||||
|
res.contentType = () => res;
|
||||||
|
const output = collect(res);
|
||||||
|
await file.send(res);
|
||||||
|
expect(await output).to.equal("z".repeat(9000) + " XXXX-1");
|
||||||
|
expect(headers).not.to.have.property("Content-Length");
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user