Files
FastapiAdmin/backend/app/modules/task/cronjob/job/service.py
T
zhangtao 07fa789d2e fix(backend): 安全默认值、异常语义、代码生成注入面与定时任务内置处理器
本批为 t10 → t13 → t15 三轮实现 + 一轮复核驳回整改的最终形态(pytest 83 passed / ruff clean,
已部署并线上验证)。

异常语义
- CustomException 默认状态码 500 → 400(约 433 处裸调用不再被计为服务端故障),
  且「包装底层异常」的 130 处显式恢复 500;get_or_404 → 404;响应体结构不变

代码生成注入面(复核 blocker F1,二次修复)
- column_type 不再做字符串拼接:改为常量白名单派生(类型名 ∈ DB_TO_SQLALCHEMY ∪ {Boolean},
  长度仅取数字/正则捕获),彻底移除 split("(")[1]
- 列类型加形状白名单;字符串/注释/文档字符串用 \uXXXX,标识符过 safe_ident(含 Python/JS 关键字),
  .vue 文本与静态属性改 HTML 实体
- 落盘闸门由 ast.parse 升级为 assert_python_structure(语法 + 顶层语句白名单 + 与哨兵骨架逐节点比对)
  与 assert_text_structure(.vue/.ts/.toml 行结构);U+2028/2029 与 C1 控制符纳入转义

安全默认值
- 生产 CORS 不再回落 ["*"](未配置即拒绝跨域,可选 CORS_STRICT_STARTUP 直接启动失败)
- OAUTH_ALLOWED_HOSTS 不再默认 "*";新增 ALLOW_LOCALHOST_HOSTS 控制 Host 校验范围
- ALLOWED_HOSTS 收窄为按配置派生;健康检查暴露 code_exec_enabled / cors_origins_configured

定时任务
- SCHEDULER_ALLOW_CODE_EXEC 默认 False;新增 builtin:<模块名>[.<函数名>] 内置处理器路线(不经 exec)
- 判定收敛到 describe_schedulability;列表返回 schedulable / unschedulable_reason;
  启动 reconcile 与状态查询对「已启用但不可调度」打 WARN(去重、恢复后清除)
- 加固:内置处理器必须在本模块内定义或经 HANDLERS 声明,拒绝模块内导入的外部对象(复核 R1)
- 顺带修复:_WARNED_UNSCHEDULABLE 的 set.pop TypeError、job.next_run_time 缺失导致的列表 500

测试:新增 tests/test_generator_template_safety.py、test_security_defaults.py、
test_scheduler_schedulability.py(含恶意载荷、闸门红绿、静态护栏)
2026-10-04 05:12:32 +08:00

164 lines
5.9 KiB
Python

