mirror of
https://github.com/fawney19/Aether.git
synced 2026-09-02 17:30:23 +08:00
feat: 用户用量页面支持分页、搜索和密钥信息展示
- 用户用量API增加search参数支持密钥名、模型名搜索 - 用户用量API返回api_key信息(id、name、display) - 用户页面记录表格增加密钥列显示 - 前端统一管理员和用户页面的分页/搜索逻辑 - 后端LIKE查询增加特殊字符转义防止SQL注入 - 添加escape_like_pattern和safe_truncate_escaped工具函数
This commit is contained in:
@@ -7,6 +7,59 @@ from typing import Any
|
||||
from sqlalchemy import func
|
||||
|
||||
|
||||
def escape_like_pattern(pattern: str) -> str:
|
||||
"""
|
||||
转义 SQL LIKE 语句中的特殊字符(%、_、\\)
|
||||
|
||||
Args:
|
||||
pattern: 原始搜索模式
|
||||
|
||||
Returns:
|
||||
转义后的模式,可安全用于 LIKE 查询(需配合 escape="\\\\")
|
||||
|
||||
Examples:
|
||||
>>> escape_like_pattern("hello_world%test")
|
||||
'hello\\\\_world\\\\%test'
|
||||
"""
|
||||
return pattern.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_")
|
||||
|
||||
|
||||
def safe_truncate_escaped(escaped: str, max_len: int) -> str:
|
||||
"""
|
||||
安全截断已转义的字符串,避免截断在转义序列中间
|
||||
|
||||
转义后的字符串中,反斜杠总是成对出现(\\\\)或作为转义符(\\%, \\_)。
|
||||
如果在某个位置截断导致末尾有奇数个反斜杠,说明截断发生在转义序列中间,
|
||||
需要去掉最后一个反斜杠以保持转义完整性。
|
||||
|
||||
Args:
|
||||
escaped: 已经过 escape_like_pattern 处理的字符串
|
||||
max_len: 最大长度
|
||||
|
||||
Returns:
|
||||
截断后的字符串,保证不会破坏转义序列
|
||||
"""
|
||||
if len(escaped) <= max_len:
|
||||
return escaped
|
||||
|
||||
truncated = escaped[:max_len]
|
||||
|
||||
# 统计末尾连续的反斜杠数量
|
||||
trailing_backslashes = 0
|
||||
for i in range(len(truncated) - 1, -1, -1):
|
||||
if truncated[i] == "\\":
|
||||
trailing_backslashes += 1
|
||||
else:
|
||||
break
|
||||
|
||||
# 如果末尾反斜杠数量为奇数,说明截断在转义序列中间
|
||||
# 需要去掉最后一个反斜杠
|
||||
if trailing_backslashes % 2 == 1:
|
||||
truncated = truncated[:-1]
|
||||
|
||||
return truncated
|
||||
|
||||
|
||||
def date_trunc_portable(dialect_name: str, interval: str, column: Any) -> Any:
|
||||
"""
|
||||
跨数据库的日期截断函数
|
||||
|
||||
Reference in New Issue
Block a user