local-triage 0.0.0-stage → 0.10.0-dev.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (223) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +90 -2
  3. package/THIRD-PARTY-NOTICES.md +48 -0
  4. package/app/__init__.py +3 -0
  5. package/app/affiliation.py +137 -0
  6. package/app/ask.py +300 -0
  7. package/app/build_info.py +98 -0
  8. package/app/capec.py +168 -0
  9. package/app/capec_view.py +27 -0
  10. package/app/codecmp.py +59 -0
  11. package/app/config.py +411 -0
  12. package/app/context.py +1451 -0
  13. package/app/cvss.py +357 -0
  14. package/app/db.py +6234 -0
  15. package/app/defect.py +670 -0
  16. package/app/defect_check.py +323 -0
  17. package/app/defect_drafts.py +612 -0
  18. package/app/defect_dto.py +149 -0
  19. package/app/defect_fields.py +248 -0
  20. package/app/defect_form.py +124 -0
  21. package/app/defect_links.py +107 -0
  22. package/app/defect_local_text.py +103 -0
  23. package/app/defect_references.py +164 -0
  24. package/app/defect_where.py +147 -0
  25. package/app/ecosystem.py +51 -0
  26. package/app/executors.py +660 -0
  27. package/app/feeds.py +1231 -0
  28. package/app/guard.py +164 -0
  29. package/app/hub_block_builder.py +200 -0
  30. package/app/hub_block_version.py +220 -0
  31. package/app/hub_defects.py +4469 -0
  32. package/app/hub_effects.py +421 -0
  33. package/app/hub_entity_versions.py +108 -0
  34. package/app/hub_import.py +2054 -0
  35. package/app/hub_onboarding.py +114 -0
  36. package/app/hub_ops.py +330 -0
  37. package/app/hub_pull.py +1195 -0
  38. package/app/hub_state.py +255 -0
  39. package/app/hub_status.py +237 -0
  40. package/app/hub_sync.py +619 -0
  41. package/app/hub_sync_api.py +241 -0
  42. package/app/hub_sync_compare.py +1054 -0
  43. package/app/hub_sync_contract.py +75 -0
  44. package/app/hub_template_model.py +74 -0
  45. package/app/hub_text_scale.py +159 -0
  46. package/app/journal.py +250 -0
  47. package/app/llm.py +850 -0
  48. package/app/llm_chat.py +596 -0
  49. package/app/llm_http.py +319 -0
  50. package/app/llm_messages.py +401 -0
  51. package/app/llm_net.py +764 -0
  52. package/app/llm_ollama.py +571 -0
  53. package/app/lockfile.py +505 -0
  54. package/app/logsafe.py +54 -0
  55. package/app/main.py +367 -0
  56. package/app/mask_display.py +132 -0
  57. package/app/masking.py +814 -0
  58. package/app/metrics.py +194 -0
  59. package/app/model_traffic.py +519 -0
  60. package/app/models.py +483 -0
  61. package/app/navigation.py +121 -0
  62. package/app/osv.py +279 -0
  63. package/app/package_identity.py +189 -0
  64. package/app/package_transition.py +824 -0
  65. package/app/profile_gate.py +71 -0
  66. package/app/prompts.py +1108 -0
  67. package/app/provider_keys.py +189 -0
  68. package/app/providers.py +511 -0
  69. package/app/routes/__init__.py +10 -0
  70. package/app/routes/api.py +153 -0
  71. package/app/routes/capec.py +186 -0
  72. package/app/routes/common.py +407 -0
  73. package/app/routes/components.py +1499 -0
  74. package/app/routes/cwe.py +168 -0
  75. package/app/routes/decision_selected.py +153 -0
  76. package/app/routes/defect_add.py +223 -0
  77. package/app/routes/defect_card_routes.py +363 -0
  78. package/app/routes/defect_selected.py +342 -0
  79. package/app/routes/feeds.py +155 -0
  80. package/app/routes/findings.py +2736 -0
  81. package/app/routes/hub.py +402 -0
  82. package/app/routes/hub_bulk.py +288 -0
  83. package/app/routes/hub_defect_routes.py +1945 -0
  84. package/app/routes/hub_status_routes.py +1132 -0
  85. package/app/routes/hub_sync_routes.py +463 -0
  86. package/app/routes/journal.py +132 -0
  87. package/app/routes/projects.py +1601 -0
  88. package/app/routes/providers.py +336 -0
  89. package/app/routes/references.py +26 -0
  90. package/app/routes/runs.py +219 -0
  91. package/app/routes/sca.py +2722 -0
  92. package/app/routes/settings.py +163 -0
  93. package/app/run_errors.py +151 -0
  94. package/app/sca.py +1662 -0
  95. package/app/sca_contract.py +1289 -0
  96. package/app/sca_research.py +1089 -0
  97. package/app/sca_research_net.py +1007 -0
  98. package/app/sca_text.py +965 -0
  99. package/app/serve.py +70 -0
  100. package/app/settings_store.py +262 -0
  101. package/app/static/app.css +2802 -0
  102. package/app/static/app.js +3772 -0
  103. package/app/static/capec-license.txt +9 -0
  104. package/app/static/favicon.svg +1 -0
  105. package/app/static/fonts/Golos-Text_DemiBold.woff2 +0 -0
  106. package/app/static/fonts/Golos-Text_Regular.woff2 +0 -0
  107. package/app/static/fonts/OFL.txt +90 -0
  108. package/app/static/navigation.js +234 -0
  109. package/app/static/observability.js +121 -0
  110. package/app/static/theme.js +49 -0
  111. package/app/storage/__init__.py +16 -0
  112. package/app/storage/local_state.py +246 -0
  113. package/app/storage/migrate.py +110 -0
  114. package/app/storage/migrations/postgresql/0001_baseline.sql +896 -0
  115. package/app/storage/migrations/postgresql/0002_feeds_and_secrets.sql +55 -0
  116. package/app/storage/migrations/postgresql/0003_project_mask_display.sql +5 -0
  117. package/app/storage/migrations/postgresql/0004_package_identity.sql +36 -0
  118. package/app/storage/postgresql.py +573 -0
  119. package/app/storage/schema.py +619 -0
  120. package/app/storage/sql.py +57 -0
  121. package/app/storage/tools.py +254 -0
  122. package/app/telemetry.py +307 -0
  123. package/app/templates/base.html +91 -0
  124. package/app/templates/capec/context.html +3 -0
  125. package/app/templates/capec/detail.html +27 -0
  126. package/app/templates/capec/index.html +128 -0
  127. package/app/templates/components/_archive.html +11 -0
  128. package/app/templates/components/archived.html +19 -0
  129. package/app/templates/components/batch.html +35 -0
  130. package/app/templates/components/detail.html +332 -0
  131. package/app/templates/components/index.html +338 -0
  132. package/app/templates/components/research_preview.html +80 -0
  133. package/app/templates/components/research_run.html +54 -0
  134. package/app/templates/cwe/bdu.html +56 -0
  135. package/app/templates/cwe/detail.html +233 -0
  136. package/app/templates/cwe/index.html +97 -0
  137. package/app/templates/error.html +8 -0
  138. package/app/templates/feeds/index.html +65 -0
  139. package/app/templates/findings/card.html +639 -0
  140. package/app/templates/findings/decision_selected.html +41 -0
  141. package/app/templates/findings/defect_add.html +46 -0
  142. package/app/templates/findings/defect_selected.html +47 -0
  143. package/app/templates/findings/list.html +153 -0
  144. package/app/templates/findings/send_selected.html +53 -0
  145. package/app/templates/findings/trace.html +72 -0
  146. package/app/templates/journal/detail.html +19 -0
  147. package/app/templates/journal/index.html +19 -0
  148. package/app/templates/macros.html +440 -0
  149. package/app/templates/navigation-fields.html +1 -0
  150. package/app/templates/navigation.html +11 -0
  151. package/app/templates/projects/_identity_summary.html +42 -0
  152. package/app/templates/projects/defect_card.html +153 -0
  153. package/app/templates/projects/defect_create_preview.html +17 -0
  154. package/app/templates/projects/defect_fields_diff.html +6 -0
  155. package/app/templates/projects/defect_fields_form.html +40 -0
  156. package/app/templates/projects/defect_references.html +17 -0
  157. package/app/templates/projects/defect_sync_preview.html +28 -0
  158. package/app/templates/projects/defect_update_actions.html +6 -0
  159. package/app/templates/projects/defect_update_preview.html +24 -0
  160. package/app/templates/projects/defects.html +126 -0
  161. package/app/templates/projects/delete.html +31 -0
  162. package/app/templates/projects/external.html +61 -0
  163. package/app/templates/projects/folders.html +64 -0
  164. package/app/templates/projects/hub_accepted.html +10 -0
  165. package/app/templates/projects/hub_assignment.html +50 -0
  166. package/app/templates/projects/hub_pending.html +20 -0
  167. package/app/templates/projects/hub_pending_list.html +17 -0
  168. package/app/templates/projects/hub_state_note.html +23 -0
  169. package/app/templates/projects/hub_step_note.html +5 -0
  170. package/app/templates/projects/hub_sync.html +105 -0
  171. package/app/templates/projects/hub_sync_confirm.html +32 -0
  172. package/app/templates/projects/hub_sync_worker.html +5 -0
  173. package/app/templates/projects/hub_text_rules.html +64 -0
  174. package/app/templates/projects/identity_report.html +74 -0
  175. package/app/templates/projects/list.html +90 -0
  176. package/app/templates/projects/project.html +433 -0
  177. package/app/templates/reference-missing.html +14 -0
  178. package/app/templates/runs/run.html +166 -0
  179. package/app/templates/sca/card.html +476 -0
  180. package/app/templates/sca/index.html +321 -0
  181. package/app/templates/settings/hub_url.html +31 -0
  182. package/app/templates/settings/index.html +216 -0
  183. package/app/templates/settings/provider.html +229 -0
  184. package/app/templates/settings/providers.html +37 -0
  185. package/app/templates/worklists.html +52 -0
  186. package/app/textsafe.py +117 -0
  187. package/app/timing.py +245 -0
  188. package/app/triage.py +1743 -0
  189. package/app/view.py +430 -0
  190. package/app/worklists.py +204 -0
  191. package/examples/demo/demo-hub-report-v2.xlsx +0 -0
  192. package/examples/demo/demo-hub-report.xlsx +0 -0
  193. package/examples/demo/repos/billing-core/pom.xml +30 -0
  194. package/examples/demo/repos/billing-core/src/main/java/org/example/billing/Invoice.java +9 -0
  195. package/examples/demo/repos/billing-core/src/main/java/org/example/billing/InvoiceRepository.java +51 -0
  196. package/examples/demo/repos/billing-core/src/main/java/org/example/billing/InvoiceStatus.java +11 -0
  197. package/examples/demo/repos/billing-core/src/main/java/org/example/billing/config/DatabaseSettings.java +18 -0
  198. package/examples/demo/repos/billing-core/src/main/java/org/example/billing/payment/PaymentTokenService.java +35 -0
  199. package/examples/demo/repos/billing-core/src/main/java/org/example/billing/web/InvoiceExportController.java +57 -0
  200. package/examples/demo/repos/catalog-api/catalog_api/__init__.py +1 -0
  201. package/examples/demo/repos/catalog-api/catalog_api/products.py +24 -0
  202. package/examples/demo/repos/catalog-api/catalog_api/tasks.py +24 -0
  203. package/examples/demo/repos/catalog-api/catalog_api/views.py +17 -0
  204. package/examples/demo/repos/catalog-api/requirements.txt +3 -0
  205. package/npm/build.json +5 -0
  206. package/npm/cli.js +346 -0
  207. package/npm/files.manifest +224 -0
  208. package/npm/lib/binding.js +64 -0
  209. package/npm/lib/cluster.js +330 -0
  210. package/npm/lib/env.js +88 -0
  211. package/npm/lib/fsx.js +125 -0
  212. package/npm/lib/http.js +53 -0
  213. package/npm/lib/metrics.js +73 -0
  214. package/npm/lib/paths.js +150 -0
  215. package/npm/lib/pg.js +351 -0
  216. package/npm/lib/proc.js +101 -0
  217. package/npm/lib/python.js +133 -0
  218. package/npm/lib/stand.js +46 -0
  219. package/npm/lib/supervisor.js +1141 -0
  220. package/npm/lib/version.js +21 -0
  221. package/npm/requirements.txt +476 -0
  222. package/npm-shrinkwrap.json +111 -0
  223. package/package.json +37 -4
