fix(shipping-notice): 收料回调路径契约改为 drop 根内文件名

- 修复跨机成功回调全程 400:Worker(Windows)与后端(Linux 挂载根)分属两机根路径不同,后端无法解析 Worker 本机绝对路径
- Worker 只上报文件名(output.name),后端按自身挂载的 drop 根拼接定位;历史载荷携带绝对路径亦按末段文件名兼容解析
- 同步收料 worker 路径契约注释与跨机路径回归测试
This commit is contained in:
hugo committed 2026-10-08 13:14:47 +08:00
1 parent 91a4bd538d
commit 4e97553f42
3 files changed
+40 -4

No files matched your search

@@ -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:
@@ -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))
@@ -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(),
})