mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-05-15 14:38:03 +02:00
fix: include file path in cache ETag
Without the path, two different files in the same repo (same sha, same
anonymization options) shared an ETag. If a browser ever sent the cached
ETag for one file while requesting another, the server would have
returned 304 against the wrong cache entry. Fold the path into the
ETag so each file has its own fingerprint.
Follow-up to b3c1030 (#439).
This commit is contained in:
+26
-13
@@ -3,37 +3,50 @@ require("ts-node/register/transpile-only");
|
||||
const { fileETag } = require("../src/server/routes/file-etag");
|
||||
|
||||
describe("fileETag", function () {
|
||||
const opts = { terms: ["alice"] };
|
||||
|
||||
it("changes when the upstream sha changes", function () {
|
||||
const opts = { terms: ["alice"] };
|
||||
expect(fileETag("sha1", opts)).to.not.equal(fileETag("sha2", opts));
|
||||
expect(fileETag("sha1", "README.md", opts)).to.not.equal(
|
||||
fileETag("sha2", "README.md", opts)
|
||||
);
|
||||
});
|
||||
|
||||
it("changes when the file path changes", function () {
|
||||
expect(fileETag("sha1", "README.md", opts)).to.not.equal(
|
||||
fileETag("sha1", "src/index.ts", opts)
|
||||
);
|
||||
});
|
||||
|
||||
// #439 — without folding the anonymization options into the ETag, editing
|
||||
// the term list left the same URL serving stale anonymized bytes.
|
||||
it("changes when the anonymization terms change", function () {
|
||||
const a = fileETag("sha1", { terms: ["alice"] });
|
||||
const b = fileETag("sha1", { terms: ["alice", "bob"] });
|
||||
expect(a).to.not.equal(b);
|
||||
expect(fileETag("sha1", "README.md", { terms: ["alice"] })).to.not.equal(
|
||||
fileETag("sha1", "README.md", { terms: ["alice", "bob"] })
|
||||
);
|
||||
});
|
||||
|
||||
it("changes when an anonymization toggle changes", function () {
|
||||
const a = fileETag("sha1", { terms: ["alice"], image: true });
|
||||
const b = fileETag("sha1", { terms: ["alice"], image: false });
|
||||
expect(a).to.not.equal(b);
|
||||
expect(
|
||||
fileETag("sha1", "README.md", { terms: ["alice"], image: true })
|
||||
).to.not.equal(
|
||||
fileETag("sha1", "README.md", { terms: ["alice"], image: false })
|
||||
);
|
||||
});
|
||||
|
||||
it("is stable for the same inputs", function () {
|
||||
const opts = { terms: ["alice", "bob"], image: true };
|
||||
expect(fileETag("sha1", opts)).to.equal(fileETag("sha1", opts));
|
||||
expect(fileETag("sha1", "README.md", opts)).to.equal(
|
||||
fileETag("sha1", "README.md", opts)
|
||||
);
|
||||
});
|
||||
|
||||
it("treats missing version like an empty string", function () {
|
||||
const opts = { terms: [] };
|
||||
expect(fileETag(undefined, opts)).to.equal(fileETag("", opts));
|
||||
expect(fileETag(undefined, "README.md", { terms: [] })).to.equal(
|
||||
fileETag("", "README.md", { terms: [] })
|
||||
);
|
||||
});
|
||||
|
||||
it("returns a quoted opaque tag", function () {
|
||||
const tag = fileETag("sha1", { terms: [] });
|
||||
const tag = fileETag("sha1", "README.md", { terms: [] });
|
||||
expect(tag).to.match(/^"f-[0-9a-f]{40}"$/);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user