CSV export returns 500 for child CREs: get_cre_path uses DB UUID instead of external ID
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 72/100
Hướng nghiên cứu
Bắt đầu tại get_cre_path trong application/database/db.py (~L2292) và so sánh với add_cre/get_path trong application/database/inmemory_graph.py (~L132-145), đoạn này dùng CRE: {cre.id} làm khóa cho các nút đồ thị bằng ID bên ngoài. Tái hiện bằng repro_get_cre_path.py đính kèm, sau đó thêm các trường hợp test_get_cre_path được đề xuất vào application/tests/db_test.py. Hoàn tất nghĩa là ba endpoint CSV trả về 200 cho một CRE con và python -m unittest application.tests.db_test chạy qua.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Issue
What is the issue?
Requesting a child (non-root) CRE in CSV format returns an HTTP 500. ExportSheet.prepare_spreadsheet() calls Node_collection.get_cre_path(), which looks up nodes in the in-memory NetworkX graph by database UUID. The graph is keyed by external CRE ID, so NetworkX raises an unhandled NodeNotFound.
Affected endpoints (format=csv, for a CRE that has a parent):
GET /rest/v1/id/<creid>GET /rest/v1/name/<crename>GET /rest/v1/text_search?text=<query>(when the results include a child CRE)
Root CREs return 200.
Expected Behaviour
get_cre_path() resolves the path between two CREs using their external IDs, and the CSV endpoints return HTTP 200 for child CREs.
Actual Behaviour
get_cre_path() passes database UUIDs to self.graph.get_path(). NetworkX raises NodeNotFound, prepare_spreadsheet() crashes, and the three endpoints above return HTTP 500.
Steps to reproduce
Environment: commit 6bc7a30e (origin/main), Python 3.11.9, application test mode (create_app(mode="test")).
- Run the attached script from the repo root with the project's virtualenv active:
python repro_get_cre_path.py(full script in the collapsed section at the bottom).
Manual equivalent:
- Create the app in test mode and a
Node_collection().with_graph(). - Add a parent CRE (
100-001) and a child CRE (100-002) linked withContains(parent -> child). - Call
collection.get_cre_path("100-001", "100-002"), or request/rest/v1/id/100-002?format=csvwithapp.testing = False.
Observed output:
graph nodes : ['CRE: 100-001', 'CRE: 100-002', 'CRE: 100-003']
external key in graph? True
UUID key in graph? False
get_cre_path('100-001', '100-002')
<repo>/application/database/db.py:2297 in get_cre_path
<repo>/application/database/inmemory_graph.py:134 in get_path
NodeNotFound: Source CRE: b8987430-303d-47be-9c19-acf025327338 is not in G
200 /rest/v1/id/100-001?format=csv
500 /rest/v1/id/100-002?format=csv
500 /rest/v1/name/Child%20CRE?format=csv
500 /rest/v1/text_search?text=Child&format=csv
Root cause
db.py#L2292-L2298:get_cre_pathconverts the external IDs to database UUIDs (CRE.id) and callsself.graph.get_path(f"CRE: {fromDbID}", f"CRE: {toDbID}").inmemory_graph.py#L138-L145:add_crekeys nodes asCRE: {cre.id}, wherecre.idis the external ID. UUIDs are never node keys.inmemory_graph.py#L132-L136:get_pathonly catchesnx.NetworkXNoPath, sonx.NodeNotFoundpropagates.spreadsheet.py#L250-L274:prepare_spreadsheethandlesdepth == 0CREs without callingget_cre_path, which is why root CREs work. Fordepth > 0it callsstorage.get_cre_path().web_main.py#L196-L200and#L638-L642: the CSV branches callprepare_spreadsheet.
Proposed fix
In db.py, make get_cre_path query the graph with the external IDs (f"CRE: {fromID}", f"CRE: {toID}") and rebuild the path with get_CREs(external_id=...). In inmemory_graph.py, make get_path also catch nx.NodeNotFound and return [].
With this patch applied locally on 6bc7a30e:
get_cre_path('100-001', '100-002') -> ['100-001', '100-002']
200 /rest/v1/id/100-001?format=csv
200 /rest/v1/id/100-002?format=csv
200 /rest/v1/name/Child%20CRE?format=csv
200 /rest/v1/text_search?text=Child&format=csv
I also added a regression test, test_get_cre_path in application/tests/db_test.py (parent->child, child->parent, same node, unrelated CREs, unknown IDs). It fails on main with NodeNotFound and passes with the patch. python -m unittest application.tests.db_test passes (62 tests).
I'll wait for maintainer feedback before opening a PR, and I'm happy to adjust the approach.
Known limitations
Verified in application test mode on a small artificial dataset. I haven't measured how the CSV export endpoints are used in practice.
repro_get_cre_path.py
"""
Reproduction script for OWASP/OpenCRE:
get_cre_path() queries the in-memory graph using DB UUID instead of external ID,
causing unhandled NodeNotFound and HTTP 500 on child CRE CSV export.
Usage:
python repro_get_cre_path.py
"""
import logging
import subprocess
import traceback
from application import create_app, sqla
from application.database import db
from application.defs import cre_defs as defs
def banner(text: str) -> None:
print("\n" + "=" * 72)
print(text)
print("=" * 72)
app = create_app(mode="test")
app.app_context().push()
logging.disable(logging.CRITICAL)
sqla.create_all()
banner("0. Commit under test")
print(
subprocess.run(
["git", "log", "-1", "--oneline"], capture_output=True, text=True
).stdout.strip()
)
collection = db.Node_collection()
collection.with_graph()
parent = collection.add_cre(
defs.CRE(id="100-001", name="Parent CRE", description="parent")
)
child = collection.add_cre(
defs.CRE(id="100-002", name="Child CRE", description="child")
)
collection.add_cre(
defs.CRE(id="100-003", name="Unrelated CRE", description="unrelated")
)
collection.add_internal_link(
higher=parent, lower=child, ltype=defs.LinkTypes.Contains
)
collection.with_graph()
banner("1. Graph keys use EXTERNAL ids, not DB UUIDs")
nodes = list(collection.graph.get_raw_graph().nodes())
print("graph nodes :", nodes)
print("parent external id :", parent.external_id)
print("parent DB UUID :", parent.id)
print("external key in graph? ", f"CRE: {parent.external_id}" in nodes)
print("UUID key in graph? ", f"CRE: {parent.id}" in nodes)
banner("2. get_cre_path('100-001', '100-002')")
try:
print([c.id for c in collection.get_cre_path("100-001", "100-002")])
except Exception as e:
for f in traceback.extract_tb(e.__traceback__):
if "application" in f.filename and ".venv" not in f.filename:
rel_path = f.filename.replace("\\", "/").split("/application/")[-1]
print(f" <repo>/application/{rel_path}:{f.lineno} in {f.name}")
print(f"{type(e).__name__}: {e}")
banner("3. REST CSV endpoints (production error handling)")
app.testing = False
client = app.test_client()
for url in [
"/rest/v1/id/100-001?format=csv",
"/rest/v1/id/100-002?format=csv",
"/rest/v1/name/Child%20CRE?format=csv",
"/rest/v1/text_search?text=Child&format=csv",
]:
resp = client.get(url, base_url="https://localhost")
print(f"{resp.status_code} {url}")
sqla.session.remove()
sqla.drop_all()
- Ngôn ngữ chính
- Python
- Star
- 216
- Fork
- 173
- Merge trung bình
- 1 ngày 21 giờ
- Pull request đã merge (30 ngày)
- 21
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Có Dockerfile hoặc tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của OWASP/OpenCRE
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
OWASP/OpenCRE#1232 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Incorrect inverse relationship type in CRE internal-link exportCó thể làm lại được @saicharan-r02 đã nhận 8 ngày trước và không có pull request nào đang mở. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
OWASP/OpenCRE#1215 · 3 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Map Analysis continues polling after a background job failsCó thể đã có người làm @abhi2005-bit đã nhận 11 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
OWASP/OpenCRE#1211 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của OWASP/OpenCRE
Issue tương tự
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
ClickHouse/dbt-clickhouse#829 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
Maintainer thường phản hồi trong vòng 1 ngày
-
triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
Maintainer thường phản hồi trong vòng 3 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
volcengine/OpenViking#8432 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 1 ngày