@@ -0,0 +1,168 @@
1
+ """Справочник классов CWE внутри приложения: список /cwe с поиском, фильтром и постраничностью, карточка
2
+ /cwe/{id} в духе страницы MITRE и страница записи БДУ по CVE /bdu/{cve}, на которую ссылаются наблюдаемые
3
+ примеры каталога CWE.
4
+
5
+ Источник – та же выгрузка каталога, что пользователь загружает на странице «Справочники» (app/feeds.py): полная
6
+ запись класса и связи между классами считаются при загрузке, поэтому здесь только чтение. Содержимое каталога –
7
+ данные: показывается как есть, на английском, автоэкранированием шаблона, без обращений в интернет. Русское
8
+ название рядом с английским берётся из выгрузки БДУ по тому же номеру и только при прямом совпадении.
9
+ Каталог не загружен – /cwe показывает пустое состояние, /cwe/{id} и /bdu/{cve} – 404 с подсказкой.
10
+ """
11
+
12
+ from __future__ import annotations
13
+
14
+ import re
15
+ from typing import Any
16
+
17
+ from fastapi import APIRouter, HTTPException, Request
18
+ from fastapi.responses import HTMLResponse
19
+
20
+ from app import worklists
21
+ from app.feeds import FeedStore, cwe_id
22
+ from app.routes.common import query, render
23
+ from app.routes.references import missing_reference
24
+
25
+ router = APIRouter()
26
+
27
+ PAGE_SIZE = 200 # записей каталога на страницу списка
28
+ KIND_FILTERS = {"weakness": "Weakness", "category": "Category"}
29
+ CWE_NOT_LOADED = "Каталог CWE не загружен. Загрузите архив cwec_latest.xml.zip на странице «Справочники»."
30
+ CWE_NO_SUCH = "Такого класса в каталоге нет."
31
+ BDU_NOT_LOADED = "Выгрузка БДУ ФСТЭК России не загружена. Загрузите архив vulxml.zip на странице «Справочники»."
32
+ TERMS_URL = "https://cwe.mitre.org/about/termsofuse.html"
33
+
34
+ _CVE = re.compile(r"CVE-\d{4}-\d{4,}")
35
+
36
+
37
+ def store_of(request: Request) -> FeedStore:
38
+ return request.app.state.feeds
39
+
40
+
41
+ def _filters(request: Request) -> dict[str, str]:
42
+ """Значения фильтров из адреса: неизвестный вид записи отбрасывается, а не отбирает молча."""
43
+ kind = request.query_params.get("kind", "")
44
+ return {"q": request.query_params.get("q", "")[:120].strip(),
45
+ "kind": kind if kind in KIND_FILTERS else "",
46
+ "page": request.query_params.get("page", "")[:6],
47
+ "abstraction": request.query_params.get("abstraction", "")[:40],
48
+ "status": request.query_params.get("status", "")[:40],
49
+ **worklists.options(request.query_params, {f"{k}_{d}" for k in ("id","name","kind","abstraction","status") for d in ("asc","desc")})}
50
+
51
+
52
+ def _list_url(base: dict[str, str], **changes: Any) -> str:
53
+ """Адрес списка с одним заменённым параметром: чипы-фильтры и постраничные ссылки."""
54
+ search = query(base, **changes)
55
+ return f"/cwe?{search}" if search else "/cwe"
56
+
57
+
58
+ def _matches(item: dict, text: str) -> bool:
59
+ """Поиск по номеру, английскому и русскому названию: запрос целиком из номера («CWE-89», «89») сверяется как
60
+ число, остальное – слова без учёта регистра («ISO 27001», «SHA1» ищутся в названиях)."""
61
+ key = cwe_id(text)
62
+ if key:
63
+ return item["id"] == key
64
+ needle = text.casefold()
65
+ return needle in item["name"].casefold() or needle in (item["name_ru"] or "").casefold()
66
+
67
+
68
+ def list_context(request: Request) -> dict[str, Any]:
69
+ """Список каталога: поиск по номеру и названиям, фильтр вида записи, постраничность, сведения о загрузке."""
70
+ filters = _filters(request)
71
+ with store_of(request).snapshot() as store:
72
+ meta = store.status().get("cwe", {})
73
+ items = store.cwe_list()
74
+ counts = {"weakness": sum(i["kind"] == "weakness" for i in items),
75
+ "category": sum(i["kind"] == "category" for i in items)}
76
+ choices = {key: sorted({i.get(key, "") for i in items if i.get(key)}) for key in ("abstraction", "status")}
77
+ for key in ("kind", "abstraction", "status"):
78
+ if filters.get(key):
79
+ items = [i for i in items if i.get(key) == filters[key]]
80
+ if filters["q"]:
81
+ items = [i for i in items if _matches(i, filters["q"])]
82
+ sort = filters["sort"] or "id_asc"
83
+ key, direction = sort.rsplit("_", 1)
84
+ items = sorted(items, key=lambda i: int(i["id"].split("-")[-1]) if key == "id" else i.get(key, "").casefold(),
85
+ reverse=direction == "desc")
86
+ filters["sort"] = sort
87
+ w = worklists.table(items, filters, path="/cwe", default_size=PAGE_SIZE)
88
+ return {"meta": meta, "loaded": bool(meta), "filters": filters, "kind_filters": KIND_FILTERS,
89
+ "choices": choices, "counts": counts, "catalog_size": sum(counts.values()), "worklist": w,
90
+ "prev_url": w["prev"], "next_url": w["next"], "page": w["page"], "pages": w["pages"],
91
+ "total": w["total"], "rows": w["rows"], "terms_url": TERMS_URL}
92
+
93
+
94
+ def detail_context(request: Request, number: str) -> dict[str, Any]:
95
+ """Карточка класса: полная запись, связи, источники из справочника ссылок, сведения о версии каталога –
96
+ одним снимком справочника, чтобы загрузка нового каталога не смешала версии на одной странице."""
97
+ with store_of(request).snapshot() as store:
98
+ return _detail(store, number)
99
+
100
+
101
+ def _detail(store: FeedStore, number: str) -> dict[str, Any]:
102
+ if not store.status().get("cwe"):
103
+ raise HTTPException(status_code=404, detail=CWE_NOT_LOADED)
104
+ page = store.cwe_page(number)
105
+ if page is None:
106
+ raise HTTPException(status_code=404, detail=CWE_NO_SUCH)
107
+ record = dict(page["record"])
108
+ record['attack_patterns'] = sorted(store.capec_for_cwes([record['id']]), key=lambda x:int(x.split('-')[1]))
109
+ # запись прежней версии приложения хранит расширенное описание строкой, полная – списком абзацев;
110
+ # карточка показывает в обоих случаях список
111
+ extended = record.get("extended")
112
+ if isinstance(extended, str):
113
+ record["extended"] = [extended] if extended.strip() else []
114
+ refs = store.cwe_refs([r["ref_id"] for r in record.get("references", [])])
115
+ # ссылки из блоков примеров – из того же справочника ссылок, без повторов
116
+ example_ids = [r["ref_id"] for ex in record.get("examples", []) for r in ex.get("refs", [])]
117
+ refs.update(store.cwe_refs([i for i in example_ids if i not in refs]))
118
+ record["references"] = [{**r, "source": refs.get(r["ref_id"], {})} for r in record.get("references", [])]
119
+ for ex in record.get("examples", []):
120
+ for ref in ex.get("refs", []):
121
+ ref["source"] = refs.get(ref["ref_id"], {})
122
+ # наблюдаемые примеры: страница БДУ по CVE есть только там, где выгрузка БДУ содержит такую запись
123
+ observed = record.get("observed", [])
124
+ return {"record": record, "links": page["links"], "name_ru": page["name_ru"],
125
+ "bdu": store.bdu_by_cve([o.get("reference", "") for o in observed]),
126
+ "meta": store.status().get("cwe", {}), "terms_url": TERMS_URL,
127
+ # запись прежней версии приложения: новых полей в ней нет, страница показывает то, что есть
128
+ "old": not record.get("full")}
129
+
130
+
131
+ def bdu_context(request: Request, cve: str) -> dict[str, Any]:
132
+ """Записи БДУ по CVE: то, что выгрузка содержит по этому идентификатору."""
133
+ key = str(cve or "").strip().upper()[:40]
134
+ if not _CVE.fullmatch(key):
135
+ raise HTTPException(status_code=404, detail="Это не номер уязвимости CVE.")
136
+ with store_of(request).snapshot() as store:
137
+ meta = store.status().get("bdu")
138
+ found = store.bdu_by_cve([key]).get(key, []) if meta else []
139
+ if not meta:
140
+ raise HTTPException(status_code=404, detail=BDU_NOT_LOADED)
141
+ if not found:
142
+ raise HTTPException(status_code=404, detail=f"В выгрузке БДУ нет записей по {key}.")
143
+ return {"cve": key, "records": found, "meta": meta}
144
+
145
+
146
+ @router.get("/cwe", response_class=HTMLResponse)
147
+ def cwe_page(request: Request):
148
+ return render(request, "cwe/index.html", list_context(request))
149
+
150
+
151
+ @router.get("/cwe/{cwe_id}", response_class=HTMLResponse)
152
+ def cwe_detail(request: Request, cwe_id: str):
153
+ """Карточка класса: номер принимается как «CWE-89» и как «89»."""
154
+ try:
155
+ context = detail_context(request, cwe_id)
156
+ except HTTPException as exc:
157
+ if exc.status_code != 404:
158
+ raise
159
+ from app.feeds import cwe_id as normalize
160
+ return missing_reference(request, "cwe", cwe_id, normalize(cwe_id), exc.detail,
161
+ store_of(request).status().get("cwe"))
162
+ return render(request, "cwe/detail.html", context)
163
+
164
+
165
+ @router.get("/bdu/{cve}", response_class=HTMLResponse)
166
+ def bdu_page(request: Request, cve: str):
167
+ """Запись БДУ по CVE: на неё ссылаются наблюдаемые примеры каталога CWE."""
168
+ return render(request, "cwe/bdu.html", bdu_context(request, cve))
@@ -0,0 +1,153 @@
1
+ """Одно решение на отмеченные находки SAST (план dev7, раздел 7), по образцу пакетного решения SCA: один вывод, текст и рекомендация
2
+ сразу на отмеченные находки, с предпросмотром состава и отказом при устаревшей версии находки или вердикта. В Hub ничего не уходит."""
3
+
4
+ from fastapi import APIRouter, Request
5
+ from starlette.concurrency import run_in_threadpool
6
+
7
+ from app import defect, hub_defects, hub_status
8
+ from app.db import source_version
9
+ from app.hub_entity_versions import digest
10
+ from app.models import Finding
11
+ from app.routes import defect_selected, hub_defect_routes as flow
12
+ from app.routes.common import db_of, project_or_404, redirect, register_messages, render
13
+ from app.routes.hub_bulk import MAX_SELECTED
14
+
15
+ router = APIRouter()
16
+ OUTCOMES = {"false_positive": "Ложное срабатывание", "true_positive": "Подтверждаю дефект"}
17
+ STALE = "Состав, данные или решения изменились после показа: проверьте состав и подтвердите ещё раз. Ничего не сохранено."
18
+ register_messages({"decide_selected": "Решение сохранено на всех находках показанного состава."})
19
+
20
+
21
+ def assess(request, c, project, selected, outcome, comment, fix):
22
+ """Состав: кто войдёт, кто исключён и почему, чьи решения будут заменены. snapshot – отпечаток показанного состояния."""
23
+ db, pid = db_of(request), project["id"]
24
+ ready, excluded, replacements = [], [], []
25
+ for hub_id in selected:
26
+ row = db.get_finding_by_hub_id(pid, hub_id)
27
+ url = flow._card_url_of(db, pid, hub_id) if row else ""
28
+ try:
29
+ if row is None or row.get("missing"):
30
+ raise hub_defects.DefectRefused("находки нет в текущей выгрузке")
31
+ if row["kind"] != "SAST":
32
+ raise hub_defects.DefectRefused("не SAST: решение по SCA – в карточке версии компонента")
33
+ bound = db.hub_defect_bound_in(c, pid, hub_id)
34
+ if bound is not None: # решение находки в дефекте – через дефект
35
+ raise hub_defects.DefectRefused(f"входит в {hub_defects.bound_name(bound)}: решение меняется в карточке находки")
36
+ ident = hub_status.issue_id(hub_id)
37
+ why = defect_selected.busy_reason(db, c, pid, hub_id, ident)
38
+ if why:
39
+ raise hub_defects.DefectRefused(why)
40
+ snap = defect_selected.target_snapshot(request, c, pid, hub_id)
41
+ except ValueError as e:
42
+ excluded.append({"hub_id": hub_id, "why": str(e), "url": url})
43
+ continue
44
+ old = snap["review"]
45
+ case = snap["case"] or {}
46
+ ready.append({"hub_id": hub_id, "url": url, "finding": Finding.from_dict(row["data"]), "snapshot": snap,
47
+ "version": source_version(snap["source"]), "revision": old.get("revision", 0),
48
+ "verdict_id": case.get("verdict_id"), "code_hash": snap["code"]["hash"], "code_chars": snap["code"]["chars"]})
49
+ if (old.get("decision") or old.get("final_comment")) and hub_defects.decision_changes(
50
+ old, "disagree", outcome, comment, fix if outcome == "true_positive" else ""):
51
+ replacements.append({"hub_id": hub_id, "revision": old.get("revision", 0),
52
+ # «Отложено» и «Риск принят» – сам вид решения, вердикт при них прежний
53
+ "outcome": (old.get("decision") if old.get("decision") in ("later", "risk_accepted")
54
+ else old.get("final_verdict") or old.get("decision")),
55
+ "comment": old.get("final_comment"), "fix": old.get("final_fix")})
56
+ state = {"selected": selected, "outcome": outcome, "comment": comment, "fix": fix,
57
+ "ready": [{k: r[k] for k in ("hub_id", "version", "revision", "verdict_id", "code_hash", "code_chars")} for r in ready],
58
+ "excluded": excluded}
59
+ return {"ready": ready, "excluded": excluded, "replacements": replacements, "snapshot": digest(state)}
60
+
61
+
62
+ def text_refusal(request, project_id, view, outcome, comment, fix) -> str:
63
+ """Что мешает сохранить: те же требования, что в карточке находки."""
64
+ from app.routes import findings as card
65
+ if outcome not in OUTCOMES:
66
+ return "Выберите решение: «Ложное срабатывание» или «Подтверждаю дефект»."
67
+ if not defect.reason_text(comment):
68
+ return card.SAST_NEED_REASON[outcome]
69
+ if outcome == "true_positive" and not defect.has_recommendation(fix):
70
+ return card.SAST_NEED_FIX
71
+ if len(comment) > card.MAX_COMMENT or len(fix) > card.MAX_FIX:
72
+ return "Текст решения слишком длинный: сократите его."
73
+ if not view["ready"]:
74
+ return "Нет находок, на которые можно сохранить решение."
75
+ db = db_of(request)
76
+ for r in view["ready"]:
77
+ origin = card.comment_origin(db, r["snapshot"]["case"], r["snapshot"]["review"], comment, project_id=project_id, hub_id=r["hub_id"])
78
+ if origin and origin != outcome:
79
+ return f"{r['hub_id']}: текст написан моделью к другому исходу, поправьте его."
80
+ return ""
81
+
82
+
83
+ def apply(request, project_id, selected, outcome, comment, fix, shown, confirmed, overwrite) -> int:
84
+ from app.routes import findings as card
85
+ db = db_of(request)
86
+ project = project_or_404(request, project_id)
87
+ view = db.on_connection(lambda c: assess(request, c, project, selected, outcome, comment, fix))
88
+ refusal = text_refusal(request, project_id, view, outcome, comment, fix)
89
+ if refusal:
90
+ raise hub_defects.DefectRefused(refusal)
91
+ if not shown or shown != view["snapshot"]:
92
+ raise hub_defects.DefectRefused(STALE)
93
+ if not confirmed:
94
+ raise hub_defects.DefectRefused("Подтвердите состав и общее решение.")
95
+ if view["replacements"] and not overwrite:
96
+ raise hub_defects.DefectRefused("Подтвердите замену перечисленных решений.")
97
+ items = []
98
+ for r in view["ready"]:
99
+ snap, old = r["snapshot"], r["snapshot"]["review"]
100
+ case = snap["case"] or {}
101
+ decision, chosen = card.sast_choice(outcome, "", case.get("verdict"), snap["code"]["stale_verdict"], old,
102
+ case.get("verdict_id"))
103
+ items.append(dict(hub_id=r["hub_id"], decision=decision, final_verdict=chosen, final_comment=comment,
104
+ final_fix=fix if outcome == "true_positive" else "", reviewed=True, verdict_id=case.get("verdict_id"),
105
+ expect={"version": r["version"], "revision": r["revision"], "verdict_id": case.get("verdict_id")},
106
+ code_hash=r["code_hash"], code_chars=r["code_chars"], require_present=True, recheck=True))
107
+
108
+ def check(c) -> None: # всё или ничего: состав перечитывается в транзакции записи
109
+ if assess(request, c, project, selected, outcome, comment, fix)["snapshot"] != shown:
110
+ raise hub_defects.DefectRefused(STALE)
111
+
112
+ db.save_reviews(project_id, items, check=check)
113
+ return len(items)
114
+
115
+
116
+ def page(request, project_id, selected, outcome="", comment="", fix="", *, confirm=False, error="", status=200):
117
+ project, db = project_or_404(request, project_id), db_of(request)
118
+ view = db.on_connection(lambda c: assess(request, c, project, selected, outcome, comment, fix))
119
+ if confirm:
120
+ refusal = text_refusal(request, project_id, view, outcome, comment, fix)
121
+ if refusal:
122
+ error, confirm, status = refusal, False, 400
123
+ return render(request, "findings/decision_selected.html", dict(project=project, selected=selected, outcome=outcome, outcomes=OUTCOMES,
124
+ comment=comment, fix=fix, confirm=confirm, error=error, **view), status_code=status)
125
+
126
+
127
+ @router.get("/projects/{project_id}/hub/decide-selected")
128
+ async def selected_page(request: Request, project_id: int):
129
+ try:
130
+ selected = defect_selected.selection(request.query_params.getlist("hub_id"))
131
+ except ValueError as e:
132
+ return await run_in_threadpool(page, request, project_id, [], error=str(e), status=400)
133
+ return await run_in_threadpool(page, request, project_id, selected)
134
+
135
+
136
+ @router.post("/projects/{project_id}/hub/decide-selected")
137
+ async def selected_submit(request: Request, project_id: int):
138
+ form = await request.form(max_fields=MAX_SELECTED + 20, max_files=0)
139
+ selected = form.getlist("hub_id")
140
+ outcome = str(form.get("outcome") or "")
141
+ comment = str(form.get("comment") or "").replace("\r\n", "\n")
142
+ fix = str(form.get("fix") or "").replace("\r\n", "\n")
143
+ try:
144
+ selected = defect_selected.selection(selected)
145
+ if form.get("action") == "apply":
146
+ await run_in_threadpool(apply, request, project_id, selected, outcome, comment, fix, str(form.get("snapshot") or ""),
147
+ form.get("confirm") == "1", form.get("overwrite") == "1")
148
+ return redirect(f"/projects/{project_id}/findings?kind=SAST&ok=decide_selected")
149
+ return await run_in_threadpool(page, request, project_id, selected, outcome, comment, fix, confirm=form.get("action") == "preview")
150
+ except ValueError as e:
151
+ if len(set(selected)) > MAX_SELECTED:
152
+ selected = []
153
+ return await run_in_threadpool(page, request, project_id, selected, outcome, comment, fix, error=str(e), status=409)
@@ -0,0 +1,223 @@
1
+ """«Добавить в дефект» (ADR-0136): находка с другой строки входит в дефект, который оператор выбирает сам, в том числе занесённый из
2
+ Hub и обновлённый через Push.
3
+
4
+ Выбор дефекта, предпросмотр, операция `POST /defects/{id}/link`, сверка чтением – та же цепочка, что у привязки соседа по группе
5
+ (`hub_defect_link_start`). Текст дефекта не меняется, решение находки остаётся её. Состав у владельца пополняет «Pull из Hub» и
6
+ «Применить»: строка привязки живёт отдельно до этого шага.
7
+ """
8
+
9
+ import json
10
+
11
+ from fastapi import APIRouter, Request
12
+ from starlette.concurrency import run_in_threadpool
13
+
14
+ from app import defect, defect_drafts, defect_where, hub_defects, hub_status, hub_sync
15
+ from app.db import Database, hub_verdict, source_version, text_hash
16
+ from app.hub_entity_versions import digest
17
+ from app.models import Finding
18
+ from app.routes import defect_selected
19
+ from app.routes import hub_defect_routes as flow
20
+ from app.routes.common import db_of, project_or_404, redirect, register_messages, render, settings_of
21
+ from app.routes.hub_status_routes import hub_target_check, send_blocker
22
+
23
+ router = APIRouter()
24
+ STALE = "Дефект, находка или решение изменились после показа: проверьте и подтвердите ещё раз. В Hub ничего не ушло."
25
+ register_messages({"defect_add_queued": "Поставлено в очередь исполнителю: добавление находки в дефект."})
26
+
27
+
28
+ def not_eligible(c, row: dict, hub_id: str) -> str:
29
+ """Почему дефект нельзя выбрать; "" – можно. Причина – для оператора: ограничение видно, а не молча скрыто."""
30
+ if row.get("kind") != "SAST":
31
+ return "дефект не SAST"
32
+ if row.get("state") != "done" or not row.get("hub_defect_id"):
33
+ return f"состояние «{hub_defects.STATE_LABELS.get(row.get('state'), row.get('state'))}»: нужен созданный дефект, подтверждённый чтением"
34
+ return hub_defects.family_running_problem(c, row) or hub_defects.add_problem(c, row, hub_id)
35
+
36
+
37
+ def defects_of(c, project_id: int, settings, app_id, hub_id: str) -> tuple[list[dict], list[dict]]:
38
+ """Дефекты SAST того же Hub и приложения: (можно выбрать, нельзя – с причиной). Только строки-владельцы: дефект – это семья строк."""
39
+ rows = [dict(r) for r in c.execute(
40
+ "SELECT * FROM hub_defects WHERE project_id = ? AND kind = 'SAST' AND (draft_owner_id IS NULL OR draft_owner_id = id)"
41
+ " AND state NOT IN ('cancelled', 'failed', 'deleted', 'import_reading', 'import_preview') ORDER BY id", (project_id,))]
42
+ ready, closed = [], []
43
+ for row in rows:
44
+ if not flow._same_hub(row, settings.hub_url, app_id):
45
+ continue
46
+ why = not_eligible(c, row, hub_id)
47
+ item = {"id": row["id"], "label": hub_defects.bound_label(row), "summary": row.get("summary") or "", "why": why}
48
+ (closed if why else ready).append(item)
49
+ return ready, closed
50
+
51
+
52
+ def _number_unique(c, project_id: int, hub_id: str) -> None:
53
+ """Номер находки в запросе – числовой и однозначный: как у отмеченных находок (defect_selected)."""
54
+ ident = hub_status.issue_id(hub_id)
55
+ same = 0
56
+ for raw in c.execute("SELECT hub_id FROM findings WHERE project_id = ? AND kind = 'SAST'", (project_id,)):
57
+ try:
58
+ same += hub_status.issue_id(raw["hub_id"]) == ident
59
+ except hub_status.StatusRefused:
60
+ continue
61
+ if same != 1:
62
+ raise hub_defects.DefectRefused("Номер находки неоднозначен после нормализации: привязка не выполняется.")
63
+
64
+
65
+ def last_attempt(c, project_id: int, hub_id: str) -> list:
66
+ """Последняя строка дефекта с этой находкой в любом состоянии, в том числе отказ и отмена. Семья их не считает, а форма, показанная до
67
+ завершённой попытки, не должна отправить запрос второй раз: после итога нужен новый предпросмотр."""
68
+ row = c.execute(
69
+ "SELECT id, state FROM hub_defects WHERE project_id = ? AND EXISTS ("
70
+ " SELECT 1 FROM json_array_elements(hub_defects.targets_json::json) AS t(value) WHERE (t.value ->> 'hub_id') = ?)"
71
+ " ORDER BY id DESC LIMIT 1", (project_id, hub_id)).fetchone()
72
+ return [row["id"], row["state"]] if row else []
73
+
74
+
75
+ def assess(request, c, project, hub_id: str, parent_id: int | None) -> dict:
76
+ """Что покажет страница и что проверит запись: находка, дефекты, цель, запрос, отпечаток показанного состояния."""
77
+ from app.routes import findings as card
78
+ db, settings, pid = db_of(request), settings_of(request), int(project["id"])
79
+ app_id = int(project.get("hub_app_id") or 0)
80
+ found = db.get_finding_by_hub_id(pid, hub_id)
81
+ view = {"hub_id": hub_id, "finding": None, "why": "", "ready": [], "closed": [], "parent": None, "target": None, "text": "",
82
+ "snapshot": "", "link": None, "link_body": "", "retry_note": ""}
83
+ if found is None or found.get("missing"):
84
+ view["why"] = "Находки нет в текущей выгрузке."
85
+ return view
86
+ if found["kind"] != "SAST":
87
+ view["why"] = "Добавить в дефект можно находку SAST."
88
+ return view
89
+ f = Finding.from_dict(found["data"])
90
+ view["finding"] = f
91
+ review = db.get_review(pid, hub_id) or {}
92
+ bound = Database.hub_defect_bound_in(c, pid, hub_id)
93
+ try:
94
+ if bound is not None:
95
+ raise hub_defects.DefectRefused(f"Находка уже входит в {hub_defects.bound_name(bound)}.")
96
+ if hub_verdict(review.get("decision") or "", review.get("final_verdict") or "") != "true_positive" or not review.get("reviewed"):
97
+ raise hub_defects.DefectRefused("Сначала сохраните решение «Подтверждаю дефект» с отметкой «Проверено».")
98
+ _number_unique(c, pid, hub_id)
99
+ busy = defect_selected.busy_reason(db, c, pid, hub_id, hub_status.issue_id(hub_id))
100
+ if busy:
101
+ raise hub_defects.DefectRefused(f"Находка {busy}.")
102
+ source = db.case_input(pid, hub_id)
103
+ case = db.get_case(pid, hub_id)
104
+ state = card.sast_assess(request, pid, review, case, source)["state"]
105
+ if state != "ok":
106
+ raise hub_defects.DefectRefused(card.sast_state_hint(state, review, card.SAST_DEFECT_NOT_READY))
107
+ origin = card.comment_origin(db, case, review, review.get("final_comment") or "", project_id=pid, hub_id=hub_id)
108
+ if origin and origin != "true_positive": # с такой находкой в составе Push дефекта откажет
109
+ raise hub_defects.DefectRefused("Обоснование – текст модели к другому вердикту: поправьте комментарий в карточке и сохраните решение.")
110
+ snapshot = defect_selected.target_snapshot(request, c, pid, hub_id)
111
+ stored = db.defect_number(pid, defect.defect_key_sast(hub_id))
112
+ except ValueError as e:
113
+ view["why"] = str(e)
114
+ return view
115
+ prior = db.hub_defect_for(pid, [hub_id])
116
+ if prior is not None and prior["state"] == "failed" and prior.get("confirm_op_id"):
117
+ # чтение не показало находку в дефекте, но POST мог выполниться (в составе ответа только открытые находки)
118
+ view["retry_note"] = (f"Прошлая попытка: {prior.get('error') or 'находки нет в составе ответа Hub'}. Запрос мог выполниться: "
119
+ "проверьте дефект в Hub до новой попытки.")
120
+ view["ready"], view["closed"] = defects_of(c, pid, settings, app_id, hub_id)
121
+ if parent_id is None:
122
+ return view
123
+ row = c.execute("SELECT * FROM hub_defects WHERE id = ? AND project_id = ?", (int(parent_id), pid)).fetchone()
124
+ parent = dict(row) if row else None
125
+ if parent is None or not any(item["id"] == parent["id"] for item in view["ready"]):
126
+ closed = next((item["why"] for item in view["closed"] if parent and item["id"] == parent["id"]), "")
127
+ view["why"] = f"Этот дефект сейчас нельзя выбрать: {closed}" if closed else "Этот дефект сейчас нельзя выбрать: выберите другой."
128
+ return view
129
+ if stored is not None and stored != parent.get("number"):
130
+ view["why"] = f"У находки уже есть свой номер ID{stored}: объединение номеров не выполняется автоматически, снимите номер в её карточке."
131
+ return view
132
+ text = card.sast_entered_text(db, settings, pid, f, review)
133
+ try:
134
+ flow.group_defect_check(db, pid, hub_id, text, [hub_id], hub_url=settings.hub_url, app_id=app_id, selected_owner=int(parent["id"]))(c)
135
+ except ValueError as e:
136
+ view["why"] = str(e)
137
+ return view
138
+ target = flow._defect_target(f, review, text, flow.defect_parts(settings, review), db.finding_sig(pid, hub_id))
139
+ blank = hub_defects.loads(parent.get("blank_json"), {}) or {}
140
+ link = hub_defects.link_request(str(parent["hub_defect_id"]), [target], blank.get("groupId"))
141
+ view.update(parent=parent, target=target, text=text, link=link, link_body=json.dumps(link["body"], ensure_ascii=False, indent=2),
142
+ snapshot=digest({
143
+ # весь показанный запрос: поля нет и поле со значением null – разные тела
144
+ "request": link, "attempt": last_attempt(c, pid, hub_id), "hub_id": hub_id, "parent": parent["id"], "hub_defect_id": parent["hub_defect_id"], "summary": parent.get("summary"),
145
+ "number": parent.get("number"), "accepted": text_hash(str(parent.get("confirmed_object_json") or "")),
146
+ "generation": parent.get("text_generation"), "defect_text": parent.get("command_text"), "draft_revision": parent.get("draft_revision"),
147
+ "members": defect_drafts.members_in(c, parent), "revision": review.get("revision"), "version": source_version(snapshot["source"]),
148
+ "code": snapshot["code"]["hash"], "text": text}))
149
+ return view
150
+
151
+
152
+ def apply(request, project_id: int, hub_id: str, parent_id: int | None, shown: str, confirmed: bool) -> dict:
153
+ if parent_id is None:
154
+ raise hub_defects.DefectRefused("Выберите дефект.")
155
+ db = db_of(request)
156
+ project = project_or_404(request, project_id)
157
+ settings = settings_of(request)
158
+ blocker = send_blocker(settings, project) or hub_sync.resync_blocker(db, project_id)
159
+ if blocker:
160
+ raise hub_defects.DefectRefused(blocker)
161
+ db.hub_expire_leases()
162
+ view = db.on_connection(lambda c: assess(request, c, project, hub_id, parent_id))
163
+ if view["why"]:
164
+ raise hub_defects.DefectRefused(view["why"])
165
+ if not shown or shown != view["snapshot"]:
166
+ raise hub_defects.DefectRefused(STALE)
167
+ if not confirmed:
168
+ raise hub_defects.DefectRefused("Подтвердите добавление находки в дефект.")
169
+ parent, target = view["parent"], view["target"]
170
+ defect_id, app_id = str(parent["hub_defect_id"]), int(project["hub_app_id"])
171
+ item = dict(purpose="defect_link", request=view["link"], hub_url=settings.hub_url, app_id=app_id, project_id=project_id,
172
+ targets=[target], text=str(parent.get("summary") or ""))
173
+ fresh = flow._fresh_check(project_id, [target], True)
174
+ hub_target = hub_target_check(request, project_id, settings.hub_url, app_id)
175
+
176
+ def check(c) -> None: # всё показанное – ещё раз в транзакции постановки
177
+ hub_target(c)
178
+ fresh(c)
179
+ again = assess(request, c, project, hub_id, parent_id)
180
+ if again["why"]:
181
+ raise hub_defects.DefectRefused(again["why"])
182
+ if again["snapshot"] != shown:
183
+ raise hub_defects.DefectRefused(STALE)
184
+
185
+ return db.hub_defect_link_start(
186
+ project_id, int(parent["id"]), defect_id, item=item, body_hash=text_hash(defect_where.body_of(parent.get("command_text"))),
187
+ hub_link=str(parent.get("hub_link") or ""), check=check, allow_imported=True, kind="SAST", number=parent.get("number"),
188
+ summary=str(parent.get("summary") or ""), anchor_hub_id=hub_id, targets=[target], number_keys=[defect.defect_key_sast(hub_id)],
189
+ command_text=str(parent.get("command_text") or ""), hub_url=settings.hub_url, app_id=app_id, left_out=[], warnings=[])
190
+
191
+
192
+ def page(request, project_id: int, hub_id: str, parent_id: int | None = None, *, confirm=False, error="", status=200):
193
+ project, db = project_or_404(request, project_id), db_of(request)
194
+ view = db.on_connection(lambda c: assess(request, c, project, hub_id, parent_id))
195
+ blocker = send_blocker(settings_of(request), project) or hub_sync.resync_blocker(db, project_id)
196
+ if confirm and not view["parent"]:
197
+ error, confirm, status = (error or view["why"] or "Выберите дефект."), False, 400
198
+ return render(request, "findings/defect_add.html", dict(project=project, confirm=confirm, error=error, blocker=blocker,
199
+ card_url=flow._card_url_of(db, project_id, hub_id), **view), status_code=status)
200
+
201
+
202
+ def _parent_id(value) -> int | None:
203
+ text = str(value or "")
204
+ return int(text) if text.isdigit() else None
205
+
206
+
207
+ @router.get("/projects/{project_id}/hub/add-to-defect")
208
+ async def add_page(request: Request, project_id: int):
209
+ return await run_in_threadpool(page, request, project_id, str(request.query_params.get("hub_id") or ""))
210
+
211
+
212
+ @router.post("/projects/{project_id}/hub/add-to-defect")
213
+ async def add_submit(request: Request, project_id: int):
214
+ form = await request.form(max_fields=8, max_files=0)
215
+ hub_id, parent_id = str(form.get("hub_id") or ""), _parent_id(form.get("defect"))
216
+ try:
217
+ if form.get("action") == "apply":
218
+ await run_in_threadpool(apply, request, project_id, hub_id, parent_id, str(form.get("snapshot") or ""), form.get("confirm") == "1")
219
+ back = flow._card_url_of(db_of(request), project_id, hub_id)
220
+ return redirect(f"{back}?ok=defect_add_queued#hub-defect")
221
+ return await run_in_threadpool(page, request, project_id, hub_id, parent_id, confirm=form.get("action") == "preview")
222
+ except ValueError as e:
223
+ return await run_in_threadpool(page, request, project_id, hub_id, parent_id, error=str(e), status=409)