From 4e97553f42be3fb0994ba2dc2acbc957f47cc62f Mon Sep 17 00:00:00 2001 From: caohuipeng Date: Thu, 8 Oct 2026 13:14:47 +0800 Subject: [PATCH] =?UTF-8?q?fix(shipping-notice):=20=E6=94=B6=E6=96=99?= =?UTF-8?q?=E5=9B=9E=E8=B0=83=E8=B7=AF=E5=BE=84=E5=A5=91=E7=BA=A6=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=20drop=20=E6=A0=B9=E5=86=85=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修复跨机成功回调全程 400:Worker(Windows)与后端(Linux 挂载根)分属两机根路径不同,后端无法解析 Worker 本机绝对路径 - Worker 只上报文件名(output.name),后端按自身挂载的 drop 根拼接定位;历史载荷携带绝对路径亦按末段文件名兼容解析 - 同步收料 worker 路径契约注释与跨机路径回归测试 --- .../shipping_notice/receipt_report_storage.py | 12 ++++++++-- .../test_freight_receipt_rpa_callback.py | 24 +++++++++++++++++++ .../freight_rpa/receipt/receipt_rpa_worker.py | 8 +++++-- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/backend/app/services/shipping_notice/receipt_report_storage.py b/backend/app/services/shipping_notice/receipt_report_storage.py index 11019780..2ad47d85 100644 --- a/backend/app/services/shipping_notice/receipt_report_storage.py +++ b/backend/app/services/shipping_notice/receipt_report_storage.py @@ -29,12 +29,20 @@ from app.services.shipping_notice.audit_service import append_shipping_audit_eve def _read_receipt_callback_file(file_path: str) -> bytes: - """只读取收料专用 drop 根内的 xlsx,拒绝 callback 任意本机文件读取。""" + """只读取收料专用 drop 根内的 xlsx,拒绝 callback 任意本机文件读取。 + + 路径契约为「drop 根内文件名」:Worker(Windows RPA 主机)与后端(Linux 容器)分属 + 两机,各自看到的根路径不同(C:\\HxopsRpaTest\\... vs /mnt/hxops-rpa-*-data/...), + Worker 只上报文件名,后端用自身挂载的 drop 根拼接定位;携带本机绝对路径的历史 + 载荷亦按末段文件名解析(2026-10-08 修复跨机成功回调全程 400 的事故)。 + """ configured_root = str(current_app.config.get('FREIGHT_RPA_SHARED_RECEIPT_DROP_DIR') or '').strip() if not configured_root: raise ValidationError('receipt callback file root is unavailable', code='SHIPPING.RECEIPT_CALLBACK_FILE_INVALID') root = Path(configured_root).resolve() - path = Path(file_path).resolve() + # 容忍历史载荷携带 Windows 绝对路径:先归一分隔符再取末段文件名(防遍历,单一分段)。 + name = Path(str(file_path).replace('\\', '/')).name + path = (root / name).resolve() try: path.relative_to(root) except ValueError as exc: diff --git a/backend/tests/test_freight_receipt_rpa_callback.py b/backend/tests/test_freight_receipt_rpa_callback.py index b55f0b56..b237cdbe 100644 --- a/backend/tests/test_freight_receipt_rpa_callback.py +++ b/backend/tests/test_freight_receipt_rpa_callback.py @@ -288,3 +288,27 @@ def test_receipt_progress_routes_are_registered(app): resp = client.post(path, json=payload, headers=headers) assert resp.status_code == 200, (path, resp.status_code, resp.get_json()) assert resp.get_json()['data']['updated'] is False + + +def test_read_receipt_callback_file_resolves_drop_root_filename(app, tmp_path): + """跨机路径契约:Worker 只上报文件名(或历史 Windows 绝对路径),后端按自身 drop 根定位。""" + from app.core.errors import ValidationError + from app.services.shipping_notice.receipt_report_storage import _read_receipt_callback_file + + with app.app_context(): + drop_root = tmp_path / 'receipt-drop' + drop_root.mkdir() + app.config['FREIGHT_RPA_SHARED_RECEIPT_DROP_DIR'] = str(drop_root) + target = drop_root / 'receipt_RUN_X_SITE_NA_2026.xlsx' + target.write_bytes(b'receipt-bytes') + + # [断言意图] 裸文件名(新 Worker 契约)与 Windows 本机绝对路径(历史载荷)都 + # 必须解析到后端 drop 根内同一文件;根外文件依旧拒绝(任意本机文件读取护栏不变)。 + assert _read_receipt_callback_file('receipt_RUN_X_SITE_NA_2026.xlsx') == b'receipt-bytes' + assert _read_receipt_callback_file( + r'C:\HxopsRpaTest\data\freight_rpa\receipt\drop\receipt_RUN_X_SITE_NA_2026.xlsx' + ) == b'receipt-bytes' + outside = tmp_path / 'outside.xlsx' + outside.write_bytes(b'x') + with pytest.raises(ValidationError, match='receipt callback file'): + _read_receipt_callback_file(str(outside)) diff --git a/scripts/freight_rpa/receipt/receipt_rpa_worker.py b/scripts/freight_rpa/receipt/receipt_rpa_worker.py index efc97be9..d10f199e 100644 --- a/scripts/freight_rpa/receipt/receipt_rpa_worker.py +++ b/scripts/freight_rpa/receipt/receipt_rpa_worker.py @@ -584,12 +584,16 @@ def _write_mock_receipt_excel(path: Path) -> None: def _post_unit_ready(unit: dict[str, Any], output: Path) -> None: - """落盘成功后上报 receipt-batch-ready(归档);调用方须放在导出重试之外。""" + """落盘成功后上报 receipt-batch-ready(归档);调用方须放在导出重试之外。 + + file_path 契约为「drop 根内文件名」:Worker(Windows)与后端(Linux 挂载根)分属 + 两机、根路径不同,后端按自身挂载根拼接定位(跨机部署下发本机绝对路径必 400)。 + """ _post_callback({ 'schema_version': 'shipping_receipt_rpa.v1', 'callback_id': unit['callback_id'], 'status': 'success', - 'file_path': str(output), + 'file_path': output.name, 'original_filename': output.name, 'sha256': hashlib.sha256(output.read_bytes()).hexdigest(), })