5bf036bb81
Project CI / AI game creator shell Rust shard 4/4 (push) Has been cancelled
Project CI / AI game creator shell Rust shard 3/4 (push) Has been cancelled
Project CI / AI game creator shell Rust shard 2/4 (push) Has been cancelled
Project CI / AI game creator shell Rust smoke (push) Has been cancelled
Project CI / AI game creator shell Rust crates (push) Has been cancelled
Project CI / Backend tests (push) Has been cancelled
Project CI / Native shell tests (push) Has been cancelled
Project CI / Frontend tests (push) Has been cancelled
Project CI / Repository checks (push) Has been cancelled
Project CI / AI game creator shell web tests (push) Has been cancelled
Project CI / AI game creator shell Rust shard 1/4 (push) Has been cancelled
HTTP 请求取消后,在途计数原先无法释放;项目元数据和 External API 鉴权依赖完整 AppState,相同鉴权和追踪配置也散落在多个入口。本次集中装配这些依赖和横切能力,保持现有公开 API、权限、计费、幂等和事务规则。 ## 修改 - 91 个受保护路由集中应用鉴权;保留方法级 404/405/HEAD/Allow、公开入口、MCP、精选缓存头及 2/4 MiB 请求限制。 - 七个项目元数据入口改用缓存的 EditorProjectState,External/MCP 鉴权改用 ExternalApiAuthState;生产实现复用 SpacetimeClient,媒体修复维持原有上传和登记顺序。 - RAII 覆盖请求 Future 取消和 panic unwind 的计数清理;正常与降级服务复用 TraceLayer,指标采用 MatchedPath 模板及固定兜底。 - LLM 普通与流式调用增加跳过参数的异步 span,保持父上下文、流式回调、错误与重试行为;补充替代依赖测试并同步锁文件和文档。 ## 验证 | 验证面 | 结果 | | --- | --- | | platform-llm 完整本地回归 | 161 个单元测试、3 个集成测试通过;1 个真实 Provider 用例按原配置忽略 | | api-server 完整回归 | 执行时 1075 通过、12 失败、6 忽略;其中 1 个新增公开读取 fixture 断言已修正,14 个路由契约回归随后全部通过;剩余 11 个是下述既有 Windows 失败 | | 窄依赖、取消与追踪 | 元数据 owner/幂等/revision、鉴权及 MCP 错误传播、取消/panic/流式响应、追踪父子关系与敏感参数省略均通过 | | 实际本地服务 | 独立 SpacetimeDB 上 102/102 检查通过,两个动态项目 ID 的路由模板及请求 ID 日志核验 3/3 通过 | | 编译与边界 | api-server cargo check、AGC 锁文件下 platform-llm cargo check、rustfmt、编码、文档索引、DDD 与 diff 检查通过 | | 合入最新 master 后 | 后端源码及锁文件保持已测内容;再次通过 14 个路由契约测试、3 个 Provider 追踪测试及编码/文档/DDD/diff 检查 | 实际服务检查覆盖 health/ready、两账号登录、项目 CRUD、幂等重复、跨 owner 拒绝、revision 冲突、External/MCP 读取、Key 撤销及 404/405。使用既有 test 环境的本地 Router 拒绝 fixture,未调用真实付费 Provider;自建服务已关闭,原开发实例保留。 ## 已知测试限制 API 全量测试尚未全绿:11 个 wallet_refund_outbox 用例在 Windows 的目录同步处失败。其生产文件与变更前内容一致;标准库隔离复现确认 File::open(目录) 返回 OS 5,而普通文件写入、同步及 hard_link 正常。这个已有的目录持久化问题未混入本次重构,也未通过跳过或弱化相关断言掩盖。 --------- Co-authored-by: kdletters <61648117+kdletters@users.noreply.github.com> Reviewed-on: http://192.168.35.82/git/GenarrativeAI/Genarrative/pulls/425
485 lines
18 KiB
Rust
485 lines
18 KiB
Rust
use axum::{
|
|
Extension, Router,
|
|
extract::DefaultBodyLimit,
|
|
middleware,
|
|
routing::{get, patch, post},
|
|
};
|
|
|
|
use crate::{
|
|
editor_project::EDITOR_LAYOUT_REQUEST_BODY_MAX_BYTES,
|
|
external_api_auth::{require_external_api_key, require_external_mcp_api_key},
|
|
external_assets_api::{
|
|
confirm_external_asset_object, create_external_direct_upload_ticket,
|
|
get_external_asset_read_url,
|
|
},
|
|
external_editor_api::{
|
|
create_external_editor_asset, create_external_editor_asset_folder,
|
|
create_external_editor_project, create_external_editor_project_resource,
|
|
delete_external_editor_asset, delete_external_editor_asset_folder,
|
|
delete_external_editor_project, edit_external_editor_image,
|
|
extract_external_editor_ui_design_assets, generate_external_editor_background_music,
|
|
generate_external_editor_character_animation, generate_external_editor_icon_spritesheet,
|
|
generate_external_editor_image, generate_external_editor_sound_effect,
|
|
generate_external_editor_video, get_external_editor_asset_library,
|
|
get_external_editor_generation_job, get_external_editor_project,
|
|
list_external_editor_projects, load_recent_external_editor_project, openapi_json,
|
|
remove_external_editor_image_background, rename_external_editor_project,
|
|
save_external_editor_canvas, update_external_editor_asset,
|
|
update_external_editor_asset_folder,
|
|
},
|
|
external_mcp,
|
|
external_skill_api::{
|
|
download_external_skill_archive, get_external_agent_integration_manifest,
|
|
get_external_skill_entry,
|
|
},
|
|
state::AppState,
|
|
};
|
|
|
|
pub fn router(state: AppState) -> Router<AppState> {
|
|
let mcp_router = Router::new()
|
|
.nest_service("/api/external/v1/mcp", external_mcp::service())
|
|
.layer(Extension(state.clone()))
|
|
.route_layer(middleware::from_fn_with_state(
|
|
state.clone(),
|
|
require_external_mcp_api_key,
|
|
));
|
|
|
|
let auth = middleware::from_fn_with_state(state, require_external_api_key);
|
|
let protected_routes = [
|
|
(
|
|
"/api/external/v1/assets/direct-upload-tickets",
|
|
post(create_external_direct_upload_ticket),
|
|
),
|
|
(
|
|
"/api/external/v1/assets/objects/confirm",
|
|
post(confirm_external_asset_object),
|
|
),
|
|
(
|
|
"/api/external/v1/assets/read-url",
|
|
get(get_external_asset_read_url),
|
|
),
|
|
(
|
|
"/api/external/v1/editor/projects",
|
|
get(list_external_editor_projects).post(create_external_editor_project),
|
|
),
|
|
(
|
|
"/api/external/v1/editor/projects/recent",
|
|
get(load_recent_external_editor_project),
|
|
),
|
|
(
|
|
"/api/external/v1/editor/projects/{project_id}",
|
|
get(get_external_editor_project).delete(delete_external_editor_project),
|
|
),
|
|
(
|
|
"/api/external/v1/editor/projects/{project_id}/metadata",
|
|
patch(rename_external_editor_project),
|
|
),
|
|
(
|
|
"/api/external/v1/editor/projects/{project_id}/canvas",
|
|
patch(save_external_editor_canvas)
|
|
.layer(DefaultBodyLimit::max(EDITOR_LAYOUT_REQUEST_BODY_MAX_BYTES)),
|
|
),
|
|
(
|
|
"/api/external/v1/editor/projects/{project_id}/resources",
|
|
post(create_external_editor_project_resource),
|
|
),
|
|
(
|
|
"/api/external/v1/editor/assets/library",
|
|
get(get_external_editor_asset_library),
|
|
),
|
|
(
|
|
"/api/external/v1/editor/assets/folders",
|
|
post(create_external_editor_asset_folder),
|
|
),
|
|
(
|
|
"/api/external/v1/editor/assets/folders/{folder_id}",
|
|
patch(update_external_editor_asset_folder).delete(delete_external_editor_asset_folder),
|
|
),
|
|
(
|
|
"/api/external/v1/editor/assets",
|
|
post(create_external_editor_asset),
|
|
),
|
|
(
|
|
"/api/external/v1/editor/assets/{asset_id}",
|
|
patch(update_external_editor_asset).delete(delete_external_editor_asset),
|
|
),
|
|
(
|
|
"/api/external/v1/generations/{operation_id}",
|
|
get(get_external_editor_generation_job),
|
|
),
|
|
(
|
|
"/api/external/v1/editor/images/generations",
|
|
post(generate_external_editor_image),
|
|
),
|
|
(
|
|
"/api/external/v1/editor/images/edits",
|
|
post(edit_external_editor_image),
|
|
),
|
|
(
|
|
"/api/external/v1/editor/images/background-removals",
|
|
post(remove_external_editor_image_background),
|
|
),
|
|
(
|
|
"/api/external/v1/editor/icon-spritesheets/generations",
|
|
post(generate_external_editor_icon_spritesheet),
|
|
),
|
|
(
|
|
"/api/external/v1/editor/ui-designs/assets/extractions",
|
|
post(extract_external_editor_ui_design_assets),
|
|
),
|
|
(
|
|
"/api/external/v1/editor/character-animations/generations",
|
|
post(generate_external_editor_character_animation),
|
|
),
|
|
(
|
|
"/api/external/v1/editor/videos/generations",
|
|
post(generate_external_editor_video),
|
|
),
|
|
(
|
|
"/api/external/v1/editor/audios/sound-effects/generations",
|
|
post(generate_external_editor_sound_effect),
|
|
),
|
|
(
|
|
"/api/external/v1/editor/audios/background-music/generations",
|
|
post(generate_external_editor_background_music),
|
|
),
|
|
];
|
|
// 在方法层统一鉴权,未支持的方法仍由 Axum 返回 405。
|
|
let protected_router = protected_routes
|
|
.into_iter()
|
|
.fold(Router::new(), |router, (path, methods)| {
|
|
router.route(path, methods.route_layer(auth.clone()))
|
|
});
|
|
|
|
Router::new()
|
|
.route("/api/external/v1/openapi.json", get(openapi_json))
|
|
.route(
|
|
"/api/external/v1/agent-integration.json",
|
|
get(get_external_agent_integration_manifest),
|
|
)
|
|
.route(
|
|
"/api/external/v1/skill/SKILL.md",
|
|
get(get_external_skill_entry),
|
|
)
|
|
.route(
|
|
"/api/external/v1/skill.zip",
|
|
get(download_external_skill_archive),
|
|
)
|
|
.merge(protected_router)
|
|
.merge(mcp_router)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod route_contract_tests {
|
|
use std::sync::Arc;
|
|
|
|
use axum::{
|
|
body::Body,
|
|
http::{Request, StatusCode, header},
|
|
middleware,
|
|
};
|
|
use futures_util::future::BoxFuture;
|
|
use http_body_util::BodyExt;
|
|
use spacetime_client::{
|
|
ExternalApiKeyAuthenticateRecordInput, ExternalApiKeyRecord, SpacetimeClientError,
|
|
};
|
|
use tower::ServiceExt;
|
|
|
|
use super::router;
|
|
use crate::{
|
|
config::AppConfig,
|
|
request_context::attach_request_context,
|
|
state::{AppState, ExternalApiAuthState, external_api_auth::ExternalApiKeyAuthenticator},
|
|
};
|
|
|
|
const VALID_KEY: &str = "tnr_sk_route-body-limit-fixture";
|
|
|
|
struct FixtureKeyAuthenticator;
|
|
|
|
impl ExternalApiKeyAuthenticator for FixtureKeyAuthenticator {
|
|
fn authenticate_external_api_key(
|
|
&self,
|
|
input: ExternalApiKeyAuthenticateRecordInput,
|
|
) -> BoxFuture<'_, Result<ExternalApiKeyRecord, SpacetimeClientError>> {
|
|
Box::pin(async move {
|
|
if input.key_hash != crate::external_api_keys::hash_external_api_key(VALID_KEY) {
|
|
return Err(SpacetimeClientError::Procedure(
|
|
"API Key 不存在".to_string(),
|
|
));
|
|
}
|
|
Ok(ExternalApiKeyRecord {
|
|
key_id: "key-route-fixture".to_string(),
|
|
owner_user_id: "owner-route-fixture".to_string(),
|
|
name: "route fixture".to_string(),
|
|
key_prefix: "tnr_sk_route".to_string(),
|
|
scopes: Vec::new(),
|
|
created_at: "2026-09-18T00:00:00Z".to_string(),
|
|
last_used_at: None,
|
|
revoked_at: None,
|
|
updated_at: "2026-09-18T00:00:00Z".to_string(),
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
const PROTECTED_ROUTES: &[(&str, &[&str])] = &[
|
|
("/api/external/v1/assets/direct-upload-tickets", &["POST"]),
|
|
("/api/external/v1/assets/objects/confirm", &["POST"]),
|
|
("/api/external/v1/assets/read-url", &["GET"]),
|
|
("/api/external/v1/editor/projects", &["GET", "POST"]),
|
|
("/api/external/v1/editor/projects/recent", &["GET"]),
|
|
(
|
|
"/api/external/v1/editor/projects/{project_id}",
|
|
&["GET", "DELETE"],
|
|
),
|
|
(
|
|
"/api/external/v1/editor/projects/{project_id}/metadata",
|
|
&["PATCH"],
|
|
),
|
|
(
|
|
"/api/external/v1/editor/projects/{project_id}/canvas",
|
|
&["PATCH"],
|
|
),
|
|
(
|
|
"/api/external/v1/editor/projects/{project_id}/resources",
|
|
&["POST"],
|
|
),
|
|
("/api/external/v1/editor/assets/library", &["GET"]),
|
|
("/api/external/v1/editor/assets/folders", &["POST"]),
|
|
(
|
|
"/api/external/v1/editor/assets/folders/{folder_id}",
|
|
&["PATCH", "DELETE"],
|
|
),
|
|
("/api/external/v1/editor/assets", &["POST"]),
|
|
(
|
|
"/api/external/v1/editor/assets/{asset_id}",
|
|
&["PATCH", "DELETE"],
|
|
),
|
|
("/api/external/v1/generations/{operation_id}", &["GET"]),
|
|
("/api/external/v1/editor/images/generations", &["POST"]),
|
|
("/api/external/v1/editor/images/edits", &["POST"]),
|
|
(
|
|
"/api/external/v1/editor/images/background-removals",
|
|
&["POST"],
|
|
),
|
|
(
|
|
"/api/external/v1/editor/icon-spritesheets/generations",
|
|
&["POST"],
|
|
),
|
|
(
|
|
"/api/external/v1/editor/ui-designs/assets/extractions",
|
|
&["POST"],
|
|
),
|
|
(
|
|
"/api/external/v1/editor/character-animations/generations",
|
|
&["POST"],
|
|
),
|
|
("/api/external/v1/editor/videos/generations", &["POST"]),
|
|
(
|
|
"/api/external/v1/editor/audios/sound-effects/generations",
|
|
&["POST"],
|
|
),
|
|
(
|
|
"/api/external/v1/editor/audios/background-music/generations",
|
|
&["POST"],
|
|
),
|
|
];
|
|
|
|
fn app() -> axum::Router {
|
|
let state = AppState::new(AppConfig::default())
|
|
.expect("state should build")
|
|
.with_external_api_auth_state(ExternalApiAuthState::new(Arc::new(
|
|
FixtureKeyAuthenticator,
|
|
)));
|
|
router(state.clone())
|
|
.layer(middleware::from_fn(attach_request_context))
|
|
.with_state(state)
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn protected_route_matrix_keeps_auth_methods_and_head_behavior() {
|
|
let app = app();
|
|
for (template, supported) in PROTECTED_ROUTES {
|
|
let path = template
|
|
.split('/')
|
|
.map(|segment| {
|
|
if segment.starts_with('{') {
|
|
"fixture"
|
|
} else {
|
|
segment
|
|
}
|
|
})
|
|
.collect::<Vec<_>>()
|
|
.join("/");
|
|
let mut allowed = supported.to_vec();
|
|
if allowed.contains(&"GET") {
|
|
allowed.push("HEAD");
|
|
}
|
|
allowed.sort_unstable();
|
|
for credential in [None, Some("Bearer invalid-token")] {
|
|
for method in [
|
|
"GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "TRACE",
|
|
] {
|
|
let mut request = Request::builder().method(method).uri(&path);
|
|
if let Some(credential) = credential {
|
|
request = request.header(header::AUTHORIZATION, credential);
|
|
}
|
|
let response = app
|
|
.clone()
|
|
.oneshot(request.body(Body::empty()).expect("request should build"))
|
|
.await
|
|
.expect("request should complete");
|
|
let expected = if allowed.contains(&method) {
|
|
StatusCode::UNAUTHORIZED
|
|
} else {
|
|
StatusCode::METHOD_NOT_ALLOWED
|
|
};
|
|
assert_eq!(
|
|
response.status(),
|
|
expected,
|
|
"{method} {path}, credential={credential:?}"
|
|
);
|
|
if expected == StatusCode::METHOD_NOT_ALLOWED {
|
|
let mut actual = response.headers()[header::ALLOW]
|
|
.to_str()
|
|
.expect("Allow should be ASCII")
|
|
.split(',')
|
|
.map(str::trim)
|
|
.collect::<Vec<_>>();
|
|
actual.sort_unstable();
|
|
assert_eq!(actual, allowed, "Allow for {method} {path}");
|
|
}
|
|
if method == "HEAD" {
|
|
assert!(
|
|
response
|
|
.into_body()
|
|
.collect()
|
|
.await
|
|
.expect("HEAD body should collect")
|
|
.to_bytes()
|
|
.is_empty(),
|
|
"HEAD {path} must not return a body"
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn unknown_paths_remain_not_found_before_authentication() {
|
|
for credential in [None, Some("Bearer invalid-token")] {
|
|
let mut request = Request::builder().uri("/api/external/v1/missing/unknown/route");
|
|
if let Some(credential) = credential {
|
|
request = request.header(header::AUTHORIZATION, credential);
|
|
}
|
|
let response = app()
|
|
.oneshot(request.body(Body::empty()).expect("request should build"))
|
|
.await
|
|
.expect("request should complete");
|
|
assert_eq!(response.status(), StatusCode::NOT_FOUND);
|
|
}
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn integration_documents_remain_public_and_mcp_keeps_its_auth_challenge() {
|
|
let app = app();
|
|
for path in [
|
|
"/api/external/v1/openapi.json",
|
|
"/api/external/v1/agent-integration.json",
|
|
"/api/external/v1/skill/SKILL.md",
|
|
"/api/external/v1/skill.zip",
|
|
] {
|
|
for method in ["GET", "HEAD"] {
|
|
let response = app
|
|
.clone()
|
|
.oneshot(
|
|
Request::builder()
|
|
.method(method)
|
|
.uri(path)
|
|
.header(header::AUTHORIZATION, "Bearer invalid-token")
|
|
.body(Body::empty())
|
|
.expect("document request should build"),
|
|
)
|
|
.await
|
|
.expect("document request should complete");
|
|
assert_eq!(response.status(), StatusCode::OK, "{method} {path}");
|
|
if method == "HEAD" {
|
|
assert!(
|
|
response
|
|
.into_body()
|
|
.collect()
|
|
.await
|
|
.expect("HEAD body should collect")
|
|
.to_bytes()
|
|
.is_empty()
|
|
);
|
|
}
|
|
}
|
|
}
|
|
for method in ["GET", "POST", "DELETE", "OPTIONS"] {
|
|
let response = app
|
|
.clone()
|
|
.oneshot(
|
|
Request::builder()
|
|
.method(method)
|
|
.uri("/api/external/v1/mcp")
|
|
.body(Body::empty())
|
|
.expect("MCP request should build"),
|
|
)
|
|
.await
|
|
.expect("MCP request should complete");
|
|
assert_eq!(response.status(), StatusCode::UNAUTHORIZED, "MCP {method}");
|
|
assert_eq!(
|
|
response.headers()[header::WWW_AUTHENTICATE],
|
|
"Bearer realm=\"genarrative-external-editor\""
|
|
);
|
|
}
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn canvas_authentication_precedes_body_limit_rejection() {
|
|
let response = app()
|
|
.oneshot(
|
|
Request::builder()
|
|
.method("PATCH")
|
|
.uri("/api/external/v1/editor/projects/fixture/canvas")
|
|
.header(header::CONTENT_TYPE, "application/json")
|
|
.body(Body::from("x".repeat(
|
|
crate::editor_project::EDITOR_LAYOUT_REQUEST_BODY_MAX_BYTES + 1,
|
|
)))
|
|
.expect("oversized request should build"),
|
|
)
|
|
.await
|
|
.expect("oversized request should complete");
|
|
assert_eq!(response.status(), StatusCode::UNAUTHORIZED);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn authenticated_canvas_keeps_its_four_mebibyte_body_limit() {
|
|
let app = app();
|
|
for (size, expected) in [
|
|
(2 * 1024 * 1024 + 1, StatusCode::UNPROCESSABLE_ENTITY),
|
|
(
|
|
crate::editor_project::EDITOR_LAYOUT_REQUEST_BODY_MAX_BYTES + 1,
|
|
StatusCode::PAYLOAD_TOO_LARGE,
|
|
),
|
|
] {
|
|
let response = app
|
|
.clone()
|
|
.oneshot(
|
|
Request::builder()
|
|
.method("PATCH")
|
|
.uri("/api/external/v1/editor/projects/fixture/canvas")
|
|
.header(header::AUTHORIZATION, format!("Bearer {VALID_KEY}"))
|
|
.header(header::CONTENT_TYPE, "application/json")
|
|
.body(Body::from(format!("\"{}\"", "x".repeat(size))))
|
|
.expect("canvas request should build"),
|
|
)
|
|
.await
|
|
.expect("canvas request should complete");
|
|
assert_eq!(response.status(), expected, "size={size}");
|
|
}
|
|
}
|
|
}
|