teqo-backup/src/utilities/boundedRequestBody.ts
Francisco Solla 9e9864f420 feat(C234): busca por selfie no álbum público — índice A/C consentido, selfie on-device
- /fotos/encontre + POST /api/fotos/selfie: só o vetor 128-d, sem imagem, sem score e
  sem nomes de terceiros; fail-closed pela flag photoAlbum.selfieSearchEnabled (default
  false) e pelos Consents por chave estável (busca-selfie-fotos/busca-selfie-indice, não
  seedados — a superfície nasce fechada até o aval jurídico/DPIA).
- Índice A/C do gate (PR #1370): só quem aderiu é indexado; descriptors do acervo são
  transitórios. Collection faceSubject (admin-only, sem vínculo com Contact), vínculos
  matchedPhotos e marker faces.checkedKey no archivePhoto (modelo + enrolledAt).
- Engine @vladmandic/face-api nos dois lados: browser lazy com modelos same-origin
  (copy-face-vision-assets no prebuild/predev/Docker) e CLI node-wasm sem tfjs-node.
  CLIs ops-only faces:enroll / faces:index --apply|--verify com guardas C231/C232.
- Opt-out self-service por reconhecimento (leave-index) + canal de remoção do C233;
  rate limit próprio (namespace selfie:<hash>) e bounded body extraído do beacon.
- UI portada classe-a-classe do design estendido (paridade certificada, tier primário);
  e2e frontendFotosSelfie (manifest+curado+projeto, engine stubada) e canário do engine
  real; runbook §C234, contrato em AGENTS-public.md e changelog.
- Migration aditiva add_face_subject (SQL revisado); hard-stops aprovados na sessão.
2026-09-27 20:04:41 -03:00

41 lines
1.1 KiB
TypeScript

import 'server-only'
/**
* Reads a request body with a hard byte ceiling, streaming and aborting as
* soon as the ceiling is crossed — a chunked body without `content-length`
* cannot make a route buffer unbounded input. `null` means "refused".
*
* Single owner of the bounded-read rule for the anonymous public JSON routes
* (the content-events beacon and the selfie search); extracted from the beacon
* route when it earned a second caller.
*/
export const readBoundedRequestBody = async (
request: Request,
maxBytes: number,
): Promise<string | null> => {
const body = request.body
if (!body) return ''
const reader = body.getReader()
const decoder = new TextDecoder()
let text = ''
let bytes = 0
try {
for (;;) {
const { done, value } = await reader.read()
if (done) break
bytes += value.byteLength
if (bytes > maxBytes) {
await reader.cancel().catch(() => undefined)
return null
}
text += decoder.decode(value, { stream: true })
}
return text + decoder.decode()
} catch {
await reader.cancel().catch(() => undefined)
return null
}
}