From ed0ed578e82935dee93e1992761e054dfb4c8913 Mon Sep 17 00:00:00 2001 From: YokiiroBW Date: Sun, 5 Jul 2026 13:13:00 +0800 Subject: [PATCH] fix: redact settings paths in operations --- backend/dto/common.py | 2 +- backend/services/settings_service.py | 17 +++++++++++++++-- core/security.py | 4 ++-- tests/test_s2_routes.py | 14 ++++++++++++++ 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/backend/dto/common.py b/backend/dto/common.py index 0224b8e..d602f4a 100644 --- a/backend/dto/common.py +++ b/backend/dto/common.py @@ -76,7 +76,7 @@ def response_from_exception(exc: Exception, request_id: str | None = None) -> di return exc.to_response(request_id=request_id) return error_response( error_code=ErrorCode.UNKNOWN_ERROR, - message="服务器内部错误", + message="\u670d\u52a1\u5668\u5185\u90e8\u9519\u8bef", details={"exception": exc.__class__.__name__}, request_id=request_id, ) diff --git a/backend/services/settings_service.py b/backend/services/settings_service.py index acd8eb7..26fa057 100644 --- a/backend/services/settings_service.py +++ b/backend/services/settings_service.py @@ -95,7 +95,7 @@ class SettingsService: def get_current(self) -> dict[str, Any]: raw = self._load_current_mapping() return { - "config_path": str(self.config_service.config_path), + "config_path": "***PATH***", "config": _mask_sensitive(raw), } @@ -152,6 +152,15 @@ class SettingsService: "sensitive": True, } ) + elif _is_path_key(key): + changes.append( + { + "path": key, + "before": _mask_path_value(before_value), + "after": _mask_path_value(after_value), + "sensitive": False, + } + ) else: changes.append( { @@ -180,7 +189,7 @@ class SettingsService: ctx=ctx, action="save_settings", target_type="config", - target_id=str(self.config_service.config_path), + target_id="***PATH***", before_snapshot=_mask_sensitive(before), after_snapshot=_mask_sensitive(after), ) @@ -274,6 +283,10 @@ def _is_absolute_path_value(value: Any) -> bool: return isinstance(value, str) and Path(value).is_absolute() +def _mask_path_value(value: Any) -> Any: + return "***PATH***" if _is_absolute_path_value(value) else value + + def _resolve_path(path: str | Path) -> Path: resolved = Path(path) if not resolved.is_absolute(): diff --git a/core/security.py b/core/security.py index 5a536a5..3e67984 100644 --- a/core/security.py +++ b/core/security.py @@ -10,7 +10,7 @@ def require_authenticated(ctx: RequestContext) -> None: if not ctx.is_authenticated: raise ApiError( ErrorCode.UNAUTHORIZED, - "ImageStudio 需要 AstrBot WebUI 登录态", + "ImageStudio \u9700\u8981 AstrBot WebUI \u767b\u5f55\u6001", ) @@ -19,7 +19,7 @@ def require_permission(ctx: RequestContext, permission: str) -> None: if not ctx.has_permission(permission): raise ApiError( ErrorCode.FORBIDDEN, - "当前用户没有执行该操作的权限", + "\u5f53\u524d\u7528\u6237\u6ca1\u6709\u6267\u884c\u8be5\u64cd\u4f5c\u7684\u6743\u9650", {"permission": permission}, ) diff --git a/tests/test_s2_routes.py b/tests/test_s2_routes.py index bbefd93..52d0566 100644 --- a/tests/test_s2_routes.py +++ b/tests/test_s2_routes.py @@ -426,6 +426,8 @@ security: current = get_current_settings(self.ctx, self.settings_service) self.assertTrue(current["ok"]) + self.assertEqual(current["data"]["config_path"], "***PATH***") + self.assertNotIn(str(self.root), str(current["data"])) config = current["data"]["config"] self.assertEqual(config["database"]["postgresql"]["dsn"], "***MASKED***") self.assertEqual(config["database"]["postgresql"]["password"], "***MASKED***") @@ -526,11 +528,23 @@ database: response = get_current_settings(self.ctx, self.settings_service) self.assertTrue(response["ok"]) + self.assertEqual(response["data"]["config_path"], "***PATH***") storage = response["data"]["config"]["storage"] self.assertEqual(storage["images_path"], "***PATH***") self.assertEqual(storage["thumbnails_path"], "***PATH***") self.assertNotIn(str(self.root), str(response["data"])) + def test_settings_diff_masks_absolute_paths(self): + diff = diff_settings( + self.ctx, + {"config": {"storage": {"images_path": str(self.root / "new-images")}}}, + self.settings_service, + ) + + self.assertTrue(diff["ok"]) + self.assertTrue(diff["data"]["has_changes"]) + self.assertNotIn(str(self.root), str(diff["data"])) + def test_storage_status_and_media_signing_do_not_expose_real_paths(self): response = get_storage_status(self.ctx, self.media_service)