fix(admin): 反馈图片代理接口按扩展名固定 Content-Type 并禁用嗅探
This commit is contained in:
1 parent
37b743d5d3
commit
e8ad4cc743
3 files changed
+22
-3
No files matched your search
@@ -144,5 +144,7 @@ def general_feedback_image_raw(feedback_id: int, image_id: int):
|
||||
"""代理输出意见反馈截图字节流(inline 展示用)。"""
|
||||
payload = report_service.get_general_feedback_image_payload(feedback_id, image_id)
|
||||
resp = Response(payload['data'], mimetype=payload['mime_type'])
|
||||
# [SECURITY] Content-Type 已按扩展名白名单收敛,仍禁浏览器嗅探,双保险防内联渲染为 HTML
|
||||
resp.headers['X-Content-Type-Options'] = 'nosniff'
|
||||
resp.headers['Content-Disposition'] = f"inline; filename*=UTF-8''{quote(payload['filename'])}"
|
||||
return resp
|
||||
@@ -37,6 +37,16 @@ USAGE_LOW_AGENT_MAX_CHATS = 2
|
||||
# 运营提醒:低使用应用最多返回条数
|
||||
USAGE_LOW_AGENT_LIST_LIMIT = 5
|
||||
|
||||
# 反馈图片响应 Content-Type 白名单:按资产扩展名固定响应类型。
|
||||
# [SECURITY] 存储 mime_type 来自上传时客户端 multipart 头,不可信;代理内联输出时
|
||||
# 若原样透传可被伪造为 text/html 等,构成存储型 XSS 面,故一律按扩展名收敛。
|
||||
FEEDBACK_IMAGE_CONTENT_TYPES = {
|
||||
'png': 'image/png',
|
||||
'jpg': 'image/jpeg',
|
||||
'jpeg': 'image/jpeg',
|
||||
'webp': 'image/webp',
|
||||
}
|
||||
|
||||
# 使用分析时间范围:key → 回溯天数(与前端下拉选项一致)
|
||||
USAGE_RANGE_DAYS = {
|
||||
'7d': 7,
|
||||
@@ -631,11 +641,14 @@ def get_general_feedback_image_payload(feedback_id: int, image_id: int) -> dict:
|
||||
用途:管理端报表页内联预览反馈截图;由 api/admin/reports.py 的 raw 路由调用。
|
||||
逻辑:校验 image 归属该 feedback → 校验资产未删除 → read_asset_bytes 读字节
|
||||
(内部再拦过期/已删除的 FILE_ASSET.*),错配一律按不存在处理避免探测他人资产。
|
||||
响应 Content-Type 按扩展名白名单(FEEDBACK_IMAGE_CONTENT_TYPES)固定,
|
||||
不透传存储 mime(其来自客户端上传头,不可信)。
|
||||
Args:
|
||||
feedback_id: 反馈 ID;image 必须归属该反馈,错配按不存在处理。
|
||||
image_id: 反馈附件图片 ID。
|
||||
Returns:
|
||||
{'data': bytes, 'mime_type': str, 'filename': str}。
|
||||
{'data': bytes, 'mime_type': str, 'filename': str};mime_type 由扩展名白名单决定,
|
||||
未知扩展名(历史/边界行)降级 application/octet-stream。
|
||||
Raises:
|
||||
NotFoundError: code=FEEDBACK.IMAGE_NOT_FOUND——image 不存在、不属于该反馈或资产已删除。
|
||||
"""
|
||||
@@ -650,9 +663,11 @@ def get_general_feedback_image_payload(feedback_id: int, image_id: int) -> dict:
|
||||
if asset is None or asset.deleted_at is not None:
|
||||
raise NotFoundError(code='FEEDBACK.IMAGE_NOT_FOUND', message='反馈图片不存在')
|
||||
data = read_asset_bytes(asset)
|
||||
# [SECURITY] 提交时仅校验扩展名白名单,存储 mime 仍是客户端自报值;响应类型只能信扩展名
|
||||
mime_type = FEEDBACK_IMAGE_CONTENT_TYPES.get(str(asset.extension or '').lower(), 'application/octet-stream')
|
||||
return {
|
||||
'data': data,
|
||||
'mime_type': asset.mime_type or 'application/octet-stream',
|
||||
'mime_type': mime_type,
|
||||
'filename': asset.original_filename,
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user