mirror of
https://github.com/vastsa/FileCodeBox.git
synced 2026-11-25 00:27:43 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c11059b851 | ||
|
|
ae7702603a | ||
|
|
a41693f90d | ||
|
|
59ed733f3a | ||
|
|
cf6d364c2b | ||
|
|
f2277e1649 |
No files matched your search
@@ -1,3 +1,3 @@
|
|||||||
{
|
{
|
||||||
".": "2.5.0"
|
".": "2.5.2"
|
||||||
}
|
}
|
||||||
+43
-5
@@ -8,6 +8,43 @@ from core.settings import settings
|
|||||||
from core.utils import get_now
|
from core.utils import get_now
|
||||||
|
|
||||||
|
|
||||||
|
def _detect_sql_dialect() -> str:
|
||||||
|
"""识别当前默认连接的 SQL 方言。
|
||||||
|
|
||||||
|
优先使用 Tortoise capabilities.dialect;
|
||||||
|
回退到 db_config 中的 engine 路径字符串。
|
||||||
|
"""
|
||||||
|
conn = connections.get("default")
|
||||||
|
dialect = getattr(getattr(conn, "capabilities", None), "dialect", "") or ""
|
||||||
|
if dialect:
|
||||||
|
return dialect.lower()
|
||||||
|
|
||||||
|
try:
|
||||||
|
engine = str(connections.db_config.get("default", {}).get("engine", "")).lower()
|
||||||
|
except Exception:
|
||||||
|
engine = ""
|
||||||
|
if "postgres" in engine or "asyncpg" in engine or "psycopg" in engine:
|
||||||
|
return "postgres"
|
||||||
|
if "mysql" in engine:
|
||||||
|
return "mysql"
|
||||||
|
if "sqlite" in engine:
|
||||||
|
return "sqlite"
|
||||||
|
return "sqlite"
|
||||||
|
|
||||||
|
|
||||||
|
def _sql_placeholders(count: int) -> list[str]:
|
||||||
|
"""根据数据库方言生成参数占位符(多数据库兼容)。
|
||||||
|
|
||||||
|
SQLite 用 ?,PostgreSQL 用 $1/$2/...,MySQL 用 %s。
|
||||||
|
"""
|
||||||
|
dialect = _detect_sql_dialect()
|
||||||
|
if dialect in {"postgres", "postgresql"}:
|
||||||
|
return [f"${i}" for i in range(1, count + 1)]
|
||||||
|
if dialect == "mysql":
|
||||||
|
return ["%s"] * count
|
||||||
|
return ["?"] * count
|
||||||
|
|
||||||
|
|
||||||
def get_storage_limit() -> int:
|
def get_storage_limit() -> int:
|
||||||
try:
|
try:
|
||||||
return max(0, int(getattr(settings, "storageLimit", 0)))
|
return max(0, int(getattr(settings, "storageLimit", 0)))
|
||||||
@@ -49,15 +86,16 @@ async def reserve_storage(token: str, size: int, ttl_seconds: int) -> None:
|
|||||||
raise HTTPException(status_code=409, detail="上传容量预留信息不一致")
|
raise HTTPException(status_code=409, detail="上传容量预留信息不一致")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
ph = _sql_placeholders(6)
|
||||||
affected, _ = await conn.execute_query(
|
affected, _ = await conn.execute_query(
|
||||||
"""
|
f"""
|
||||||
INSERT INTO storagereservation (token, size, expires_at)
|
INSERT INTO storagereservation (token, size, expires_at)
|
||||||
SELECT ?, ?, ?
|
SELECT {ph[0]}, {ph[1]}, {ph[2]}
|
||||||
WHERE (
|
WHERE (
|
||||||
COALESCE((SELECT SUM(size) FROM filecodes), 0)
|
COALESCE((SELECT SUM(size) FROM filecodes), 0)
|
||||||
+ COALESCE((SELECT SUM(size) FROM storagereservation WHERE expires_at > ?), 0)
|
+ COALESCE((SELECT SUM(size) FROM storagereservation WHERE expires_at > {ph[3]}), 0)
|
||||||
+ ?
|
+ {ph[4]}
|
||||||
) <= ?
|
) <= {ph[5]}
|
||||||
""",
|
""",
|
||||||
[token, requested_size, expires_at, now, requested_size, limit],
|
[token, requested_size, expires_at, now, requested_size, limit],
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -19,6 +19,13 @@ from core.utils import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def validate_expire_style(expire_style: str) -> str:
|
||||||
|
"""校验过期方式是否在管理员配置的白名单内。"""
|
||||||
|
if expire_style not in settings.expireStyle:
|
||||||
|
raise HTTPException(status_code=400, detail="过期时间类型错误")
|
||||||
|
return expire_style
|
||||||
|
|
||||||
|
|
||||||
async def get_file_path_name(file: UploadFile) -> Tuple[str, str, str, str, str]:
|
async def get_file_path_name(file: UploadFile) -> Tuple[str, str, str, str, str]:
|
||||||
today = await get_now()
|
today = await get_now()
|
||||||
storage_path = settings.storage_path.strip("/")
|
storage_path = settings.storage_path.strip("/")
|
||||||
|
|||||||
+24
-6
@@ -4,13 +4,14 @@ import hashlib
|
|||||||
import os
|
import os
|
||||||
import uuid
|
import uuid
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from urllib.parse import unquote
|
from urllib.parse import quote, unquote
|
||||||
|
|
||||||
from typing import Optional, Tuple, Union
|
from typing import Optional, Tuple, Union
|
||||||
|
|
||||||
from fastapi import APIRouter, Form, Request, UploadFile, File, Depends, HTTPException
|
from fastapi import APIRouter, Form, Request, UploadFile, File, Depends, HTTPException
|
||||||
from pydantic import BaseModel, ValidationError
|
from pydantic import BaseModel, ValidationError
|
||||||
from starlette import status
|
from starlette import status
|
||||||
|
from starlette.responses import Response
|
||||||
from tortoise.expressions import Case, F, Q, When
|
from tortoise.expressions import Case, F, Q, When
|
||||||
|
|
||||||
from apps.admin.dependencies import share_required_login
|
from apps.admin.dependencies import share_required_login
|
||||||
@@ -27,6 +28,7 @@ from apps.base.utils import (
|
|||||||
get_file_path_name,
|
get_file_path_name,
|
||||||
ip_limit,
|
ip_limit,
|
||||||
get_chunk_file_path_name,
|
get_chunk_file_path_name,
|
||||||
|
validate_expire_style,
|
||||||
)
|
)
|
||||||
from core.response import APIResponse
|
from core.response import APIResponse
|
||||||
from core.settings import settings
|
from core.settings import settings
|
||||||
@@ -151,6 +153,7 @@ async def share_text(
|
|||||||
expire_style: str = Form(default="day"),
|
expire_style: str = Form(default="day"),
|
||||||
ip: str = Depends(ip_limit["upload"]),
|
ip: str = Depends(ip_limit["upload"]),
|
||||||
):
|
):
|
||||||
|
validate_expire_style(expire_style)
|
||||||
text_size = len(text.encode("utf-8"))
|
text_size = len(text.encode("utf-8"))
|
||||||
max_txt_size = 222 * 1024
|
max_txt_size = 222 * 1024
|
||||||
if text_size > max_txt_size:
|
if text_size > max_txt_size:
|
||||||
@@ -186,8 +189,7 @@ async def share_file(
|
|||||||
):
|
):
|
||||||
file_size = await validate_file_size(file, settings.uploadSize)
|
file_size = await validate_file_size(file, settings.uploadSize)
|
||||||
validate_file_type(file.filename or "", file.content_type)
|
validate_file_type(file.filename or "", file.content_type)
|
||||||
if expire_style not in settings.expireStyle:
|
validate_expire_style(expire_style)
|
||||||
raise HTTPException(status_code=400, detail="过期时间类型错误")
|
|
||||||
path, suffix, prefix, uuid_file_name, save_path = await get_file_path_name(file)
|
path, suffix, prefix, uuid_file_name, save_path = await get_file_path_name(file)
|
||||||
reservation_token = f"file:{uuid.uuid4().hex}"
|
reservation_token = f"file:{uuid.uuid4().hex}"
|
||||||
await reserve_storage(reservation_token, file_size, ttl_seconds=3600)
|
await reserve_storage(reservation_token, file_size, ttl_seconds=3600)
|
||||||
@@ -334,6 +336,17 @@ async def get_code_file(code: str, ip: str = Depends(ip_limit["error"])):
|
|||||||
assert isinstance(file_code, FileCodes)
|
assert isinstance(file_code, FileCodes)
|
||||||
if not await consume_file_usage(file_code):
|
if not await consume_file_usage(file_code):
|
||||||
return APIResponse(code=404, detail="文件已过期")
|
return APIResponse(code=404, detail="文件已过期")
|
||||||
|
if file_code.text is not None:
|
||||||
|
filename = f"{file_code.prefix or 'Text'}{file_code.suffix or '.txt'}"
|
||||||
|
return Response(
|
||||||
|
content=file_code.text,
|
||||||
|
media_type="text/plain",
|
||||||
|
headers={
|
||||||
|
"Content-Disposition": (
|
||||||
|
f"attachment; filename*=UTF-8''{quote(filename, safe='')}"
|
||||||
|
)
|
||||||
|
},
|
||||||
|
)
|
||||||
return await file_storage.get_file_response(file_code)
|
return await file_storage.get_file_response(file_code)
|
||||||
|
|
||||||
|
|
||||||
@@ -362,7 +375,12 @@ async def select_file(data: SelectFileModel, ip: str = Depends(ip_limit["error"]
|
|||||||
async def download_file(key: str, code: str, ip: str = Depends(ip_limit["error"])):
|
async def download_file(key: str, code: str, ip: str = Depends(ip_limit["error"])):
|
||||||
file_storage: FileStorageInterface = storages[settings.file_storage]()
|
file_storage: FileStorageInterface = storages[settings.file_storage]()
|
||||||
normalized_code = normalize_share_code(code)
|
normalized_code = normalize_share_code(code)
|
||||||
if await get_select_token(normalized_code) != key:
|
# 同时接受当前窗口与上一窗口 token,避免时间窗边界竞态导致偶发 403
|
||||||
|
valid_keys = {
|
||||||
|
await get_select_token(normalized_code, offset=0),
|
||||||
|
await get_select_token(normalized_code, offset=1),
|
||||||
|
}
|
||||||
|
if key not in valid_keys:
|
||||||
ip_limit["error"].add_ip(ip)
|
ip_limit["error"].add_ip(ip)
|
||||||
raise HTTPException(status_code=403, detail="下载鉴权失败")
|
raise HTTPException(status_code=403, detail="下载鉴权失败")
|
||||||
has, file_code = await get_code_file_by_code(normalized_code)
|
has, file_code = await get_code_file_by_code(normalized_code)
|
||||||
@@ -647,6 +665,7 @@ async def complete_upload(
|
|||||||
chunk_info = await UploadChunk.filter(upload_id=upload_id, chunk_index=-1).first()
|
chunk_info = await UploadChunk.filter(upload_id=upload_id, chunk_index=-1).first()
|
||||||
if not chunk_info:
|
if not chunk_info:
|
||||||
raise HTTPException(status.HTTP_404_NOT_FOUND, detail="上传会话不存在")
|
raise HTTPException(status.HTTP_404_NOT_FOUND, detail="上传会话不存在")
|
||||||
|
validate_expire_style(data.expire_style)
|
||||||
await reserve_storage(
|
await reserve_storage(
|
||||||
f"chunk:{upload_id}",
|
f"chunk:{upload_id}",
|
||||||
chunk_info.file_size,
|
chunk_info.file_size,
|
||||||
@@ -763,8 +782,7 @@ async def presign_upload_init(
|
|||||||
403,
|
403,
|
||||||
f"文件大小超过限制,最大为 {settings.uploadSize / (1024 * 1024):.2f} MB",
|
f"文件大小超过限制,最大为 {settings.uploadSize / (1024 * 1024):.2f} MB",
|
||||||
)
|
)
|
||||||
if data.expire_style not in settings.expireStyle:
|
validate_expire_style(data.expire_style)
|
||||||
raise HTTPException(400, "过期时间类型错误")
|
|
||||||
|
|
||||||
upload_id = uuid.uuid4().hex
|
upload_id = uuid.uuid4().hex
|
||||||
reservation_token = f"presign:{upload_id}"
|
reservation_token = f"presign:{upload_id}"
|
||||||
|
|||||||
+10
-3
@@ -25,6 +25,7 @@ from core.settings import data_root, settings
|
|||||||
from apps.base.models import FileCodes, UploadChunk
|
from apps.base.models import FileCodes, UploadChunk
|
||||||
from core.utils import get_file_url, sanitize_filename
|
from core.utils import get_file_url, sanitize_filename
|
||||||
from fastapi.responses import FileResponse, StreamingResponse
|
from fastapi.responses import FileResponse, StreamingResponse
|
||||||
|
from starlette.background import BackgroundTask
|
||||||
|
|
||||||
|
|
||||||
class FileStorageInterface:
|
class FileStorageInterface:
|
||||||
@@ -395,7 +396,9 @@ class S3FileStorage(FileStorageInterface):
|
|||||||
return StreamingResponse(
|
return StreamingResponse(
|
||||||
stream_generator(),
|
stream_generator(),
|
||||||
media_type="application/octet-stream",
|
media_type="application/octet-stream",
|
||||||
headers=headers
|
headers=headers,
|
||||||
|
# 兜底关闭会话:客户端中断时与 generator finally 双保险
|
||||||
|
background=BackgroundTask(session.close),
|
||||||
)
|
)
|
||||||
except HTTPException:
|
except HTTPException:
|
||||||
raise
|
raise
|
||||||
@@ -733,7 +736,9 @@ class OneDriveFileStorage(FileStorageInterface):
|
|||||||
return StreamingResponse(
|
return StreamingResponse(
|
||||||
stream_generator(),
|
stream_generator(),
|
||||||
media_type="application/octet-stream",
|
media_type="application/octet-stream",
|
||||||
headers=headers
|
headers=headers,
|
||||||
|
# 兜底关闭会话:客户端中断时与 generator finally 双保险
|
||||||
|
background=BackgroundTask(session.close),
|
||||||
)
|
)
|
||||||
except HTTPException:
|
except HTTPException:
|
||||||
raise
|
raise
|
||||||
@@ -1209,7 +1214,9 @@ class WebDAVFileStorage(FileStorageInterface):
|
|||||||
return StreamingResponse(
|
return StreamingResponse(
|
||||||
stream_generator(),
|
stream_generator(),
|
||||||
media_type="application/octet-stream",
|
media_type="application/octet-stream",
|
||||||
headers=headers
|
headers=headers,
|
||||||
|
# 兜底关闭会话:客户端中断时与 generator finally 双保险
|
||||||
|
background=BackgroundTask(session.close),
|
||||||
)
|
)
|
||||||
except aiohttp.ClientError as e:
|
except aiohttp.ClientError as e:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
|
|||||||
+8
-3
@@ -41,17 +41,22 @@ async def get_now():
|
|||||||
return datetime.datetime.now(datetime.timezone(datetime.timedelta(hours=8)))
|
return datetime.datetime.now(datetime.timezone(datetime.timedelta(hours=8)))
|
||||||
|
|
||||||
|
|
||||||
async def get_select_token(code: str):
|
async def get_select_token(code: str, offset: int = 0):
|
||||||
"""
|
"""
|
||||||
获取下载token
|
获取下载token
|
||||||
:param code:
|
:param code: 取件码
|
||||||
|
:param offset: 时间窗口偏移(0=当前窗口,1=上一个窗口)。
|
||||||
|
用于兼容窗口边界竞态:用户在某窗口末尾获取的 token,
|
||||||
|
请求到达服务器时可能已进入下一窗口。
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
token = getattr(settings, "jwt_secret", "")
|
token = getattr(settings, "jwt_secret", "")
|
||||||
if not token:
|
if not token:
|
||||||
raise RuntimeError("应用签名密钥未初始化")
|
raise RuntimeError("应用签名密钥未初始化")
|
||||||
|
# 每个窗口约 1000 秒;offset 允许校验上一窗口,避免边界竞态
|
||||||
|
time_factor = int(time.time() / 1000) - max(0, int(offset))
|
||||||
return hashlib.sha256(
|
return hashlib.sha256(
|
||||||
f"{code}{int(time.time() / 1000)}000{token}".encode()
|
f"{code}{time_factor}000{token}".encode()
|
||||||
).hexdigest()
|
).hexdigest()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
version: "3"
|
version: "3"
|
||||||
services:
|
services:
|
||||||
file-code-box:
|
file-code-box:
|
||||||
image: lanol/filecodebox:2.5.0 # x-release-please-version
|
image: lanol/filecodebox:2.5.2 # x-release-please-version
|
||||||
volumes:
|
volumes:
|
||||||
- fcb-data:/app/data:rw
|
- fcb-data:/app/data:rw
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
|||||||
@@ -1,5 +1,20 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## [2.5.2](https://github.com/vastsa/FileCodeBox/compare/v2.5.1...v2.5.2) (2026-07-23)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* harden share expiry, download token, quota SQL, and CORS ([ae77026](https://github.com/vastsa/FileCodeBox/commit/ae7702603a99dc77b9b48e78746058b2a74b0bf5))
|
||||||
|
* harden share expiry, download token, quota SQL, and CORS ([a41693f](https://github.com/vastsa/FileCodeBox/commit/a41693f90ded0234e0883c399254319bef04924a))
|
||||||
|
|
||||||
|
## [2.5.1](https://github.com/vastsa/FileCodeBox/compare/v2.5.0...v2.5.1) (2026-07-21)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* patch CVE dependencies and path traversal risks ([00f3abd](https://github.com/vastsa/FileCodeBox/commit/00f3abd3b20fe2344e4dbf8d53714773e23a1050))
|
||||||
|
|
||||||
## [2.5.0](https://github.com/vastsa/FileCodeBox/compare/v2.4.0...v2.5.0) (2026-07-11)
|
## [2.5.0](https://github.com/vastsa/FileCodeBox/compare/v2.4.0...v2.5.0) (2026-07-11)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
<!-- Current release: 2.5.0 x-release-please-version -->
|
<!-- Current release: 2.5.2 x-release-please-version -->
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -38,7 +38,7 @@ docker run -d --restart unless-stopped \
|
|||||||
-p 12345:12345 \
|
-p 12345:12345 \
|
||||||
-v ./data:/app/data \
|
-v ./data:/app/data \
|
||||||
--name filecodebox \
|
--name filecodebox \
|
||||||
lanol/filecodebox:2.5.0 # x-release-please-version
|
lanol/filecodebox:2.5.2 # x-release-please-version
|
||||||
```
|
```
|
||||||
|
|
||||||
Open `http://localhost:12345` and complete the first-run setup.
|
Open `http://localhost:12345` and complete the first-run setup.
|
||||||
|
|||||||
+1
-1
@@ -38,7 +38,7 @@ docker run -d --restart unless-stopped \
|
|||||||
-p 12345:12345 \
|
-p 12345:12345 \
|
||||||
-v ./data:/app/data \
|
-v ./data:/app/data \
|
||||||
--name filecodebox \
|
--name filecodebox \
|
||||||
lanol/filecodebox:2.5.0 # x-release-please-version
|
lanol/filecodebox:2.5.2 # x-release-please-version
|
||||||
```
|
```
|
||||||
|
|
||||||
打开 `http://localhost:12345`,按引导完成首次初始化。
|
打开 `http://localhost:12345`,按引导完成首次初始化。
|
||||||
|
|||||||
@@ -767,7 +767,9 @@ async def refresh_settings_middleware(request, call_next):
|
|||||||
app.add_middleware(
|
app.add_middleware(
|
||||||
CORSMiddleware,
|
CORSMiddleware,
|
||||||
allow_origins=["*"],
|
allow_origins=["*"],
|
||||||
allow_credentials=True,
|
# 前端使用 Bearer Token,不依赖 Cookie credentials。
|
||||||
|
# allow_origins=["*"] 与 allow_credentials=True 组合不符合 CORS 规范。
|
||||||
|
allow_credentials=False,
|
||||||
allow_methods=["*"],
|
allow_methods=["*"],
|
||||||
allow_headers=["*"],
|
allow_headers=["*"],
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -24,7 +24,7 @@
|
|||||||
## 一条命令开始
|
## 一条命令开始
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
docker run -d --restart unless-stopped -p 12345:12345 -v ./data:/app/data --name filecodebox lanol/filecodebox:2.5.0 # x-release-please-version
|
docker run -d --restart unless-stopped -p 12345:12345 -v ./data:/app/data --name filecodebox lanol/filecodebox:2.5.2 # x-release-please-version
|
||||||
```
|
```
|
||||||
|
|
||||||
访问 `http://localhost:12345`,完成首次初始化。生产环境建议固定版本号;`latest` 指向最新正式版。
|
访问 `http://localhost:12345`,完成首次初始化。生产环境建议固定版本号;`latest` 指向最新正式版。
|
||||||
|
|||||||
+1
-1
@@ -24,7 +24,7 @@ A lightweight, modern, self-hosted file sharing service. No account required—u
|
|||||||
## Start with one command
|
## Start with one command
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
docker run -d --restart unless-stopped -p 12345:12345 -v ./data:/app/data --name filecodebox lanol/filecodebox:2.5.0 # x-release-please-version
|
docker run -d --restart unless-stopped -p 12345:12345 -v ./data:/app/data --name filecodebox lanol/filecodebox:2.5.2 # x-release-please-version
|
||||||
```
|
```
|
||||||
|
|
||||||
Open `http://localhost:12345` and complete first-run setup. Pin a version in production; `latest` tracks the newest stable release.
|
Open `http://localhost:12345` and complete first-run setup. Pin a version in production; `latest` tracks the newest stable release.
|
||||||
|
|||||||
@@ -71,6 +71,7 @@ class ShareUsageSecurityTests(unittest.TestCase):
|
|||||||
await self._assert_count_consumption_is_atomic()
|
await self._assert_count_consumption_is_atomic()
|
||||||
await self._assert_time_expiration_and_usage_are_atomic()
|
await self._assert_time_expiration_and_usage_are_atomic()
|
||||||
await self._assert_download_url_is_single_use()
|
await self._assert_download_url_is_single_use()
|
||||||
|
await self._assert_text_share_downloads_as_txt()
|
||||||
await self._assert_limited_files_do_not_expose_direct_urls()
|
await self._assert_limited_files_do_not_expose_direct_urls()
|
||||||
await self._assert_metadata_counts_all_attempts()
|
await self._assert_metadata_counts_all_attempts()
|
||||||
finally:
|
finally:
|
||||||
@@ -156,6 +157,30 @@ class ShareUsageSecurityTests(unittest.TestCase):
|
|||||||
self.assertEqual(record.expired_count, 0)
|
self.assertEqual(record.expired_count, 0)
|
||||||
self.assertEqual(record.used_count, 1)
|
self.assertEqual(record.used_count, 1)
|
||||||
|
|
||||||
|
async def _assert_text_share_downloads_as_txt(self):
|
||||||
|
record = await FileCodes.create(
|
||||||
|
code="text-download",
|
||||||
|
prefix="TAG",
|
||||||
|
text="hello, 文本直链",
|
||||||
|
expired_count=-1,
|
||||||
|
expired_at=None,
|
||||||
|
)
|
||||||
|
|
||||||
|
with patch.dict(views.storages, {"local": FakeStorage}):
|
||||||
|
response = await views.get_code_file(
|
||||||
|
code=record.code, ip="127.0.0.1"
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
self.assertEqual(response.body.decode("utf-8"), record.text)
|
||||||
|
self.assertEqual(response.media_type, "text/plain")
|
||||||
|
self.assertEqual(
|
||||||
|
response.headers["content-disposition"],
|
||||||
|
"attachment; filename*=UTF-8''TAG.txt",
|
||||||
|
)
|
||||||
|
await record.refresh_from_db()
|
||||||
|
self.assertEqual(record.used_count, 1)
|
||||||
|
|
||||||
async def _assert_limited_files_do_not_expose_direct_urls(self):
|
async def _assert_limited_files_do_not_expose_direct_urls(self):
|
||||||
record = await FileCodes.create(
|
record = await FileCodes.create(
|
||||||
code="proxy-only",
|
code="proxy-only",
|
||||||
|
|||||||
@@ -0,0 +1,141 @@
|
|||||||
|
import asyncio
|
||||||
|
import hashlib
|
||||||
|
import time
|
||||||
|
import unittest
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
from fastapi import HTTPException
|
||||||
|
from tortoise import Tortoise
|
||||||
|
|
||||||
|
from apps.base import views
|
||||||
|
from apps.base.models import FileCodes
|
||||||
|
from apps.base.quota import _detect_sql_dialect, _sql_placeholders, reserve_storage
|
||||||
|
from apps.base.utils import validate_expire_style
|
||||||
|
from core.settings import settings
|
||||||
|
from core.utils import get_select_token
|
||||||
|
|
||||||
|
|
||||||
|
class FakeStorage:
|
||||||
|
async def get_file_url(self, file_code):
|
||||||
|
return f"https://example.invalid/{file_code.code}"
|
||||||
|
|
||||||
|
async def get_file_response(self, file_code):
|
||||||
|
return {"downloaded": file_code.code}
|
||||||
|
|
||||||
|
|
||||||
|
class SecurityPatchTests(unittest.TestCase):
|
||||||
|
def test_validate_expire_style_rejects_unknown_mode(self):
|
||||||
|
original = dict(settings.user_config)
|
||||||
|
try:
|
||||||
|
settings.expireStyle = ["day", "count"]
|
||||||
|
self.assertEqual(validate_expire_style("day"), "day")
|
||||||
|
with self.assertRaises(HTTPException) as ctx:
|
||||||
|
validate_expire_style("forever")
|
||||||
|
self.assertEqual(ctx.exception.status_code, 400)
|
||||||
|
finally:
|
||||||
|
settings.user_config = original
|
||||||
|
|
||||||
|
def test_sql_placeholders_follow_dialect(self):
|
||||||
|
asyncio.run(self._assert_sql_placeholders())
|
||||||
|
|
||||||
|
async def _assert_sql_placeholders(self):
|
||||||
|
await Tortoise.init(
|
||||||
|
config={
|
||||||
|
"connections": {
|
||||||
|
"default": {
|
||||||
|
"engine": "tortoise.backends.sqlite",
|
||||||
|
"credentials": {"file_path": ":memory:"},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"apps": {
|
||||||
|
"models": {
|
||||||
|
"models": ["apps.base.models"],
|
||||||
|
"default_connection": "default",
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"use_tz": False,
|
||||||
|
"timezone": "Asia/Shanghai",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
self.assertEqual(_detect_sql_dialect(), "sqlite")
|
||||||
|
self.assertEqual(_sql_placeholders(3), ["?", "?", "?"])
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"apps.base.quota._detect_sql_dialect", return_value="postgres"
|
||||||
|
):
|
||||||
|
self.assertEqual(_sql_placeholders(3), ["$1", "$2", "$3"])
|
||||||
|
with patch("apps.base.quota._detect_sql_dialect", return_value="mysql"):
|
||||||
|
self.assertEqual(_sql_placeholders(2), ["%s", "%s"])
|
||||||
|
|
||||||
|
# sqlite 下 reserve_storage 仍可正常工作
|
||||||
|
settings.storageLimit = 100
|
||||||
|
await Tortoise.generate_schemas()
|
||||||
|
await reserve_storage("patch-token", 10, 300)
|
||||||
|
finally:
|
||||||
|
settings.storageLimit = 0
|
||||||
|
await Tortoise.close_connections()
|
||||||
|
|
||||||
|
def test_download_token_accepts_previous_window(self):
|
||||||
|
asyncio.run(self._assert_download_token_window())
|
||||||
|
|
||||||
|
async def _assert_download_token_window(self):
|
||||||
|
original = dict(settings.user_config)
|
||||||
|
settings.jwt_secret = "test-secret-key-for-token-window"
|
||||||
|
now = 2_000_500 # 落在窗口 2000 内
|
||||||
|
code = "ABCDE"
|
||||||
|
with patch("core.utils.time.time", return_value=now):
|
||||||
|
current = await get_select_token(code, offset=0)
|
||||||
|
previous = await get_select_token(code, offset=1)
|
||||||
|
|
||||||
|
expected_current = hashlib.sha256(
|
||||||
|
f"{code}{int(now / 1000)}000{settings.jwt_secret}".encode()
|
||||||
|
).hexdigest()
|
||||||
|
expected_previous = hashlib.sha256(
|
||||||
|
f"{code}{int(now / 1000) - 1}000{settings.jwt_secret}".encode()
|
||||||
|
).hexdigest()
|
||||||
|
self.assertEqual(current, expected_current)
|
||||||
|
self.assertEqual(previous, expected_previous)
|
||||||
|
|
||||||
|
await Tortoise.init(
|
||||||
|
config={
|
||||||
|
"connections": {
|
||||||
|
"default": {
|
||||||
|
"engine": "tortoise.backends.sqlite",
|
||||||
|
"credentials": {"file_path": ":memory:"},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"apps": {
|
||||||
|
"models": {
|
||||||
|
"models": ["apps.base.models"],
|
||||||
|
"default_connection": "default",
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"use_tz": False,
|
||||||
|
"timezone": "Asia/Shanghai",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
await Tortoise.generate_schemas()
|
||||||
|
try:
|
||||||
|
await FileCodes.create(
|
||||||
|
code=code,
|
||||||
|
prefix="demo",
|
||||||
|
suffix=".txt",
|
||||||
|
expired_count=-1,
|
||||||
|
size=1,
|
||||||
|
)
|
||||||
|
# 模拟请求进入下一窗口,但仍携带上一窗口 token
|
||||||
|
next_window = now + 1000
|
||||||
|
with patch("core.utils.time.time", return_value=next_window):
|
||||||
|
with patch.dict(views.storages, {"local": FakeStorage}):
|
||||||
|
result = await views.download_file(
|
||||||
|
key=current, code=code, ip="127.0.0.1"
|
||||||
|
)
|
||||||
|
self.assertEqual(result, {"downloaded": code})
|
||||||
|
finally:
|
||||||
|
settings.user_config = original
|
||||||
|
await Tortoise.close_connections()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
Reference in new issue
Block a user