from sqlalchemy.ext.asyncio import AsyncSession
from app.config.setting import settings
from app.core.ap_scheduler import SchedulerUtil
from app.core.base_schema import AuthSchema, PageResultSchema
from app.core.exceptions import CustomException
from app.utils.common_util import search_to_dict
from .crud import JobCRUD
from .schema import (
JobOutSchema,
JobQueryParam,
SchedulerJobModifySchema,
SchedulerJobSchema,
SchedulerStatusSchema,
)
class JobService:
"""调度器监控模块服务层"""
def __init__(self, auth: AuthSchema, db: AsyncSession) -> None:
self.auth = auth
self.db = db
async def get_job_log_detail(self, id: int) -> JobOutSchema:
obj = await JobCRUD(self.auth, self.db).get_obj_by_id_crud(id=id)
if not obj:
raise CustomException(msg="执行日志不存在")
return JobOutSchema.model_validate(obj)
async def get_job_log_list(
self,
search: JobQueryParam | None = None,
order_by: list[dict[str, str]] | None = None,
) -> list[JobOutSchema]:
if order_by is None:
order_by = [{"created_time": "desc"}]
obj_list = await JobCRUD(self.auth, self.db).get_obj_list_crud(search=search_to_dict(search, {}), order_by=order_by)
return [JobOutSchema.model_validate(obj) for obj in obj_list]
async def get_job_log_page(
self,
page_no: int,
page_size: int,
search: JobQueryParam | None = None,
order_by: list[dict[str, str]] | None = None,
) -> PageResultSchema[JobOutSchema]:
offset = (page_no - 1) * page_size
ob = order_by or [{"created_time": "desc"}]
return await JobCRUD(self.auth, self.db).page(
offset=offset,
limit=page_size,
order_by=ob,
search=search_to_dict(search, {}),
out_schema=JobOutSchema,
)
async def delete_job_log(self, ids: list[int]) -> None:
if not ids:
raise CustomException(msg="删除失败,删除对象不能为空")
await JobCRUD(self.auth, self.db).delete_obj_crud(ids=ids)
async def clear_job_log(self) -> None:
await JobCRUD(self.auth, self.db).clear_obj_crud()
# APScheduler 状态常量: 0=停止, 1=运行中, 2=暂停
_SCHEDULER_STATE_MAP: dict[int, str] = {0: "停止", 1: "运行中", 2: "暂停"}
@staticmethod
def get_scheduler_status() -> SchedulerStatusSchema:
state = SchedulerUtil.get_scheduler_state()
is_running = SchedulerUtil.is_running()
jobs = SchedulerUtil.get_jobs()
# 可观测性(t15):把"已注册但当前不可执行"的任务显式打出并计数,
# 运维不必逐个点开才知道开关调整/处理器改名后哪些任务静默失效
unschedulable = SchedulerUtil.log_unschedulable_jobs(context="调度器状态查询")
return SchedulerStatusSchema(
status=JobService._SCHEDULER_STATE_MAP.get(state, "未知"),
is_running=is_running,
job_count=len(jobs),
code_exec_enabled=settings.SCHEDULER_ALLOW_CODE_EXEC,
unschedulable_count=len(unschedulable),
)
@staticmethod
def get_scheduler_jobs() -> list[SchedulerJobSchema]:
jobs = SchedulerUtil.get_jobs()
items: list[SchedulerJobSchema] = []
for job in jobs:
schedulable, reason = SchedulerUtil.describe_schedulability(SchedulerUtil.job_code_block(job))
items.append(
SchedulerJobSchema(
id=job.id,
name=job.name,
trigger=str(job.trigger),
# pending/未排程任务没有 next_run_time 属性(APScheduler 只在真正排程后设置),必须 getattr 兜底
next_run_time=str(job.next_run_time) if getattr(job, "next_run_time", None) else None,
status=SchedulerUtil.get_job_status(job_id=job.id),
schedulable=schedulable,
unschedulable_reason=None if schedulable else reason,
),
)
return items
# ─── 调度器与任务运维(controller 唯一入口,禁止直调 SchedulerUtil)───
@staticmethod
def _ensure_job_exists(job_id: str) -> None:
if not SchedulerUtil.get_job(job_id=job_id):
raise CustomException(msg=f"任务 {job_id} 不存在或已从调度器移除")
@staticmethod
async def start_scheduler() -> None:
await SchedulerUtil.start()
@staticmethod
def pause_scheduler() -> None:
SchedulerUtil.pause()
@staticmethod
def resume_scheduler() -> None:
SchedulerUtil.resume()
@staticmethod
async def shutdown_scheduler() -> None:
await SchedulerUtil.shutdown()
@staticmethod
def clear_scheduler_jobs() -> None:
SchedulerUtil.clear_jobs()
@staticmethod
def get_scheduler_console() -> str:
return SchedulerUtil.print_jobs()
@staticmethod
def pause_job(job_id: str) -> None:
JobService._ensure_job_exists(job_id=job_id)
SchedulerUtil.pause_job(job_id=job_id)
@staticmethod
def resume_job(job_id: str) -> None:
JobService._ensure_job_exists(job_id=job_id)
SchedulerUtil.resume_job(job_id=job_id)
@staticmethod
def run_job_now(job_id: str) -> None:
JobService._ensure_job_exists(job_id=job_id)
SchedulerUtil.run_job_now(job_id=job_id)
@staticmethod
def modify_job(job_id: str, data: SchedulerJobModifySchema) -> None:
JobService._ensure_job_exists(job_id=job_id)
changes = data.to_changes()
if not changes:
raise CustomException(msg="没有需要修改的任务属性")
SchedulerUtil.modify_job(job_id=job_id, **changes)
@staticmethod
def remove_job(job_id: str) -> None:
JobService._ensure_job_exists(job_id=job_id)
SchedulerUtil.remove_job(job_id=job_id)