S99 上传payload加固
这个提交存在于:
父节点
297f3beaef
当前提交
36a3c1708c
共有 2 个文件被更改,包括 214 次插入 和 6 次删除
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import PurePosixPath
|
||||
from typing import Any
|
||||
from uuid import uuid4
|
||||
|
||||
|
|
@ -13,6 +14,14 @@ from database.repositories.material_repository import MaterialRepository
|
|||
|
||||
|
||||
MATERIAL_STATUSES = {"pending_upload", "pending_recognition", "pending_confirm", "confirmed", "archived"}
|
||||
MAX_UPLOAD_BYTES = 20 * 1024 * 1024
|
||||
UPLOAD_SIZE_FIELDS = ("size_bytes", "content_length", "file_size", "size")
|
||||
UPLOAD_ALLOWED_EXTENSIONS = {".jpg", ".jpeg", ".png", ".webp"}
|
||||
UPLOAD_ALLOWED_CONTENT_TYPES = {
|
||||
"image/jpeg": {".jpg", ".jpeg"},
|
||||
"image/png": {".png"},
|
||||
"image/webp": {".webp"},
|
||||
}
|
||||
|
||||
|
||||
class MaterialService:
|
||||
|
|
@ -173,21 +182,31 @@ class MaterialService:
|
|||
return self.update_material(material_id, {"recognition_status": "archived"}, ctx)
|
||||
|
||||
def upload_material(self, payload: dict[str, Any], ctx: RequestContext) -> Material:
|
||||
image_uri = payload.get("image_uri")
|
||||
image_uri = str(payload.get("image_uri") or "").strip()
|
||||
if not image_uri:
|
||||
raise ApiError(ErrorCode.VALIDATION_FAILED, "image_uri is required", {"field": "image_uri"})
|
||||
display_name = str(payload.get("display_name") or payload.get("filename") or "Uploaded material").strip()
|
||||
filename = _normalize_upload_filename(payload.get("filename"))
|
||||
content_type = _normalize_upload_content_type(payload.get("content_type"))
|
||||
declared_size = _normalize_upload_size(payload)
|
||||
upload_metadata = _normalize_upload_metadata(payload.get("metadata"))
|
||||
extension = _validate_upload_file_type(filename=filename, image_uri=image_uri, content_type=content_type)
|
||||
display_name = str(payload.get("display_name") or _display_name_from_filename(filename) or "Uploaded material").strip()
|
||||
thumbnail_uri = str(payload.get("thumbnail_uri") or image_uri).strip()
|
||||
return self.create_material(
|
||||
{
|
||||
"category_id": payload.get("category_id"),
|
||||
"display_name": display_name,
|
||||
"image_uri": image_uri,
|
||||
"thumbnail_uri": payload.get("thumbnail_uri") or image_uri,
|
||||
"thumbnail_uri": thumbnail_uri,
|
||||
"recognition_status": "pending_recognition",
|
||||
"metadata": {
|
||||
**upload_metadata,
|
||||
"upload": {
|
||||
"filename": payload.get("filename"),
|
||||
"content_type": payload.get("content_type"),
|
||||
"filename": filename,
|
||||
"content_type": content_type,
|
||||
"extension": extension,
|
||||
"size_bytes": declared_size,
|
||||
"max_size_bytes": MAX_UPLOAD_BYTES,
|
||||
"mock": True,
|
||||
}
|
||||
},
|
||||
|
|
@ -289,3 +308,101 @@ def _mock_tags(material: Material) -> list[str]:
|
|||
if keyword in text:
|
||||
tags.append(keyword)
|
||||
return tags
|
||||
|
||||
|
||||
def _normalize_upload_filename(value: Any) -> str | None:
|
||||
if value is None:
|
||||
return None
|
||||
filename = str(value).strip()
|
||||
if not filename:
|
||||
return None
|
||||
if len(filename) > 255 or filename in {".", ".."} or any(marker in filename for marker in ("/", "\\", "\x00", ":")):
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"filename must be a safe basename",
|
||||
{"field": "filename"},
|
||||
)
|
||||
return filename
|
||||
|
||||
|
||||
def _normalize_upload_content_type(value: Any) -> str | None:
|
||||
if value is None:
|
||||
return None
|
||||
content_type = str(value).split(";", 1)[0].strip().lower()
|
||||
if not content_type:
|
||||
return None
|
||||
if content_type not in UPLOAD_ALLOWED_CONTENT_TYPES:
|
||||
raise ApiError(
|
||||
ErrorCode.UNSUPPORTED_FILE_TYPE,
|
||||
"unsupported upload content type",
|
||||
{
|
||||
"field": "content_type",
|
||||
"content_type": content_type,
|
||||
"allowed": sorted(UPLOAD_ALLOWED_CONTENT_TYPES),
|
||||
},
|
||||
)
|
||||
return content_type
|
||||
|
||||
|
||||
def _normalize_upload_size(payload: dict[str, Any]) -> int | None:
|
||||
for field in UPLOAD_SIZE_FIELDS:
|
||||
if field not in payload or payload[field] in (None, ""):
|
||||
continue
|
||||
try:
|
||||
size = int(payload[field])
|
||||
except (TypeError, ValueError):
|
||||
raise ApiError(ErrorCode.VALIDATION_FAILED, "upload size must be an integer", {"field": field}) from None
|
||||
if size < 0:
|
||||
raise ApiError(ErrorCode.VALIDATION_FAILED, "upload size must not be negative", {"field": field})
|
||||
if size > MAX_UPLOAD_BYTES:
|
||||
raise ApiError(
|
||||
ErrorCode.UPLOAD_TOO_LARGE,
|
||||
"upload exceeds maximum allowed size",
|
||||
{"field": field, "size_bytes": size, "max_size_bytes": MAX_UPLOAD_BYTES},
|
||||
)
|
||||
return size
|
||||
return None
|
||||
|
||||
|
||||
def _normalize_upload_metadata(value: Any) -> dict[str, Any]:
|
||||
if value in (None, ""):
|
||||
return {}
|
||||
if not isinstance(value, dict):
|
||||
raise ApiError(ErrorCode.VALIDATION_FAILED, "metadata must be an object", {"field": "metadata"})
|
||||
return dict(value)
|
||||
|
||||
|
||||
def _validate_upload_file_type(*, filename: str | None, image_uri: str, content_type: str | None) -> str:
|
||||
extension = _suffix(filename) or _suffix(image_uri)
|
||||
if not extension:
|
||||
raise ApiError(
|
||||
ErrorCode.UNSUPPORTED_FILE_TYPE,
|
||||
"upload filename or image_uri must include a supported image extension",
|
||||
{"field": "filename", "allowed": sorted(UPLOAD_ALLOWED_EXTENSIONS)},
|
||||
)
|
||||
if extension not in UPLOAD_ALLOWED_EXTENSIONS:
|
||||
raise ApiError(
|
||||
ErrorCode.UNSUPPORTED_FILE_TYPE,
|
||||
"unsupported upload file extension",
|
||||
{"field": "filename", "extension": extension, "allowed": sorted(UPLOAD_ALLOWED_EXTENSIONS)},
|
||||
)
|
||||
if content_type and extension not in UPLOAD_ALLOWED_CONTENT_TYPES[content_type]:
|
||||
raise ApiError(
|
||||
ErrorCode.UNSUPPORTED_FILE_TYPE,
|
||||
"upload extension does not match content type",
|
||||
{"field": "content_type", "extension": extension, "content_type": content_type},
|
||||
)
|
||||
return extension
|
||||
|
||||
|
||||
def _suffix(value: str | None) -> str | None:
|
||||
if not value:
|
||||
return None
|
||||
suffix = PurePosixPath(str(value).split("?", 1)[0]).suffix.lower()
|
||||
return suffix or None
|
||||
|
||||
|
||||
def _display_name_from_filename(filename: str | None) -> str | None:
|
||||
if not filename:
|
||||
return None
|
||||
return PurePosixPath(filename).stem.replace("_", " ").strip() or None
|
||||
|
|
|
|||
|
|
@ -17,8 +17,9 @@ from backend.routes.material_routes import (
|
|||
update_material,
|
||||
upload_material,
|
||||
)
|
||||
from backend.dto.common import ApiError, ErrorCode
|
||||
from backend.services.media_service import MediaService
|
||||
from backend.services.material_service import MaterialService
|
||||
from backend.services.material_service import MAX_UPLOAD_BYTES, MaterialService
|
||||
from core.audit_service import AuditService
|
||||
from core.config_service import ConfigService
|
||||
from core.identity import RequestContext
|
||||
|
|
@ -132,12 +133,21 @@ storage:
|
|||
{
|
||||
"category_id": category.category_id,
|
||||
"filename": "white_blouse.png",
|
||||
"content_type": "image/png; charset=binary",
|
||||
"size_bytes": "2048",
|
||||
"image_uri": "storage://materials/white_blouse.png",
|
||||
"metadata": {"source": "unit_test"},
|
||||
},
|
||||
self.ctx,
|
||||
)
|
||||
self.assertEqual(uploaded.recognition_status, "pending_recognition")
|
||||
self.assertEqual(uploaded.thumbnail_uri, "storage://materials/white_blouse.png")
|
||||
self.assertEqual(uploaded.display_name, "white blouse")
|
||||
self.assertEqual(uploaded.metadata["source"], "unit_test")
|
||||
self.assertEqual(uploaded.metadata["upload"]["filename"], "white_blouse.png")
|
||||
self.assertEqual(uploaded.metadata["upload"]["content_type"], "image/png")
|
||||
self.assertEqual(uploaded.metadata["upload"]["extension"], ".png")
|
||||
self.assertEqual(uploaded.metadata["upload"]["size_bytes"], 2048)
|
||||
|
||||
recognized = self.material_service.recognize_material(uploaded.material_id, self.ctx)
|
||||
self.assertEqual(recognized.recognition_status, "pending_confirm")
|
||||
|
|
@ -160,6 +170,73 @@ storage:
|
|||
self.assertEqual(re_recognized.recognition_status, "pending_confirm")
|
||||
self.assertIn("white", re_recognized.tags)
|
||||
|
||||
def test_upload_rejects_unsafe_filename(self):
|
||||
category = self.material_service.create_category({"name": "Upper"}, self.ctx)
|
||||
|
||||
with self.assertRaises(ApiError) as caught:
|
||||
self.material_service.upload_material(
|
||||
{
|
||||
"category_id": category.category_id,
|
||||
"filename": "../white_blouse.png",
|
||||
"image_uri": "storage://materials/white_blouse.png",
|
||||
},
|
||||
self.ctx,
|
||||
)
|
||||
|
||||
self.assertEqual(caught.exception.error_code, ErrorCode.VALIDATION_FAILED)
|
||||
self.assertEqual(caught.exception.details["field"], "filename")
|
||||
|
||||
def test_upload_rejects_unsupported_file_type(self):
|
||||
category = self.material_service.create_category({"name": "Upper"}, self.ctx)
|
||||
|
||||
with self.assertRaises(ApiError) as caught:
|
||||
self.material_service.upload_material(
|
||||
{
|
||||
"category_id": category.category_id,
|
||||
"filename": "white_blouse.gif",
|
||||
"content_type": "image/gif",
|
||||
"image_uri": "storage://materials/white_blouse.gif",
|
||||
},
|
||||
self.ctx,
|
||||
)
|
||||
|
||||
self.assertEqual(caught.exception.error_code, ErrorCode.UNSUPPORTED_FILE_TYPE)
|
||||
|
||||
def test_upload_rejects_mismatched_content_type(self):
|
||||
category = self.material_service.create_category({"name": "Upper"}, self.ctx)
|
||||
|
||||
with self.assertRaises(ApiError) as caught:
|
||||
self.material_service.upload_material(
|
||||
{
|
||||
"category_id": category.category_id,
|
||||
"filename": "white_blouse.jpg",
|
||||
"content_type": "image/png",
|
||||
"image_uri": "storage://materials/white_blouse.jpg",
|
||||
},
|
||||
self.ctx,
|
||||
)
|
||||
|
||||
self.assertEqual(caught.exception.error_code, ErrorCode.UNSUPPORTED_FILE_TYPE)
|
||||
self.assertEqual(caught.exception.details["field"], "content_type")
|
||||
|
||||
def test_upload_rejects_oversized_payload(self):
|
||||
category = self.material_service.create_category({"name": "Upper"}, self.ctx)
|
||||
|
||||
with self.assertRaises(ApiError) as caught:
|
||||
self.material_service.upload_material(
|
||||
{
|
||||
"category_id": category.category_id,
|
||||
"filename": "white_blouse.png",
|
||||
"content_type": "image/png",
|
||||
"size_bytes": MAX_UPLOAD_BYTES + 1,
|
||||
"image_uri": "storage://materials/white_blouse.png",
|
||||
},
|
||||
self.ctx,
|
||||
)
|
||||
|
||||
self.assertEqual(caught.exception.error_code, ErrorCode.UPLOAD_TOO_LARGE)
|
||||
self.assertEqual(caught.exception.details["max_size_bytes"], MAX_UPLOAD_BYTES)
|
||||
|
||||
def test_material_routes_wrap_responses_and_require_permission(self):
|
||||
category = create_material_category(self.ctx, {"name": "Accessory"}, self.material_service)
|
||||
self.assertTrue(category["ok"])
|
||||
|
|
@ -230,6 +307,20 @@ storage:
|
|||
self.assertTrue(uploaded["data"]["thumbnail_url"].startswith("/api/v1/plugins/extensions/"))
|
||||
self.assertNotIn(str(self.root), str(uploaded["data"]))
|
||||
|
||||
rejected = upload_material(
|
||||
self.ctx,
|
||||
{
|
||||
"category_id": category["data"]["category_id"],
|
||||
"filename": "blue_dress.exe",
|
||||
"content_type": "application/octet-stream",
|
||||
"image_uri": "storage://materials/blue_dress.exe",
|
||||
},
|
||||
self.material_service,
|
||||
self.media_service,
|
||||
)
|
||||
self.assertFalse(rejected["ok"])
|
||||
self.assertEqual(rejected["error_code"], "UNSUPPORTED_FILE_TYPE")
|
||||
|
||||
material_id = uploaded["data"]["material_id"]
|
||||
recognized = recognize_material(self.ctx, material_id, self.material_service, self.media_service)
|
||||
self.assertTrue(recognized["ok"])
|
||||
|
|
|
|||
正在加载…
添加表格
在新工单中引用