fix: silent-truncation, token-refresh, and content-type bugs across hot paths

Follow-up review pass after the cache fixes turned up several bugs in
the same family — silent failures that look like success to the client,
plus content-correctness issues in the ZIP and per-file delivery paths.

- zipStream: stop calling archive.finalize() on upstream/parser errors.
  That produced a valid-looking ZIP (200 OK, archive opens) silently
  missing entries — same class as #694, but worse because the user has
  no signal anything went wrong. Destroy the response on failure
  instead so the client sees a connection drop.
- zipStream: apply per-repo image/pdf gates inside the entry handler.
  The single-file /file/... endpoint refuses to serve those types
  via AnonymizedFile.isFileSupported when image=false / pdf=false, but
  the ZIP shipped them anyway — privacy-relevant for maintainers who
  toggle image=false to suppress identifying screenshots. Threaded
  contentOptions through both ZIP entry points (direct and streamer).
- GitHubUtils.getToken: validate the OAuth token-refresh response
  before persisting. On a non-2xx response or a body without a string
  token, we used to overwrite the stored token with `undefined`, which
  then propagated as `Authorization: token undefined` to every API
  call — 401 even on public repos, with the config.GITHUB_TOKEN
  fallback unreachable because the field was no longer falsy.
- AnonymizedFile.send (streamer branch): forward Content-Type from the
  upstream streamer response. got.stream(...).pipe(res) carries body
  bytes only, so the parent response had no Content-Type and browsers
  guessed (text rendered as download, etc.). Also resolve on
  res.on("finish") in addition to "close" — keep-alive sockets stay
  open long after the response is delivered, delaying countView().
- Repository.updateIfNeeded: persist a renamed source.repositoryName
  even when the commit hasn't changed. Previously the new value lived
  in memory only and was overwritten on the next reload, so the
  rename detection ran every request.
- Repository.anonymize: stop materialising a dummy {path:"",name:""}
  FileModel for empty repos. That row collided with the special case
  in AnonymizedFile.getFileInfo and surfaced in unfiltered listings.
- streamer/route POST /: reject filePath segments containing ".." or
  empty parts. Defence in depth — the parent server validates against
  FileModel before calling, but the streamer joins filePath straight
  into the storage path, so any future caller forwarding an
  unvalidated path could traverse out of the repo root.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
tdurieux
2026-05-05 09:19:05 +03:00
parent f413a30313
commit 5b72b630c4
7 changed files with 192 additions and 52 deletions
+22 -15
View File
@@ -5,7 +5,7 @@ import * as sha1 from "crypto-js/sha1";
import User from "./User";
import GitHubStream from "./source/GitHubStream";
import Zip from "./source/Zip";
import { anonymizePath } from "./anonymize-utils";
import { anonymizePathCompiled, compileTerms } from "./anonymize-utils";
import UserModel from "./model/users/users.model";
import { IAnonymizedRepositoryDocument } from "./model/anonymizedRepositories/anonymizedRepositories.types";
import { AnonymizeTransformer } from "./anonymize-utils";
@@ -35,10 +35,11 @@ function anonymizeTreeRecursive(
includeSha: false,
}
): Partial<IFile>[] {
const compiled = compileTerms(terms);
return tree.map((file) => {
return {
name: anonymizePath(file.name, terms),
path: anonymizePath(file.path, terms),
name: anonymizePathCompiled(file.name, compiled),
path: anonymizePathCompiled(file.path, compiled),
size: file.size,
sha: opt.includeSha
? file.sha
@@ -293,8 +294,17 @@ export default class Repository {
force: true,
});
// update the repository name if it has changed
this.model.source.repositoryName = ghRepo.fullName;
// update the repository name if it has changed. Persist it
// immediately — otherwise, when the commit is unchanged and the
// function returns early below, the renamed value lives in this
// in-memory model only and the next request reloads the stale
// name from MongoDB and re-runs the rename detection forever.
if (this.model.source.repositoryName !== ghRepo.fullName) {
this.model.source.repositoryName = ghRepo.fullName;
if (isConnected) {
await this._model.save();
}
}
const branches = await ghRepo.branches({
force: true,
accessToken: token,
@@ -359,20 +369,17 @@ export default class Repository {
}
this.model.increment();
await this.updateStatus(RepositoryStatus.DOWNLOAD);
const files = await this.files({
await this.files({
force: false,
progress,
recursive: false,
});
if (files.length === 0) {
// create a dummy file when the repo is empty to avoid errors
await new FileModel({
repoId: this.repoId,
path: "",
name: "",
size: 0,
}).save();
}
// Previously inserted a dummy {path:"", name:"", size:0} FileModel
// here for empty repos "to avoid errors" — but that record collides
// with the special-case in AnonymizedFile.getFileInfo for the empty
// path, surfaces in unfiltered file listings, and breaks anything
// that assumes FileModel rows correspond to real files. Empty repos
// are handled by the route layer; nothing to materialise here.
await this.updateStatus(RepositoryStatus.READY);
await this.computeSize();
}