fix: redact settings paths in operations
这个提交存在于:
父节点
a4d189e139
当前提交
ed0ed578e8
共有 4 个文件被更改,包括 32 次插入 和 5 次删除
|
|
@ -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 exc.to_response(request_id=request_id)
|
||||||
return error_response(
|
return error_response(
|
||||||
error_code=ErrorCode.UNKNOWN_ERROR,
|
error_code=ErrorCode.UNKNOWN_ERROR,
|
||||||
message="服务器内部错误",
|
message="\u670d\u52a1\u5668\u5185\u90e8\u9519\u8bef",
|
||||||
details={"exception": exc.__class__.__name__},
|
details={"exception": exc.__class__.__name__},
|
||||||
request_id=request_id,
|
request_id=request_id,
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -95,7 +95,7 @@ class SettingsService:
|
||||||
def get_current(self) -> dict[str, Any]:
|
def get_current(self) -> dict[str, Any]:
|
||||||
raw = self._load_current_mapping()
|
raw = self._load_current_mapping()
|
||||||
return {
|
return {
|
||||||
"config_path": str(self.config_service.config_path),
|
"config_path": "***PATH***",
|
||||||
"config": _mask_sensitive(raw),
|
"config": _mask_sensitive(raw),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -152,6 +152,15 @@ class SettingsService:
|
||||||
"sensitive": True,
|
"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:
|
else:
|
||||||
changes.append(
|
changes.append(
|
||||||
{
|
{
|
||||||
|
|
@ -180,7 +189,7 @@ class SettingsService:
|
||||||
ctx=ctx,
|
ctx=ctx,
|
||||||
action="save_settings",
|
action="save_settings",
|
||||||
target_type="config",
|
target_type="config",
|
||||||
target_id=str(self.config_service.config_path),
|
target_id="***PATH***",
|
||||||
before_snapshot=_mask_sensitive(before),
|
before_snapshot=_mask_sensitive(before),
|
||||||
after_snapshot=_mask_sensitive(after),
|
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()
|
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:
|
def _resolve_path(path: str | Path) -> Path:
|
||||||
resolved = Path(path)
|
resolved = Path(path)
|
||||||
if not resolved.is_absolute():
|
if not resolved.is_absolute():
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ def require_authenticated(ctx: RequestContext) -> None:
|
||||||
if not ctx.is_authenticated:
|
if not ctx.is_authenticated:
|
||||||
raise ApiError(
|
raise ApiError(
|
||||||
ErrorCode.UNAUTHORIZED,
|
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):
|
if not ctx.has_permission(permission):
|
||||||
raise ApiError(
|
raise ApiError(
|
||||||
ErrorCode.FORBIDDEN,
|
ErrorCode.FORBIDDEN,
|
||||||
"当前用户没有执行该操作的权限",
|
"\u5f53\u524d\u7528\u6237\u6ca1\u6709\u6267\u884c\u8be5\u64cd\u4f5c\u7684\u6743\u9650",
|
||||||
{"permission": permission},
|
{"permission": permission},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -426,6 +426,8 @@ security:
|
||||||
|
|
||||||
current = get_current_settings(self.ctx, self.settings_service)
|
current = get_current_settings(self.ctx, self.settings_service)
|
||||||
self.assertTrue(current["ok"])
|
self.assertTrue(current["ok"])
|
||||||
|
self.assertEqual(current["data"]["config_path"], "***PATH***")
|
||||||
|
self.assertNotIn(str(self.root), str(current["data"]))
|
||||||
config = current["data"]["config"]
|
config = current["data"]["config"]
|
||||||
self.assertEqual(config["database"]["postgresql"]["dsn"], "***MASKED***")
|
self.assertEqual(config["database"]["postgresql"]["dsn"], "***MASKED***")
|
||||||
self.assertEqual(config["database"]["postgresql"]["password"], "***MASKED***")
|
self.assertEqual(config["database"]["postgresql"]["password"], "***MASKED***")
|
||||||
|
|
@ -526,11 +528,23 @@ database:
|
||||||
response = get_current_settings(self.ctx, self.settings_service)
|
response = get_current_settings(self.ctx, self.settings_service)
|
||||||
|
|
||||||
self.assertTrue(response["ok"])
|
self.assertTrue(response["ok"])
|
||||||
|
self.assertEqual(response["data"]["config_path"], "***PATH***")
|
||||||
storage = response["data"]["config"]["storage"]
|
storage = response["data"]["config"]["storage"]
|
||||||
self.assertEqual(storage["images_path"], "***PATH***")
|
self.assertEqual(storage["images_path"], "***PATH***")
|
||||||
self.assertEqual(storage["thumbnails_path"], "***PATH***")
|
self.assertEqual(storage["thumbnails_path"], "***PATH***")
|
||||||
self.assertNotIn(str(self.root), str(response["data"]))
|
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):
|
def test_storage_status_and_media_signing_do_not_expose_real_paths(self):
|
||||||
response = get_storage_status(self.ctx, self.media_service)
|
response = get_storage_status(self.ctx, self.media_service)
|
||||||
|
|
||||||
|
|
|
||||||
正在加载…
添加表格
在新工单中引用