fix(privacy): apply noindex+mask policy to machine-readable outputs (#173) - #183
fix(privacy): apply noindex+mask policy to machine-readable outputs (#173)#183LyuboslavLyubenov wants to merge 33 commits into
Conversation
lyubomir-bozhinov
left a comment
There was a problem hiding this comment.
Добре структуриран — маскирането е на query слой преди R2, noindex-gate-ът на .json е чист, тестовете са солидни. Но тезата на PR-а („noindex+mask на всички machine-readable изходи") не е изпълнена докрай:
🔴 .data single-fetch payload е непокрита machine-readable повърхност. При ssr:true RRv7 сервира всеки loader и на GET /<път>.data. company.tsx loader-ът връща eik + displayName немаскирани, а headers() е publicCache(...) → отговорът минава през hardenResponse (само baseSecurityHeaders, без X-Robots-Tag) и се кешира на edge. Значи /companies/:eik.data връща немаскиран ЕИК на физическо лице, кеширан, без noindex — точно експозицията от #173, на повърхността, която PR-ът не изброява. (Потвърдих reachability-то на .data живо на публичния prod; robots.txt не покрива /*.data.) Поправка: приложи предиката/маската и за .data, или централизирай X-Robots-Tag в hardenResponse вместо per-route.
🟠 Sitemap-ът ползва по-тесния предикат. sitemaps.ts филтрира с isNaturalPersonProfileName (само по име), не с разширения isNaturalPersonBidder (+legal_form), който PR-ът ползва навсякъде другаде. ЕТ, разпознат само по legal_form, се маскира/noindex-ва навсякъде, но пак се рекламира в /sitemap-companies — каним crawl на страница, която политиката де-индексира.
За проверка (не потвърдено срещу данни): премахнатият consortium guard в isNaturalPersonBidder — нито един caller не филтрира kind==='consortium', та консорциум с водещ „ЕТ …" в името може да се over-маскира като „Частно лице". Струва си да се потвърди срещу корпуса, преди да се третира като дефект.
lyubomir-bozhinov
left a comment
There was a problem hiding this comment.
Инлайн котви към горния преглед — .data single-fetch privacy gap.
|
Прегледах PR #183 изцяло — целия diff (24 файла, +1882/−45), всички коментари по ревюто, issue #173, както и текущото състояние на кода в работното дърво ( ОбобщениеЦентрализацията на Сигурност / SQL / OWASP
🔴 Съществени пропуски (data-integrity / поверителност)1. Подизпълнителят ( 2. Sitemap-ът ползва по-тесния предикат (незатворена забележка от предишното ревю). 3. HTML профилът скрива ЕИК на физическите лица — противоречи на собствената документация в PR-а. Споделеният loader прави 🟠 По-малки бележки4. 5. Възможно over-маскиране на консорциум. Съответствие с issue #173Ядрото на issue-то (bulk-indexable ЕИК на ЕТ в Бележки
Вердикт: Заявка за промени (Request changes) — блокиращо е т.1 (немаскиран ЕИК/име на подизпълнител-физическо лице в Благодаря за прегледната работа по маркерния договор и тестовете — след затварянето на горните пропуски PR-ът ще е в много добра форма. |
|
Блокерът е затворен — ре-проверих на връх
Точно това затваря находката ми (немаскиран ЕИК на физ. лице, кеширан, без noindex, изтичащ през Единствено: branch-ът е в конфликт с main ( |
…mber worker adr to 0008 Rebase of midt-bg#183 onto upstream/main (post-midt-bg#182 ADR reorganization) restructured the privacy-policy and worker-level X-Robots-Tag ADRs to live in docs/adr/ rather than inline in docs/architecture.md: - New docs/adr/0007-privacy-masking.md — content extracted from the inline ADR-0002 in architecture.md; relative paths adjusted (../ → ../../) for the new adr/ location; cross-link to the worker ADR now points to 0008. - docs/adr/0003-centralized-x-robots-tag-worker.md → docs/adr/0008-centralized-x-robots-tag-worker.md — renumbered to free the 0003 slot taken by upstream's value-flag ADR; internal cross-link from architecture.md#adr-0002-... to 0007-privacy-masking.md. - docs/adr/README.md — index extended with the two new entries. - docs/architecture.md — adopted upstream's short summary form; the inline ADR-0001+0002 contents are removed (the rendering ADR lives at adr/0001-rendering-and-security.md and the privacy policy at adr/0007-privacy-masking.md); Решения (ADR) section now also points to 0007 and 0008. - docs/privacy-masking.md — cross-link from architecture.md#adr-0002-... to adr/0007-privacy-masking.md; ADR-0003 to ADR-0008. No code changes; verified pnpm check:docs (docs-integrity gate from midt-bg#182) passes.
The three files modified by PR midt-bg#183 carried pre-existing prettier debt that the original review flagged (`pnpm lint` exit 1 with `contract.json.test.ts`, `companies.test.ts`, `companies.ts`). The repo's CI is configured as blocking lint (`2d93cd5`, comment in .github/workflows/ci.yml), so this would have blocked the PR from merging. Run `pnpm prettier --write` on the three files — no semantic changes.
|
Прегледах отново новия връх 1) Списък „Фирми" ( 2) Начална страница ( 3) Страница на договора ( 4) Подизпълнител.
Корен (altitude): маскирането е на leaf routes/exports, не на споделените проекции. Устойчивият фикс: маскирай в PR-ът затваря буквата на #173 (.json/.csv), но не и духа — ЕТ ЕИК на индексираните HTML страници. Блокер до фикса на споделените проекции. |
nedda76
left a comment
There was a problem hiding this comment.
Прегледах PR-а спрямо #173. Механиката marker→header е добре построена и тествана: CSV се маскира преди записа в R2, worker-ът слага X-Robots-Tag: noindex и трие marker-а преди edge cache (cache-safe — app.nofollow.test.ts го покрива). Но маската не покрива всяка machine-readable повърхност и на места е обезсилена — има потвърдени пътища, по които суровият ЕИК на физическо лице все още изтича. Тоест PR-ът още не затваря #173.
Блокиращи — потвърдени течове на суров ЕИК
-
slugпресейва ЕИК-а. Маската нулираeik, но оставяslug— а за eik-базирани субектиslugе самият нормализиран ЕИК (companySlug('eik:222222222') === '222222222'). Тоестbidder.slug/company.slugв/contracts/:id.jsonи/companies/:eik.dataносят точно идентификатора, който маската маха. ADR-0007 („slug-ът не е PII") не важи за физически лица — а те са именно маскираната популация. -
/contracts/:id.dataне е маскиран. HTML маршрутътcontract.tsxе недокоснат; приssr:truesingle-fetch/contracts/:id.dataсервира payload-а на loader-а машинно четимо —bidder.eikиsubcontractor.eik, без noindex..jsonе маскиран, но.dataблизнакът му — не (точно класът повърхности, заради който съществува ADR-0008). -
Подизпълнителят не се маскира.
maskContractForPrivacyпипа самоbidder+sourceNames.bidder;subcontractorминава непокътнат, а подизпълнителят може да е ЕТ/физическо лице → ЕИК изтича. Рядко (~0.8% от договорите), но реален непокрит път. -
/companies.data(списък) не е маскиран./companies.csvе, но.dataблизнакът на списъчния маршрут връщаCompanyListItemсъс суровeik+ име за физически лица → точно „bulk searchable/downloadable" вредата от #173, само през.dataвместо.csv.
За обсъждане — по-нисък приоритет
- Мрежовият граф в маскирания
company.dataноси node id-таeik:<ЕИК>→ физическо лице като възел изтича ЕИК. Частично фундаментално за eik-базираните id-та (като #1). - Регресия за легитимни фирми: предикатът пада към name-евристика (
ЕТ/ETпрефикс) дори при реаленlegal_form(ООД) → фирма с име „ET Engineering" получава скрит валиден ЕИК (нарушава изискване #5). Евристиката е заварена, но сега тя контролира скриване на ЕИК, не само мек noindex — цената на false positive расте. - Несъгласуваност:
.json/.csvзаменят името с етикет, ноcompany.data/HTML пазят ЕТ името дословно. Трите machine-readable пътя маскират различни полета.
Чисто (за фокус)
marker→header плъмбингът и cache safety; обединеният предикат isNaturalPersonBidder (и двата source() клона проектират legal_form); запазеното показване на юридически лица по покритите пътища; authorities.csv (публични органи — умишлено без body-маска).
Коренът на #1/#2/#4 е един: маската покрива .json/.csv, но не и .data близнаците и не пипа slug. Докато .data повърхностите и slug не се покрият, #173 остава отворен.
ydimitrof
left a comment
There was a problem hiding this comment.
Преглед на PR #173 — „fix(privacy): noindex + маскиране за машинно-четими изходи"
Какво прави PR-ът
Прилага политика за поверителност върху машинно-четимите изходи (turbo-stream .data, JSON, CSV), така че идентификаторите и имената на физически лица/ЕТ да бъдат маскирани, а не само маркирани с noindex. Архитектурата е чиста и с ясно разделение на отговорностите: route-овете маркират чрез markPrivacyMaskApplied, worker-ът превежда маркера в X-Robots-Tag: noindex чрез applyPrivacyMaskHeaders и го изтрива преди кеш/клиент. Въвежда се единен предикат isNaturalPersonBidder и константа MASKED_NATURAL_PERSON_LABEL в @sigma/shared, преизползвани в streamCompaniesCsv и streamContractsCsv; legal_form се прокарва коректно през двата клона на source() и през getContract.
Сигурност (Фаза 0)
ЧИСТО и в двете партиди — няма зашити тайни/ключове, нови зависимости, нови или подозрителни URL адреси, нито зловредни шаблони. Промяната всъщност намалява изтичането на лични данни. Тестовото покритие е силно и смислено: покрити са MISS/HIT/dynamic, идемпотентност, edge-cache инвариантите, негативните случаи, двата клона на source(), name-евристиката при legal_form=null и таблица на истинност на предиката. Документацията (ADR-0007, ADR-0008, privacy-masking.md) е изчерпателна и синхронизирана с кода.
Блокираща забележка
- (Средно — поверителност/консистентност) Машинно-четимият близнак
/companies/:eik.dataизчиства самоcompany.eik, но връща пълното име на физическото лице (displayName) буквално — за разлика от/contracts/:id.jsonи CSV, които заменят името сMASKED_NATURAL_PERSON_LABEL. Тъй като.dataе машинно-четим изход, а самата обосновка на PR-а (ADR-0007) гласи, чеnoindexе недостатъчен срещу ботове, оставянето на суровото име само задnoindexпротиворечи на декларираната цел. Тестътcompany.data.test.tsдори утвърждава това като очаквано. Нужно е явно решение: да се маскира името и в.data, или изрично да се документира защо.dataостава немаскиран.
Некритични забележки
- (Ниско — поверителност) При маскиране
bidder.slug/company.slugсе запазват. Ако slug-овете се извеждат от името на субекта, URL-фрагментът може да разкрие идентичността въпреки маскирането наname/eik. ADR-0007 приема slug като „URL фрагмент, не PII", но не адресира случая на slug, изведен от име. - (Ниско — точност на маскирането)
isNaturalPersonBidderсе вика без предварителна проверка заbidder_kind, въпреки че документацията му възлага филтрирането на консорциумите на викащия. Консорциум с име, започващо с „ЕТ ", ще бъде over-маскиран като „Частно лице" — privacy-safe, но с загуба на информация. - (Ниско — DB/производителност) Клонът по подразбиране на
source()вече винаги правиLEFT JOIN bidders, а това е и hot path заlistCompanies. Да се потвърди, чеcompany_totalsняма собствена колонаlegal_form(иначеct.*+b.legal_formдава двусмислена дублирана колона) и че има индекс поbidders.id. - (Ниско — дублиран/мъртъв код) Няколко случая на дублиране, противоречащи на правилото „NO CODE DUPLICATION": излишните предварителни извиквания на
markPrivacyMaskAppliedвresponseFromR2Object(csv-export.ts), коитоmarkCsvCacheведнага презаписва; и inline вариант на правилата заlegal_formвapps/web/app/routes/company.tsx. - (За потвърждение)
details.tsпрокарваbidder_legal_formбез маскиране и без тест в прегледаната партида — да се потвърди, че JSON маскирането се извършва другаде.
Вердикт
Промяната е висококачествена и без изтичане на данни в прегледаните файлове. Единствената блокираща точка е несъответствието в поверителността при /companies/:eik.data (т.1) в PR, чиято цел е именно защита на лични данни — тя трябва да бъде адресирана или изрично обоснована преди одобрение. Останалите забележки са некритични.
lyubomir-bozhinov
left a comment
There was a problem hiding this comment.
Благодаря — маскирането на .json и CSV е издържано. Но остава отворена точно най-чувствителната machine-readable повърхност, заради която е #173: detail страницата на договора и нейният .data twin.
apps/web/app/routes/contract.tsx — loader-ът (:62-67) връща { contract } суров; файлът не се пипа в този PR и няма нито маска, нито noindex. getContract селектира b.eik_normalized AS bidder_eik (packages/db/src/queries/details.ts:440) и го отдава суров (:583 eik: r.bidder_eik, :595 eik: r.subcontractor_eik). contract.tsx ги рендира без маска — :221 ЕИК на изпълнителя, :261 ЕИК на подизпълнителя. Worker-ът прилага само applyPrivacyMaskHeaders(headers) (header-only, noindex) — няма body маска на ниво worker.
Ефект (prod е публичен, unauthenticated):
GET /contracts/<slug>.data→ turbo-stream body сbidder.eik/subcontractor.eikна физическо лице (ЕТ), немаскиран и безX-Robots-Tag.GET /contracts/<slug>(HTML) → същият ЕИК, индексируем.
Същата политика, различно прилагане и на списъците:
/companies.data:toCompanyListItem(packages/db/src/queries/rows.ts:58) връщаeik: r.eikбезisNaturalPersonBidderпроверка, докато/companies.csvго маскира./contracts.data:toItemвcontracts.tsдава немаскирани имена на физически лица (CSV пътят вече маскира).
Предложение: маскирай в споделения слой — в getContract, или в loader-а на contract.tsx огледално на company.tsx:77-85 — за да го наследят HTML, .data и .json, вместо per-route. Същото за toCompanyListItem (редът вече носи legal_form) и за подизпълнителя в maskContractForPrivacy.
Докато .data twin-ът не минава през същата маска като .json, #173 не е затворен. Проверих горните редове на HEAD (468a116). Блокиращо за merge.
…port The R2-body branch (responseFromR2Object) and the 304 branch each called markPrivacyMaskApplied directly, then handed the response to markCsvCache, which calls it again internally. The marker was applied twice on MISS/HIT/304 paths — idempotent in effect, but dead code that hid markCsvCache as the single source of truth for the privacy marker on every CSV path (PR midt-bg#183 review T-004, "NO DEAD CODE / NO CODE DUPLICATION"). Drop the direct calls; rely solely on markCsvCache. Add a TDD guard that spies on markPrivacyMaskApplied and asserts exactly one call per response path (MISS/HIT/dynamic/304), so a future duplicate cannot sneak back in.
…ortium over-masking isNaturalPersonBidder's docstring delegates consortium filtering to the caller — a JV is a legal entity even if a lead member's name / legal_form matches a sole-trader signal. But streamContractsCsv and streamCompaniesCsv both invoked it WITHOUT a bidder_kind guard, so a consortium such as "ЕТ Иван Петров; Строй ООД" (or any consortium whose legal_form collided with a sole-trader form) was masked to MASKED_NATURAL_PERSON_LABEL with its ЕИК cleared. The result was privacy-safe (over-masking, no leak) but a behavioral change that dropped the lead member's name + ЕИК and contradicted the predicate's contract. Add an early bidder_kind/kind !== 'consortium' guard in both streamers so consortium rows keep the "… и др." shape and their ЕИК. TDD: failing tests first (consortium with ЕТ lead name + ЕТ legal_form, and the leading-ЕТ name heuristic with legal_form null), then the guard (PR midt-bg#183 T-006).
…ne duplication) The docstring claimed the legal_form rules were "carried inline in apps/web/app/routes/company.tsx until the route migrates" — but ADR-0007 §1 already removed the legacy inline isSingleNaturalPersonProfile, and company.tsx now calls this shared predicate directly (verified: no legal_form string- matching exists outside packages/shared). The stale claim created exactly the divergence risk the PR midt-bg#183 reviewer flagged under "NO CODE DUPLICATION": a future reader could believe a second copy still lives in the route and maintain it separately. Rewrite the docstring to state the predicate is the single source of truth and enumerate the downstream surfaces that consume it (HTML noindex, CSV masking, JSON masking), with a pointer to the bidder_kind/kind consortium guards added in the CSV streamers (PR midt-bg#183 T-006). No behavior change.
…6, §7) Two PR midt-bg#183 review threads asked for explicit product decisions on the company profile masking surface. Both are recorded here as policy. §6 — displayName stays visible in the HTML profile and its `.data` twin; only the ЕИК is masked. The trading name is PUBLIC (rendered verbatim on the HTML page and in <title>); the sensitive natural-person identifier is the ЕИК. The `.data` turbo-stream is React Router v7's single-fetch transport for client-side navigations, NOT a standalone export like /contracts/:id.json — masking the name there would break client-rendered pages. Consistent policy: name = public, ЕИК = sensitive. company.tsx loader comment now states this; the company.data.test.ts assertion locks displayName-verbatim + eik-null as the contract. §7 — the name-keyed natural-person slug (n + base64url(name)) is a tracked limitation, not changed in this PR. The name is public (§6), the sitemap already filters these records, and reworking the slug scheme is cross-cutting (URL stability, internal links, identity system) and out of scope for a masking PR. No behavior change.
|
Прегледах #183 на дълбочина срещу head Силната част (потвърдено):
MAJOR 1 — JSON masker-ът НЯМА consortium guard-а, който CSV пътят има. MAJOR 2 — най-изложената повърхност остава отворена: страницата на договора + MAJOR 3 (test-gap + архитектура) — препращането на маркера към worker-а за MINOR — не-ЕТ физически лица могат да минат немаскирани. NIT — PR-ът е CONFLICTING спрямо main — нужен е rebase (отделно от горното). Насоката е правилна; двата masker-а — consortium guard в JSON и покриване на страницата/ |
… path The `/contracts/:id.json` masker (`maskContractForPrivacy`) lacked the `bidder_kind !== 'consortium'` guard that the CSV streamer already has (`contracts.ts:459`). A consortium whose display name begins with „ЕТ " (first member is a sole trader, e.g. „ЕТ Иван Петров; Строй ООД") was over-masked to „Частно лице" — losing the „… и др." shape, the consortium ЕИК, and gaining an unearned `noindex`. `isNaturalPersonBidder`'s docstring delegates consortium filtering to the caller; this adds the caller guard, mirroring the CSV path exactly. Flagged as MAJOR 1 in the PR midt-bg#183 review of head a9b18ae. TDD: failing consortium cases first (name-based + legal_form-based, plus a loader-level marker-omission case), then the guard.
`contract.tsx` was the most-indexable surface still open: its loader returned
`{ contract }` raw with no privacy marker, `robots.txt` does not block
`/contracts/:id` (or its `.data` twin), and the page rendered `c.bidder.eik`
verbatim — so a sole-trader's ЕИК was indexable on both the HTML page and the
RRv7 single-fetch `.data` payload. That is a worse exposure than the already-
closed `.json`/`.csv` paths.
Masking + signalling in the SHARED loader covers both surfaces at once (the
`.data` twin reuses the same loader), mirroring `company.tsx:89` exactly:
ЕИК (the sensitive natural-person ID) → null on the returned object, the
trading displayName stays PUBLIC (ADR-0007 §6), and the `X-Privacy-Mask:
applied` marker is translated to `X-Robots-Tag: noindex` by the worker. The
`kind === 'consortium'` guard matches the JSON masker (MAJOR 1) and the CSV
streamer so a JV is never over-masked/noindexed. `headers()` forwards the
marker onto the HTML response (RR does not auto-propagate loader headers).
Flagged as MAJOR 2 in the PR midt-bg#183 review of head a9b18ae.
TDD: failing loader/headers/pipeline cases first, then the loader change.
…eal worker The PR midt-bg#183 review (MAJOR 3) noted the marker→`.data`→`X-Robots-Tag` forwarding was only proven through fixtures that INJECT the marker by hand in the stubbed RR handler — which proves the worker CAN translate a marker, not that a real loader's marker survives the pipeline to the final `.data` HTTP response. That left a „green tests, hidden gap" risk on the most-indexable surface. Add four cases driving the REAL `worker.fetch` (→ handleRequest → hardenResponse → applyPrivacyMaskHeaders → edgeCache.put) against `/contracts/<x>.data`: masked sole-trader → noindex + marker stripped + masked body preserved; cached entry carries noindex (HIT-path invariant); second request HITs and serves noindex verbatim; legal-entity negative (no marker → no noindex). The handler returns the exact shape `contract.tsx`'s masked loader branch now produces (MAJOR 2), so this is an honest end-to-end proof of the forwarding guarantee. Note: the review's suggested path-based worker match (the weekly-digest `DIGEST_DETAIL_PATH` precedent) does not exist in this codebase — the worker does no path-based matching; the marker-based design (ADR-0008) is the established architecture and is sound, so this keeps it.
|
@lyubomir-bozhinov Благодаря за задълбочения adversarial pass — трите MAJOR забележки са затворени в три фокусирани комита върху MAJOR 1 — consortium guard в JSON masker-а ( MAJOR 2 — маскиране на страницата на договора + MAJOR 3 — end-to-end доказателство, че маркерът достига Забележка към предложението ти за path-based worker match (седмичният digest За останалите ти точки (не са код-промени в този PR):
Rebase: PR-ът е Пълна локална проверка: |
…-Mask marker The literal X-Robots-Tag header is no longer set anywhere in apps/web; it is now written by exactly one helper (applyPrivacyMaskHeaders in apps/web/app/lib/security.ts), called by the worker hardenResponse after the base security headers and before the cacheable-HTML branch. A new internal marker X-Privacy-Mask: applied is the route-side signal that the response carries masked natural-person data. Route handlers (csv-export.ts markCsvCache + 304 branch, contract.json.tsx loader) and the worker consume that marker; it is deleted unconditionally before the response is returned or stored in edgeCache.put so it never reaches clients. The HIT path in handleRequest (apps/web/workers/app.ts) is unchanged: it copies cached.headers verbatim, and the cached entry is the post-hardenResponse response, so the header survives the edge cache by construction. apps/web/workers/app.nofollow.test.ts (T-005 + T-008) exercises the end-to-end worker flow for the .data twin of /companies/:eik and the contract.json natural-person branch. No edits to packages/db or to the public API contract; bidders.legal_form stays server-only and company.eik stays on the CompanyRecord type (masked to null by the loader, not removed).
…ough headers()
The single-fetch .data twin of the company profile now clears company.eik to null
on the natural-person branch (per isNaturalPersonBidder) and signals the
worker via the internal X-Privacy-Mask: applied header on the Response.json
return. The route's headers() export now destructures { loaderHeaders } from
Route.HeadersArgs and forwards the marker explicitly so the worker
hardenResponse can translate it into X-Robots-Tag: noindex on the HTML
response (getDocumentHeadersImpl only auto-propagates Set-Cookie).
Legal-entity records keep the plain-object return unchanged — no marker, no
mutation, no Response.json wrap. The not-found short-circuit (throw new
Response('Not Found', ...)) runs before the masking gate, so 404s never
carry the marker.
The HTML meta() noindex branch is unchanged — natural-person pages
continue to emit <meta name="robots" content="noindex"> via the existing
seoMeta + isNaturalPersonBidder gate. The new headers() forward adds a
redundant X-Robots-Tag: noindex HTTP header alongside the meta tag, which
is acceptable (the worker translates the marker for all responses).
apps/web/app/routes/company.data.test.ts is the focused new test suite
(7 tests across 5 describe blocks): natural-person loader return asserts
company.eik === null and X-Privacy-Mask: applied; legal-entity loader
return asserts a plain object with eik unchanged and no marker; headers()
test exercises both branches (marker present → forwarded + Cache-Control;
marker absent → Cache-Control only); meta() test covers the natural-person
noindex HTML tag; the worker-pipeline describe calls applyPrivacyMaskHeaders
on the loader return and asserts X-Robots-Tag: noindex is set while
X-Privacy-Mask is stripped, proving the worker translate end-to-end.
…rage
ADR-0002 (docs/architecture.md): the Решение section now describes the
centralized X-Robots-Tag: noindex write site (hardenResponse in
apps/web/workers/app.ts, via the applyPrivacyMaskHeaders helper in
apps/web/app/lib/security.ts). The bullet on per-route CSV/contract-json
writes is replaced by a single sentence naming hardenResponse, the marker
flow, and the deletion pre-edgeCache.put.
The Засегнати повърхности list grows to explicitly enumerate:
- the .data twin of /companies/:eik (React Router v7 single-fetch,
automatic via the shared loader in company.tsx)
- apps/web/workers/app.ts (hardenResponse) as the centralized
enforcement point under a new 'Worker — централизирана точка за
прилагане' sub-heading
- apps/web/app/lib/security.ts as the policy helper home, with
PRIVACY_MASK_APPLIED as the literal-typed constant.
The privacy page (apps/web/app/routes/privacy.tsx) #natural-person-data
section grows to enumerate /companies/:eik.data alongside the existing
/contracts/:id.json and the three CSV exports. A follow-up paragraph in
Bulgarian prose explains that the X-Robots-Tag: noindex policy is now
applied uniformly at the worker edge so future machine-readable surfaces
inherit it automatically — without naming the X-Privacy-Mask marker or
the helper functions (user-facing wording only).
No edits to package.json, pnpm-lock.yaml, or the public API contract.
Daily autonomous rebase — conflict resolved, PR now MERGEABLEHead bumped from What changed
Validation (clean tree, fresh
|
ydimitrof
left a comment
There was a problem hiding this comment.
Ревю на PR #173 — политика noindex + маскиране за машинно-четими изходи
Какво прави PR-ът
Прилага единна политика за поверителност (маскиране + noindex) към машинно-четимите изходи (CSV/JSON). Маскирането на данни за физически лица (ЕИК → null, символен етикет MASKED_NATURAL_PERSON_LABEL) се извършва per-row преди запис в кеша/R2. Route слоят само поставя вътрешен маркер (X-Privacy-Mask / markCsvCache), а worker-ът (hardenResponse) го превежда до публичния X-Robots-Tag: noindex и премахва вътрешния маркер — чиста separation of concerns с единствен източник на истина. Добавени са ADR-0036/0037, оперативна документация и разширение на getContract.
Обща оценка
Логиката е солидна, а тестовото покритие — силно: проверяват се двата source() клона, случаите ЕТ/ООД/ДЗЗД, leading-ЕТ евристиката, консорциуми, идемпотентност („точно веднъж"), както и end-to-end worker pipeline-а (маркер → noindex, стрип преди кеш, HIT verbatim). Сигурността е чиста: няма тайни, нови URL/зависимости или злонамерени шаблони.
Блокиращи находки
- Оставен merge-conflict маркер
<<<<<<< HEADвdocs/adr/README.md— счупва ADR индекс-таблицата и е признак за неразрешен merge. Трябва да се премахне преди merge. company.tsx(loader) — липсва guard за консорциум. За разлика отcontract.tsx/contract.json.tsx, които добавятkind !== 'consortium'предиisNaturalPersonBidder(...), loader-ът за компанията маскира ЕИК за всяко съвпадение без изключение за консорциуми. Тъй катоisNaturalPersonBidderделегира филтрирането на консорциуми на извикващия, консорциум сdisplayName, започващ с „ЕТ …", ще бъде над-маскиран. Коментарите в другите файлове твърдят, че „огледално повтарят company.tsx:89", но там guard няма — трябва да се изравни за консистентност на политиката.
Незначителни находки
- Грешни номера на ADR в
docs/architecture.md—(0033)/(0034)сочат към файлове 0036/0037. - Възможна регресия в производителността: rollup-клонът на
source()вече правиLEFT JOIN biddersза всички списъчни заявки (вкл. HTMLlistCompanies), макарlegal_formда е нужен само на CSV стрийма. csv-export.ts—markPrivacyMaskAppliedсе извиква безусловно вmarkCsvCache, докато docstring-ът гласи да се извиква само при маскирани данни за физическо лице. Ако blanket-политиката е умишлена (както подсказваprivacy.tsx), актуализирайте docstring-а, за да не подвежда.
Заключение
REQUEST_CHANGES. Кодовата логика и тестовете са в добро състояние; блокиращи са conflict-маркерът в docs/adr/README.md и липсващият consortium-guard в company.tsx. След тяхната поправка (плюс номерата на ADR и джойн-а) PR-ът е близо до одобрение.
PR midt-bg#183 review (ydimitrof, 2026-08-18) flagged three doc issues: - docs/adr/README.md had a stray `<<<<<<< HEAD` line on the index table (PR branch carried 0033-0034 from the privacy work; upstream brought 0033-0037 from the registry-evidence work; the merge was botched and dropped the closing half of the conflict). - docs/README.md had the same kind of conflict — both sides listed different amendment-implementation plans (midt-bg#305 vs midt-bg#306). Kept both. - docs/architecture.md referenced ADR (0033)/(0034) but linked to the privacy ADRs at adr/0036-privacy-masking.md / 0037-centralized-x-robots-tag-worker.md. Fixed the visible numbers to (0036)/(0037) so the reader is not misled into looking up unrelated registry-evidence records. Verified with `git grep -nE '^(<{7}|={7}|>{7})'` — no conflict markers remain.
PR midt-bg#183 review (ydimitrof, BLOCKING midt-bg#1, 2026-08-18) caught the same over-masking hole that contract.tsx and contract.json.tsx already guard against: `isNaturalPersonBidder` delegates consortium filtering to the caller, so a ДЗЗД whose first member is an ЕТ ("ЕТ Иван Петров; Строй ООД") was being over-masked to "Частно лице" with a zeroed ЕИК — exactly the privacy-safe-but-information-losing case the contract siblings already prevent. Added a regression test (consortium-with-sole-trader-first-member) that asserts the loader returns the plain object with `company.eik` unchanged and no `X-Privacy-Mask` marker. Implemented the mirror guard: `company.kind !== 'consortium'` precedes the `isNaturalPersonBidder` check. Updated the inline ADR-0036 §3 comment to call out the symmetry with the contract loaders.
PR midt-bg#183 review (ydimitrof, midt-bg#5, 2026-08-18): `source()` in companies.ts was projecting `b.legal_form AS legal_form` (and doing a LEFT JOIN on bidders) on every `listCompanies` query, but `toCompanyListItem` does not consume it — only the CSV streamer (`streamCompaniesCsv`) needs it for the natural-person masker. The join is on PK so the cost was bounded, but on uncached list queries it was wasted work. Added a `legalForm` option to `source()` that controls only the unfiltered rollup subquery. The base-aggregation CTE always projects legal_form (it already INNER JOINs bidders for the GROUP BY, so the projection is free, and keeping it consistent lets both consumers share the same SQL when filters are active). Added two tests pinning the SQL shape: list path must not contain `LEFT JOIN bidders` / `b.legal_form AS legal_form` on the rollup branch; CSV path must.
…kApplied PR midt-bg#183 review (ydimitrof, midt-bg#3, 2026-08-18): the docstring on `markPrivacyMaskApplied` said "callers must invoke this only when the response body contains masked natural-person data", but `markCsvCache` in csv-export.ts invokes it unconditionally for every CSV response. The blanket call is intentional — the policy documented in `apps/web/app/routes/privacy.tsx` and `docs/privacy-masking.md` applies `noindex` to all three public CSV exports regardless of body content, because CSV is a bulk machine-readable surface — but the docstring was misleading future callers. Expanded the docstring to enumerate the two legitimate call sites (per-row maskers vs blanket-policy surfaces) and reference the policy docs. Added a regression test pinning the blanket behaviour across all three CSV routes (contracts, companies, authorities) when the body contains zero masked rows.
|
Ежедневен автономен преглед — 5-те нишки от прегледа на ydimitrof (2026-08-18) са адресирани и затворени. Commit-и:
Какво е затворено:
Верификация: HEAD на PR-а: |
lyubomir-bozhinov
left a comment
There was a problem hiding this comment.
Потвърждавам срещу HEAD — това затваря моя CHANGES_REQUESTED за детайл-страницата на договора и .data близнака:
- loader-ът нулира
bidder.eikна споделения обект и връщаResponse.jsonс маркера → един loader покрива и HTML, и.dataнаведнъж;headersexport-ът форуърдваX-Privacy-Maskявно (RR не авто-пропагира loader headers освенSet-Cookie), тъй чеhardenResponseслагаX-Robots-Tag: noindexи на двете повърхности. - consortium guard-ът (
kind !== 'consortium') пази JV с водещ ЕТ от over-mask;contract.data.test.tsфиксираeik === null+ маркера.
Проверих и bidder.slug — остава ЕИК (companySlug за eik:-ключ), но това е рутиращият ключ към noindex-натата /companies/:eik (както в company.tsx), не изтичане.
Чисто от моя страна. Мога да сменя ревюто на approve при merge, за да падне и формалният block.
ydimitrof
left a comment
There was a problem hiding this comment.
Преглед — batch 1/2: политика noindex+mask за машинно-четливи изходи (#173)
Фаза 0 — Сканиране за критична сигурност: ЧИСТО
- Няма hardcoded тайни (API ключове, пароли, токени) в дифа.
- Няма нови/променени външни URL адреси извън whitelist.
- Няма зловредни шаблони (backdoors, code injection, обфускация).
- Няма нови/променени зависимости.
- Добавеният вътрешен header
X-Privacy-Maskсе изтрива отapplyPrivacyMaskHeadersпреди отговорът да достигне edge cache/клиента — маркерът не изтича навън.
Обща оценка
Промяната е добре структурирана, атомарна и с изключително добро тестово покритие (нови тестове за security, csv-export, company.data, contract.data, contract.json). Разделянето „route-слой поставя маркер → worker-слой го превежда в X-Robots-Tag: noindex“ е чисто и добре документирано. Consortium guard-овете (kind !== 'consortium') са консистентни между CSV streamer, JSON masker, contract.tsx и company.tsx loader и са добре покрити с тестове. Документацията в privacy.tsx е ясна и на български.
Намерения
- Несъответствие в
company.tsxmeta()спрямо loader-а за консорциуми (correctness) — виж inline коментара.meta()добавяrobots: noindexза консорциум с водещо „ЕТ …“ вdisplayName, докато loader-ът умишлено НЕ маскира/noindex-ва консорциуми чрезkind !== 'consortium'guard. Това противоречи на изричния consortium guard, който е сърцевина на PR-а.
По-малки бележки (не блокиращи)
- Стилова непоследователност в подхода за noindex:
contract.json.tsxзадаваX-Robots-Tag: noindexдиректно, докатоcontract.tsx/company.tsxразчитат на вътрешния маркер, преведен от worker-а. Коментарът вcontract.data.test.tsдори твърди „loader-ът НЕ трябва да емитира X-Robots-Tag директно — това е работа на worker-а (ADR-0037)“, което се разминава с реалното поведение на.jsonroute-а. Двата подхода работят, но си струва да се уеднаквят или да се документира защо.jsonе изключение. - Липсва изричен
Cache-Controlна natural-person Response вcompany.tsxloader: връщаResponse.json(..., { headers: { 'X-Privacy-Mask': 'applied' } })безCache-Control, докатоcontract.tsxго задава изрично.headers()компенсира за document заявките, но е добре да е симетрично сcontract.tsxза яснота и за.dataпътя.
Качествени порти
- Тестове: отлично покритие, тестовете са смислени (проверяват реалното маскиране, reference-equality, идемпотентност, blanket CSV политика, 304 клон) — не са тривиални.
- Код: чист, следва установените шаблони, без дублиране (JSON masker използва shared serializer; маркерът е с единствен източник на истина в
markCsvCache). - Сигурност (agent-level): маскира се чувствителният идентификатор (ЕИК); публичните имена остават verbatim по продуктова политика (ADR-0036). Няма SQL/XSS повърхности в дифа.
- Документация:
privacy.tsxе актуализирана и съответства на кода.
Препоръка: COMMENT — едно смислено несъответствие за адресиране (meta/loader консорциуми) + две минорни бележки; в останалото PR-ът е с високо качество.
Daily autonomous review — consortium guard в
|
PR midt-bg#183 review (lyubomir-bozhinov, 2026-08-20) caught the same class of .data/HTML asymmetry as the prior consortium guards: meta() emits <meta robots noindex> for a prose-consortium (kind === 'consortium' && membershipNote), but the loader returned the plain object without the X-Privacy-Mask marker, so the worker did not stamp X-Robots-Tag: noindex on the .data twin. A crawler that doesn't honour <meta> would index the raw membershipNote (which itself can carry identifying names). TDD: three new cases in company.data.test.ts pin the new branch (marker set, ЕИК unchanged) and the negative case (membershipNote null falls through to the plain-object path). Implementation: a second guard in company.tsx loader mirrors the natural-person branch — same Response.json wrap, same marker — but without the field mutation, because the consortium ЕИК is a public legal-entity identifier and must not be zeroed. ADR-0036 §8 records the policy decision and cites the loader branch. Verified: pnpm --filter @sigma/web test (550 passing, +3), pnpm typecheck (7/7), pnpm prettier --check (clean), pnpm check:docs (ok).
…upstream Includes the prose-consortium noindex fix (1d316a3, this branch's new commit) so it lands together with the upstream sync — single integration point, single green run on CI. # Conflicts: # docs/adr/README.md
Daily autonomous review — prose-consortium noindex за
|
|
Ядрото е издържано — маскирането в CSV 1) Major — leaderboard
|
The shared toCompanyListItem (used by /companies + /companies.data and the home top-10) and toItem (used by /contracts + /contracts.data and the home single-offer tables) returned ЕИК + source name verbatim for sole traders, so the leaderboard HTML page AND its RRv7 single-fetch .data twin both served the natural-person identifier un-masked — exactly the midt-bg#173 CWE-359 class the existing CSV/JSON streamers already guard against. This is the third surface (PR midt-bg#183 review midt-bg#1). Mirror the CSV streamer guard: bidder_kind !== 'consortium' && isNaturalPersonBidder(...) zeroes ЕИК, replaces name/displayName with MASKED_NATURAL_PERSON_LABEL, and drops hasEik to false. JVs whose first member is a sole trader (e.g. 'ЕТ Иван Петров; Строй ООД') keep their consortium name + ЕИК verbatim, matching the existing consortium guards in streamContractsCsv / streamCompaniesCsv / maskContractForPrivacy. The contract list path gains a SELECT b.legal_form AS bidder_legal_form projection on the shared SELECT/FROM block (listContracts, listSingleOfferContracts, streamContractsCsv, contractsSummary) so the masker has the sole-trader signal on every list query — base-aggregation CTE was not touched, it already projects legal_form for its grouping.
…sked The leaderboard list mappers now mask sole-trader rows (prior commit), but the .data twin of /companies and /contracts is a separate machine-readable surface — search engines that don't honour the HTML meta/noindex tag would still index the masked row (PR midt-bg#183 review midt-bg#1). Stamp the internal X-Privacy-Mask: applied marker when ANY item on the page is masked, and forward it via the route's headers() export. The worker hardenResponse → applyPrivacyMaskHeaders translates the marker into X-Robots-Tag: noindex on the .data response and strips the marker before the edge cache. Mirrors the company.tsx + contract.tsx per-row pattern. Marker is internal — it never reaches the client.
…body
The masker maskContractForPrivacy widens its input to ContractRecord &
{ bidder_legal_form: string | null } so it has the sole-trader signal, but
the public ContractRecord API contract does NOT include that field — the
'not on the wire' invariant from the PR description was violated on every
loader branch:
- masked branch: ...record spread preserved the extra field → JSON body
carried the natural-person classifier alongside the masked name
- passthrough branch (legal entity / consortium): the masker returns the
record BY REFERENCE, so serializeJsonForScript(masked) serialized
bidder_legal_form straight from getContract's widened return shape
Add an explicit destructure that strips the field on every branch (TDD:
two loader tests assert body.bidder_legal_form is undefined on both the
masked sole-trader path and the legal-entity passthrough path). The
masker's own masked branch also drops the field, defense-in-depth.
|
Подходът на маскирането е правилен, но при проверка на реален D1 излизат един blocker и една дупка в доказателството около 1. Blocker (Major):
|
| Заявка | HTTP | X-Robots-Tag |
body |
|---|---|---|---|
/companies.data (маскиран ЕТ ред) |
200 | noindex |
name="Частно лице", eik=null |
/companies/<ет>.data |
200 | noindex |
eik=null, търговското име се пази (ADR-0039) |
/companies.data?count=21-100 (само ЮЛ) |
200 | липсва | ЕИК + име verbatim |
/contracts.data (ЕТ изпълнител) |
200 | noindex |
bidderName="Частно лице" |
/contracts/<id>.json (ЕТ) |
200 | noindex |
без bidder_legal_form, eik=null, име маскирано |
Т.е. RRv7 7.18 изпълнява headers export-а на .data и пренася маркера — propagation-ът работи, X-Privacy-Mask се трие от клиентския отговор. Предлагам да го заковем с integration тест (върху lane-а от #177), за да не регресира при RRv7 ъпгрейд или изпуснат headers export. Всеки ред е доказано sensitive: изключиш ли маскирането → 4-те позитивни падат; махнеш ли само headers forward-а в companies.tsx → пада точно .data noindex assert-ът.
Предложен тест + 2 helper-а (пълен код)
apps/web/test/integration/privacy-noindex-data.test.ts
// Privacy masking + `noindex` on the machine-readable twins — end-to-end through the REAL worker
// pipeline (issue #173, PR #183). This is the durable regression guard that the earlier lane
// (`apps/web/workers/app.nofollow.test.ts`) could not be: that test hand-injects the internal
// `X-Privacy-Mask` marker into a MOCKED `createRequestHandler`, so it proves the worker's
// marker→`X-Robots-Tag` translation but NOT that React Router v7's single-fetch pipeline actually
// runs a route's `headers` export (and forwards a loader-set marker) onto the `/<path>.data`
// response. RRv7's `getDocumentHeadersImpl` forwards only `Set-Cookie` by default; the `headers`
// export in `companies.tsx` / `contracts.tsx` / `company.tsx` is the explicit forward that PR #183
// relies on. Whether that forward fires on the `.data` request is the whole open question, and it
// is only answerable by driving the real handler — which the #177 integration lane does, via
// `wrangler.getPlatformProxy()` (real D1 + migrations) and `appFetch()`.
//
// Ground truth (recorded in the PR): RRv7 7.18.0 DOES run the `headers` export on `.data` and
// carries the returned marker onto the `.data` HTTP response, so `hardenResponse` translates it to
// `X-Robots-Tag: noindex` and deletes the internal marker. These assertions lock that in and would
// fail if a RRv7 upgrade, a dropped `headers` export, or a broken loader marker regressed it.
//
// The `.data` body is RRv7's single-fetch turbo-stream; `decodeSingleFetch` reconstructs the exact
// client-visible loader data so the row assertions read real objects, not a grepped flat array.
import { describe, expect, it, beforeAll } from 'vitest';
import { appFetch, seedRows } from './setup';
import { decodeSingleFetch, routeData } from './helpers/single-fetch';
// ── Fixture entities (seeded into THIS file's isolated proxy DB) ───────────────────────────────
// A sole trader (ЕТ, natural person), a legal entity (ООД, has a public ЕИК), and a consortium
// whose name leads with a sole trader — the MAJOR-class over-mask trap the `kind !== 'consortium'`
// guard exists for. ЕИК values and contract counts are chosen so each lands in a distinct
// `?count` bucket, letting a filtered request isolate a natural-person-free page.
const ET_EIK = '999000111';
const ET_NAME = 'ЕТ ДРИФТ - НИКОЛАЙ КИРОВ';
const ET_NAME_TOKEN = 'НИКОЛАЙ КИРОВ'; // the sensitive source-name fragment that must never leak
const OOD_EIK = '200000002';
const OOD_NAME = 'СТРОЙ ООД';
const CONSORTIUM_EIK = '300000003';
const CONSORTIUM_NAME = 'ЕТ Иван Петров; Строй ООД';
const CONSORTIUM_MEMBER_TOKEN = 'Строй ООД'; // a member name that must survive verbatim (not masked)
const MASK_LABEL = 'Частно лице';
const SEED: readonly string[] = [
// Sole trader — legal_form 'ЕТ' AND a leading-"ЕТ " name both flag it as a natural person.
`INSERT OR IGNORE INTO bidders (id, name, bulstat, eik_normalized, eik_valid, is_consortium, kind, legal_form)
VALUES ('eik:${ET_EIK}', '${ET_NAME}', '${ET_EIK}', '${ET_EIK}', 1, 0, 'company', 'ЕТ')`,
`INSERT OR IGNORE INTO company_totals (bidder_id, name, kind, eik, eik_valid, won_eur, contracts, authorities, eu_eur, first_date, last_date)
VALUES ('eik:${ET_EIK}', '${ET_NAME}', 'company', '${ET_EIK}', 1, 9000000.0, 5, 1, 0, '2021-01-01', '2022-12-01')`,
// Legal entity — plain ООД with a public ЕИК; must stay verbatim and out of the noindex bucket.
`INSERT OR IGNORE INTO bidders (id, name, bulstat, eik_normalized, eik_valid, is_consortium, kind, legal_form, settlement)
VALUES ('eik:${OOD_EIK}', '${OOD_NAME}', '${OOD_EIK}', '${OOD_EIK}', 1, 0, 'company', 'ООД', 'Пловдив')`,
`INSERT OR IGNORE INTO company_totals (bidder_id, name, kind, eik, eik_valid, settlement, won_eur, contracts, authorities, eu_eur, first_date, last_date)
VALUES ('eik:${OOD_EIK}', '${OOD_NAME}', 'company', '${OOD_EIK}', 1, 'Пловдив', 8000000.0, 50, 1, 0, '2020-01-01', '2022-12-28')`,
// Consortium whose lead member is a sole trader — the over-mask guard target. Kept verbatim.
`INSERT OR IGNORE INTO bidders (id, name, bulstat, eik_normalized, eik_valid, is_consortium, kind, legal_form)
VALUES ('eik:${CONSORTIUM_EIK}', '${CONSORTIUM_NAME}', '${CONSORTIUM_EIK}', '${CONSORTIUM_EIK}', 1, 1, 'consortium', 'ДЗЗД')`,
`INSERT OR IGNORE INTO company_totals (bidder_id, name, kind, eik, eik_valid, won_eur, contracts, authorities, eu_eur, first_date, last_date)
VALUES ('eik:${CONSORTIUM_EIK}', '${CONSORTIUM_NAME}', 'consortium', '${CONSORTIUM_EIK}', 1, 7000000.0, 10, 1, 0, '2021-01-01', '2022-06-01')`,
// One contract won by the sole trader — exercises /contracts.data (list mask) + /contracts/:id.json.
`INSERT OR IGNORE INTO contracts (id, tender_id, bidder_id, amount, currency, signed_at, value_flag, date_flag, amount_eur, fx_converted)
VALUES ('c:ET-1', 't:FIX-1', 'eik:${ET_EIK}', 5000000, 'BGN', '2022-06-01', 'ok', 'ok', 5000000, 0)`,
];
type ListItem = {
slug: string;
name: string;
displayName: string;
eik: string | null;
hasEik: boolean;
isConsortium: boolean;
};
type ContractItem = { id: string; bidderSlug: string; bidderName: string; bidderDisplayName: string };
async function getData(path: string): Promise<Response> {
return appFetch(new Request(`https://sigma.bg${path}`, { headers: { 'CF-Connecting-IP': '203.0.113.201' } }));
}
async function companiesItems(query = ''): Promise<{ res: Response; items: ListItem[] }> {
const res = await getData(`/companies.data${query}`);
const data = routeData<{ page: { items: ListItem[] } }>(decodeSingleFetch(await res.text()), 'routes/companies');
return { res, items: data.page.items };
}
function bySlug<T extends { slug?: string; bidderSlug?: string }>(items: T[], slug: string): T {
const hit = items.find((i) => i.slug === slug || i.bidderSlug === slug);
if (!hit) throw new Error(`row with slug ${slug} not on page; got ${items.map((i) => i.slug ?? i.bidderSlug).join(', ')}`);
return hit;
}
describe('privacy: noindex + masking on machine-readable twins (real RRv7 single-fetch)', () => {
beforeAll(async () => {
await seedRows(SEED);
});
it('the internal X-Privacy-Mask marker never reaches the client on any .data response', async () => {
for (const path of ['/companies.data', `/companies/${ET_EIK}.data`, '/contracts.data']) {
const res = await getData(path);
expect(res.headers.get('X-Privacy-Mask'), `marker leaked on ${path}`).toBeNull();
}
});
it('/companies.data — masked ЕТ page carries noindex; row is masked (name + null ЕИК)', async () => {
const { res, items } = await companiesItems();
expect(res.headers.get('X-Robots-Tag')).toBe('noindex');
const et = bySlug(items, ET_EIK);
expect(et.name).toBe(MASK_LABEL);
expect(et.displayName).toBe(MASK_LABEL);
expect(et.eik).toBeNull();
expect(et.hasEik).toBe(false);
// Sensitivity: the sensitive source name must be absent from the raw twin, not just the object.
expect(await getData('/companies.data').then((r) => r.text())).not.toContain(ET_NAME_TOKEN);
});
it('/companies/<et>.data — detail twin carries noindex; ЕИК masked, trading name preserved (ADR-0039)', async () => {
const res = await getData(`/companies/${ET_EIK}.data`);
expect(res.status).toBe(200);
expect(res.headers.get('X-Robots-Tag')).toBe('noindex');
const company = routeData<{ company: { eik: string | null; displayName: string } }>(
decodeSingleFetch(await res.text()),
'routes/company',
).company;
// The sensitive identifier (ЕИК) is masked; the trading name stays public — the `.data` twin is
// the client-nav transport that re-renders the same HTML page (ADR-0039 §3).
expect(company.eik).toBeNull();
expect(company.displayName).toContain('НИКОЛАЙ КИРОВ');
});
it('/contracts.data — page with an ЕТ bidder carries noindex; bidder masked', async () => {
const res = await getData('/contracts.data');
expect(res.headers.get('X-Robots-Tag')).toBe('noindex');
const items = routeData<{ result: { items: ContractItem[] } }>(
decodeSingleFetch(await res.text()),
'routes/contracts',
).result.items;
const et = bySlug(items, ET_EIK);
expect(et.bidderName).toBe(MASK_LABEL);
expect(et.bidderDisplayName).toBe(MASK_LABEL);
});
it('/contracts/<id>.json — ЕТ bidder masked, ЕИК null, server-only legal_form stripped, noindex', async () => {
const res = await getData('/contracts/ET-1.json');
expect(res.headers.get('X-Robots-Tag')).toBe('noindex');
const body = (await res.json()) as {
bidder: { name: string; displayName: string; eik: string | null };
sourceNames: { bidder: string };
} & Record<string, unknown>;
expect(body.bidder.name).toBe(MASK_LABEL);
expect(body.bidder.displayName).toBe(MASK_LABEL);
expect(body.bidder.eik).toBeNull();
expect(body.sourceNames.bidder).toBe(MASK_LABEL);
// The server-only natural-person classifier must never reach the client body.
expect('bidder_legal_form' in body).toBe(false);
});
it('NEGATIVE — a legal-entity-only page is NOT noindexed and keeps ЕИК + name verbatim', async () => {
// ?count=21-100 selects the ООД (50 contracts) and the base fixture company (30), excludes the
// ЕТ (5) and the consortium (10) — a page with zero natural persons. Proves the noindex signal
// is data-driven, not blanket (no over-noindexing of public companies).
const { res, items } = await companiesItems('?count=21-100');
expect(res.status).toBe(200);
expect(res.headers.get('X-Robots-Tag')).toBeNull();
const ood = bySlug(items, OOD_EIK);
expect(ood.name).toBe(OOD_NAME);
expect(ood.eik).toBe(OOD_EIK);
expect(ood.hasEik).toBe(true);
expect(items.some((i) => i.name === MASK_LABEL)).toBe(false);
});
it('GUARD — a consortium led by a sole trader is NOT over-masked (name + ЕИК kept verbatim)', async () => {
const { items } = await companiesItems();
const consortium = bySlug(items, CONSORTIUM_EIK);
expect(consortium.isConsortium).toBe(true);
expect(consortium.name).not.toBe(MASK_LABEL);
expect(consortium.name).toContain(CONSORTIUM_MEMBER_TOKEN);
expect(consortium.eik).toBe(CONSORTIUM_EIK);
});
});apps/web/test/integration/helpers/single-fetch.ts (turbo-stream декодер, за да четат ред-по-ред реалните loader данни)
// Decoder for React Router v7's single-fetch (`/<path>.data`) turbo-stream payload.
//
// RRv7 serves the `.data` twin of a document route as a flat, de-duplicated value table (its
// vendored `turbo-stream` encoding): a JSON array where index 0 is the root value and every nested
// value is referenced by its index in the array. Objects are encoded as `{ "_<keyIndex>": valueIndex }`,
// arrays as `[index, …]`, primitives (string / number / boolean / literal `null`) inline, and a
// handful of NEGATIVE indices are sentinels for the JS values that JSON can't carry
// (`undefined` / `NaN` / `±Infinity`) plus `null`.
//
// This decoder reconstructs the object graph so integration tests can assert on the real
// client-visible loader data — the same bytes a browser turns back into `loaderData` on a
// client-side navigation — instead of grepping the opaque flat array. It is intentionally minimal:
// it collapses every negative sentinel to `null`, which is all the privacy assertions need (a
// masked `eik` is `null`; an unmasked one is a non-empty string — the two never collide). If a
// future test needs to distinguish `undefined` from `null` or read a `NaN`, extend the sentinel
// handling then.
//
// The `cache` (index → built value) both memoises shared references and terminates the cyclic
// graphs RRv7 can emit (a value that refers back to an ancestor index).
export function decodeSingleFetch(text: string): Record<string, { data: unknown }> {
const arr = JSON.parse(text) as unknown[];
const cache = new Map<number, unknown>();
function build(ref: unknown): unknown {
if (typeof ref !== 'number') return ref;
if (ref < 0) return null; // turbo-stream sentinel (undefined / null / NaN / ±Infinity) → nullish
if (cache.has(ref)) return cache.get(ref);
const value = arr[ref];
if (value === null || typeof value !== 'object') {
cache.set(ref, value);
return value;
}
if (Array.isArray(value)) {
const out: unknown[] = [];
cache.set(ref, out);
for (const item of value) out.push(build(item));
return out;
}
const out: Record<string, unknown> = {};
cache.set(ref, out);
for (const [encodedKey, valueRef] of Object.entries(value)) {
// Object keys are `_<keyIndex>` — the property name itself lives in the value table.
const key = build(Number(encodedKey.slice(1)));
out[String(key)] = build(valueRef);
}
return out;
}
return build(0) as Record<string, { data: unknown }>;
}
/** Read one route's decoded loader `data` from a decoded single-fetch payload. */
export function routeData<T = unknown>(
decoded: Record<string, { data: unknown }>,
routeId: string,
): T {
const entry = decoded[routeId];
if (!entry) {
throw new Error(
`[single-fetch] route "${routeId}" not present in payload; got routes: ${Object.keys(decoded).join(', ')}`,
);
}
return entry.data as T;
}apps/web/test/integration/setup.ts — добавя seedRows() (seed на допълнителни редове в изолираната per-file D1 на lane-а):
export async function seedRows(statements: readonly string[]): Promise<void> {
const proxy = await getProxy();
// `DB.exec` treats each newline as a statement boundary, so a multi-line statement errors with
// "incomplete input". Route every statement through the same string-aware collapser the migration
// seeder uses (`stripSqlCommentsAndCollapse`) so callers can write readable multi-line SQL.
for (const stmt of statements) {
for (const collapsed of stripSqlCommentsAndCollapse(stmt)) {
await proxy.env.DB.exec(collapsed);
}
}
}
seedRowsминава statement-ите презstripSqlCommentsAndCollapse, защотоDB.execдели по нов ред.
Ако е удобно, мога да оформя fix-а като отделен commit/patch за pull.
…n't 500 PR midt-bg#183 review (lyubomir-bozhinov, 2026-08-24, MAJOR midt-bg#1): COLS in companies.ts names 'legal_form', but the rollup subquery in source() only projects it on the CSV path (3cd5d23 made it conditional to skip the LEFT JOIN on the list hot path). The conditional predated the masking mapper added in 3458dae — toCompanyListItem reads r.legal_form for the sole-trader mask, so on real D1 the SELECT fails with 'no such column: legal_form' and /companies + /companies.data return 500. The mocked-DB unit suite never executes the SQL and shipped the bug. Restore the LEFT JOIN + projection on the rollup branch (PK lookup, bounded cost), flip the unit test that asserted the broken shape, and add an end-to-end SQL test against real node:sqlite that pins the real-D1 behavior (sole trader masked, ООД verbatim, consortium not over-masked, CSV parity).
Daily autonomous review — blocker за
|
…upstream Resolve additive conflicts in test files where both sides introduced independent imports (PR midt-bg#183 MASKED_NATURAL_PERSON_LABEL + upstream fakeD1 from @sigma/test-support) and in docs/README.md (new implementation-plans/287 entry from upstream).
Какво и защо
HTML профилът на фирма вече прилага
noindexза разпознати физически лица / еднолични търговци (ЕТ),но същите идентификатори остават достъпни от машинно-четливите повърхности — JSON записът
на договора (
/contracts/:id.json) и трите CSV експорта (/contracts.csv,/companies.csv,/authorities.csv). Те връщат ЕИК и оригиналното име от източника безX-Robots-Tag: noindexи без маскиране, edge-кешират се (
Cache-Control: public, max-age=3600) и попадат в индекситена търсачките и ботовете.
Несъответствието прави идентификаторите, които HTML умишлено държи извън търсачките,
търсещи се и изтегляеми накуп — класът CWE-359, описан в issue #173.
Решение
Прилага се политика „
noindexплюс маскиране" с един общ предикат отpackages/shared/src/format.ts, който заменя досегашната дублирана логика вcompany.tsx:isNaturalPersonBidder(name, legalForm)комбинираlegal_form LIKE 'ЕТ%'(вкл. латинското
ET, разширените формиЕДНОЛИЧЕН ТЪРГОВЕЦ,SOLE TRADER,INDIVIDUAL)с водещия
ЕТсуфикс в името (който вече беше вisNaturalPersonProfileName).MASKED_NATURAL_PERSON_LABEL('Частно лице') е константа, която се внася по символ отвсеки консуматор — преименуването ѝ не чупи нито един тест. Inline
isSingleNaturalPersonProfileе премахнат от
company.tsx.packages/db/src/queries/{contracts,companies}.tsправятSELECT b.legal_formи маскиратcontractor_eik/eikиcontractor/nameпредибайтовете да стигнат до R2 — edge кешът не може никога да сервира немаскиран
естествено-личностен ред.
apps/web/app/lib/csv-export.tsдобавяX-Robots-Tag: noindexна четирите клона: 200/206 HIT, dynamic (филтриран) и 304.
packages/db/src/queries/details.tsразширяваgetContractсbidder_legal_form(server-only, не изтича към клиента —ContractRecordостава непроменен).apps/web/app/routes/contract.json.tsxизнася чистmaskContractForPrivacy(record, bidderLegalForm)помощник, който слагаX-Robots-Tag: noindexсамо когато маскиранетореално е приложило — reference-equality гейт
masked !== recordправи повторнотоизвикване на предиката ненужно.
#natural-person-dataвapps/web/app/routes/privacy.tsxописва кои полета се маскират, кои повърхностиносят
noindex-а и че HTML профилът остава непокътнат. Кадърът е инженерно ръководство,не правен съвет — същият disclaimer носи и ADR-0002.
docs/architecture.md(български, огледало на ADR-0001):Контекст / Решение / Последствия / Засегнати повърхности / Доказателство. Цитираните exit code-овеса от реален пост-едит прогон в чисто и замърсено дърво, не оценки.
Юридическите лица (
legal entity) са непроменени — ЕИК и имената им остават видимивъв всички повърхности.
Валидация (реални данни)
pnpm typecheck— exit 0 (7/7 turbo задачи).pnpm test --force— exit 1, заради 3 пред-съществуващи повреди в@sigma/db(
integrity-checks.test.tsreconciliation gate + 2 таймаута вrefresh-slice.test.ts).Същият набор е налице и в предишния
main, не е въведен от този PR.Всичките 38 нови теста минават — пост-едит и пре-едит прогонът показват същия набор
пред-съществуващи повреди, без нови въведени от този PR.
pnpm lint— exit 1, 6 prettier warnings: 2 пред-съществуващи (RiskIndicators.tsx,riskLogic.test.ts) + 4 нововъведени (contract.json.test.ts,privacy.tsx,companies.test.ts,companies.ts). Няма нови lint-видове — само пренасяне на редовезаради български / EN текст в JSX.
@sigma/shared42/42,@sigma/db(
contracts.test.ts+companies.test.ts+details.test.ts) 26/26,@sigma/web(contract.json.test.ts+csv-export.test.ts) 33/33.Извън обхвата
bidders.legal_formвече присъстваше вmigrations/0000_init.sql.ContractRecord(API contract) е непроменен от страна на клиента; полетоbidder_legal_formостава server-only (добавя се в
details.ts, използва се от route-а, не се изпраща на клиента).noindexмета-етикет минава през същия споделен предикат.Чеклист
Co-Authored-By:trailermidt-bg/sigma:mainpnpm typecheckминава; пред-съществуващитеpnpm test/pnpm lintповреди садокументирани като baseline в секция „Валидация (реални данни)" по-горе
Closes #173