feat(resource/broker): 实现受保护凭证代办与项目 readiness #20

Merged
lanrtop merged 5 commits from feat/project-resource-broker into master 2026-09-23 20:36:21 +08:00
11 changed files with 2204 additions and 249 deletions
Showing only changes of commit 3a560f149e - Show all commits

View File

@ -6,7 +6,6 @@ pub struct ApiKey {
pub id: String, pub id: String,
pub name: String, pub name: String,
pub provider: String, pub provider: String,
pub api_key: String,
pub base_url: Option<String>, pub base_url: Option<String>,
pub model: Option<String>, pub model: Option<String>,
pub notes: Option<String>, pub notes: Option<String>,
@ -29,17 +28,15 @@ fn row_to_api_key(row: &rusqlite::Row<'_>) -> rusqlite::Result<ApiKey> {
id: row.get(0)?, id: row.get(0)?,
name: row.get(1)?, name: row.get(1)?,
provider: row.get(2)?, provider: row.get(2)?,
api_key: row.get(3)?, base_url: row.get(3)?,
base_url: row.get(4)?, model: row.get(4)?,
model: row.get(5)?, notes: row.get(5)?,
notes: row.get(6)?, created_at: row.get(6)?,
created_at: row.get(7)?, updated_at: row.get(7)?,
updated_at: row.get(8)?,
}) })
} }
const SELECT_COLS: &str = const SELECT_COLS: &str = "id, name, provider, base_url, model, notes, created_at, updated_at";
"id, name, provider, api_key, base_url, model, notes, created_at, updated_at";
pub fn list_api_keys(conn: &Connection) -> Vec<ApiKey> { pub fn list_api_keys(conn: &Connection) -> Vec<ApiKey> {
let sql = format!("SELECT {SELECT_COLS} FROM api_keys ORDER BY updated_at DESC"); let sql = format!("SELECT {SELECT_COLS} FROM api_keys ORDER BY updated_at DESC");
@ -52,23 +49,34 @@ pub fn list_api_keys(conn: &Connection) -> Vec<ApiKey> {
.unwrap_or_default() .unwrap_or_default()
} }
pub fn create_api_key(conn: &Connection, input: &SaveApiKeyInput) -> Result<ApiKey, String> { pub fn create_api_key(
conn: &Connection,
store: &impl crate::credential_store::CredentialStore,
input: &SaveApiKeyInput,
) -> Result<ApiKey, String> {
let id = format!("ak-{}", uuid::Uuid::new_v4()); let id = format!("ak-{}", uuid::Uuid::new_v4());
conn.execute( let reference = crate::credential_store::versioned_reference(
"INSERT INTO api_keys (id, name, provider, api_key, base_url, model, notes) crate::credential_store::LegacyCredentialKind::ApiKey,
VALUES (?1,?2,?3,?4,?5,?6,?7)", &id,
params![ );
id, crate::credential_store::put_verified(store, &reference, input.api_key.as_bytes())?;
input.name, let result = (|| {
input.provider, let tx = conn
input.api_key, .unchecked_transaction()
input.base_url, .map_err(|_| "创建 API Key 失败".to_string())?;
input.model, tx.execute(
input.notes "INSERT INTO api_keys (id, name, provider, api_key, credential_ref, base_url, model, notes)
], VALUES (?1,?2,?3,'',?4,?5,?6,?7)",
) params![id, input.name, input.provider, reference, input.base_url, input.model, input.notes],
.map_err(|e| e.to_string())?; ).map_err(|_| "创建 API Key 失败".to_string())?;
get_api_key(conn, &id).ok_or_else(|| "创建后查询失败".into()) let key = get_api_key(&tx, &id).ok_or_else(|| "创建后查询失败".to_string())?;
tx.commit().map_err(|_| "创建 API Key 失败".to_string())?;
Ok(key)
})();
if result.is_err() {
let _ = store.delete(&reference);
}
result
} }
pub fn get_api_key(conn: &Connection, id: &str) -> Option<ApiKey> { pub fn get_api_key(conn: &Connection, id: &str) -> Option<ApiKey> {
@ -78,24 +86,63 @@ pub fn get_api_key(conn: &Connection, id: &str) -> Option<ApiKey> {
pub fn update_api_key( pub fn update_api_key(
conn: &Connection, conn: &Connection,
store: &impl crate::credential_store::CredentialStore,
id: &str, id: &str,
input: &SaveApiKeyInput, input: &SaveApiKeyInput,
) -> Result<ApiKey, String> { ) -> Result<ApiKey, String> {
let rows = conn.execute( let old_reference: Option<String> = conn
"UPDATE api_keys SET name=?1, provider=?2, api_key=?3, base_url=?4, model=?5, notes=?6, updated_at=datetime('now') .query_row(
WHERE id=?7", "SELECT credential_ref FROM api_keys WHERE id = ?1",
params![input.name, input.provider, input.api_key, input.base_url, input.model, input.notes, id], params![id],
|row| row.get(0),
) )
.map_err(|e| e.to_string())?; .map_err(|_| "API Key 不存在".to_string())?;
if rows == 0 { let reference = crate::credential_store::versioned_reference(
return Err(format!("API Key {} 不存在", id)); crate::credential_store::LegacyCredentialKind::ApiKey,
id,
);
crate::credential_store::put_verified(store, &reference, input.api_key.as_bytes())?;
let result = (|| {
let tx = conn
.unchecked_transaction()
.map_err(|_| "更新 API Key 失败".to_string())?;
let rows = tx.execute(
"UPDATE api_keys SET name=?1, provider=?2, api_key='', credential_ref=?3, base_url=?4, model=?5, notes=?6, updated_at=datetime('now')
WHERE id=?7 AND credential_ref IS ?8",
params![input.name, input.provider, reference, input.base_url, input.model, input.notes, id, old_reference],
).map_err(|_| "更新 API Key 失败".to_string())?;
if rows != 1 {
return Err("更新 API Key 失败:凭证引用已变化".into());
} }
get_api_key(conn, id).ok_or_else(|| "更新后查询失败".into()) let key = get_api_key(&tx, id).ok_or_else(|| "更新后查询失败".to_string())?;
tx.commit().map_err(|_| "更新 API Key 失败".to_string())?;
Ok(key)
})();
if result.is_err() {
let _ = store.delete(&reference);
} else if let Some(old) = old_reference {
let _ = store.delete(&old);
}
result
} }
pub fn delete_api_key_fn(conn: &Connection, id: &str) -> Result<(), String> { pub fn delete_api_key_fn(
conn: &Connection,
store: &impl crate::credential_store::CredentialStore,
id: &str,
) -> Result<(), String> {
let reference: Option<String> = conn
.query_row(
"SELECT credential_ref FROM api_keys WHERE id=?1",
params![id],
|row| row.get(0),
)
.map_err(|_| "API Key 不存在".to_string())?;
conn.execute("DELETE FROM api_keys WHERE id=?1", params![id]) conn.execute("DELETE FROM api_keys WHERE id=?1", params![id])
.map_err(|e| e.to_string())?; .map_err(|_| "删除 API Key 失败".to_string())?;
if let Some(reference) = reference {
let _ = store.delete(&reference);
}
Ok(()) Ok(())
} }
@ -110,17 +157,148 @@ pub fn get_api_keys() -> Result<Vec<ApiKey>, String> {
#[tauri::command] #[tauri::command]
pub fn add_api_key(input: SaveApiKeyInput) -> Result<ApiKey, String> { pub fn add_api_key(input: SaveApiKeyInput) -> Result<ApiKey, String> {
let conn = crate::db::pool().get().map_err(|e| e.to_string())?; let conn = crate::db::pool().get().map_err(|e| e.to_string())?;
create_api_key(&conn, &input) create_api_key(
&conn,
&crate::credential_store::WindowsCredentialStore,
&input,
)
} }
#[tauri::command] #[tauri::command]
pub fn edit_api_key(id: String, input: SaveApiKeyInput) -> Result<ApiKey, String> { pub fn edit_api_key(id: String, input: SaveApiKeyInput) -> Result<ApiKey, String> {
let conn = crate::db::pool().get().map_err(|e| e.to_string())?; let conn = crate::db::pool().get().map_err(|e| e.to_string())?;
update_api_key(&conn, &id, &input) update_api_key(
&conn,
&crate::credential_store::WindowsCredentialStore,
&id,
&input,
)
} }
#[tauri::command] #[tauri::command]
pub fn delete_api_key(id: String) -> Result<(), String> { pub fn delete_api_key(id: String) -> Result<(), String> {
let conn = crate::db::pool().get().map_err(|e| e.to_string())?; let conn = crate::db::pool().get().map_err(|e| e.to_string())?;
delete_api_key_fn(&conn, &id) delete_api_key_fn(&conn, &crate::credential_store::WindowsCredentialStore, &id)
}
pub fn reveal_api_key_for_conn(
conn: &Connection,
store: &impl crate::credential_store::CredentialStore,
id: &str,
) -> Result<String, String> {
let bytes = crate::credential_store::get_or_migrate_legacy(
conn,
store,
crate::credential_store::LegacyCredentialKind::ApiKey,
id,
)?;
String::from_utf8(bytes).map_err(|_| "凭证状态 needs_repair:API Key 编码无效".to_string())
}
/// 仅供本机 UI 在用户显式请求查看或复制时调用;MCP 不公开此 command。
#[tauri::command]
pub fn reveal_api_key(id: String) -> Result<String, String> {
let conn = crate::db::pool().get().map_err(|e| e.to_string())?;
reveal_api_key_for_conn(&conn, &crate::credential_store::WindowsCredentialStore, &id)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::credential_store::CredentialStore;
#[test]
fn list_hides_key_and_explicit_reveal_requires_existing_id() {
let conn = crate::db::conn_with_schema();
let store = crate::credential_store::MemoryCredentialStore::new();
conn.execute("INSERT INTO api_keys (id, name, provider, api_key) VALUES ('k1', 'key', 'test', 'secret')", []).unwrap();
let listed = list_api_keys(&conn);
assert!(serde_json::to_string(&listed).unwrap().contains("secret") == false);
assert_eq!(
reveal_api_key_for_conn(&conn, &store, "k1").unwrap(),
"secret"
);
assert!(reveal_api_key_for_conn(&conn, &store, "missing").is_err());
let (legacy, reference): (String, String) = conn
.query_row(
"SELECT api_key, credential_ref FROM api_keys WHERE id='k1'",
[],
|row| Ok((row.get(0)?, row.get(1)?)),
)
.unwrap();
assert!(legacy.is_empty());
assert!(store.get(&reference).unwrap().is_some());
}
#[test]
fn api_key_write_failure_preserves_legacy_and_metadata() {
let conn = crate::db::conn_with_schema();
conn.execute("INSERT INTO api_keys (id, name, provider, api_key) VALUES ('k1', 'old', 'test', 'synthetic-old-key')", []).unwrap();
let failing = crate::credential_store::MemoryCredentialStore::failing();
let input = SaveApiKeyInput {
name: "new".into(),
provider: "test".into(),
api_key: "synthetic-new-key".into(),
base_url: None,
model: None,
notes: None,
};
let error = update_api_key(&conn, &failing, "k1", &input).unwrap_err();
assert!(error.contains("needs_repair"));
assert!(!error.contains("synthetic-new-key"));
let (name, old, reference): (String, String, Option<String>) = conn
.query_row(
"SELECT name, api_key, credential_ref FROM api_keys WHERE id='k1'",
[],
|row| Ok((row.get(0)?, row.get(1)?, row.get(2)?)),
)
.unwrap();
assert_eq!(name, "old");
assert_eq!(old, "synthetic-old-key");
assert!(reference.is_none());
let store = crate::credential_store::MemoryCredentialStore::new();
update_api_key(&conn, &store, "k1", &input).unwrap();
let listed = list_api_keys(&conn);
assert!(!serde_json::to_string(&listed)
.unwrap()
.contains("synthetic-new-key"));
assert_eq!(
reveal_api_key_for_conn(&conn, &store, "k1").unwrap(),
"synthetic-new-key"
);
}
#[test]
fn new_api_key_keeps_only_reference_in_database_and_delete_cleans_store() {
let conn = crate::db::conn_with_schema();
let store = crate::credential_store::MemoryCredentialStore::new();
let input = SaveApiKeyInput {
name: "test".into(),
provider: "test".into(),
api_key: "synthetic-new-key".into(),
base_url: None,
model: None,
notes: None,
};
let key = create_api_key(&conn, &store, &input).unwrap();
let (legacy, reference): (String, String) = conn
.query_row(
"SELECT api_key, credential_ref FROM api_keys WHERE id=?1",
params![key.id],
|row| Ok((row.get(0)?, row.get(1)?)),
)
.unwrap();
assert!(legacy.is_empty());
assert_eq!(
store.get(&reference).unwrap().as_deref(),
Some(b"synthetic-new-key".as_slice())
);
assert_eq!(
reveal_api_key_for_conn(&conn, &store, &key.id).unwrap(),
"synthetic-new-key"
);
delete_api_key_fn(&conn, &store, &key.id).unwrap();
assert!(store.get(&reference).unwrap().is_none());
}
} }

View File

@ -15,22 +15,69 @@ pub struct GitStatus {
/// 按 URL 前缀匹配已注册的 Gitea 实例,返回其 access_token /// 按 URL 前缀匹配已注册的 Gitea 实例,返回其 access_token
fn gitea_token_for_url(url: &str) -> Option<String> { fn gitea_token_for_url(url: &str) -> Option<String> {
let conn = db::pool().get().ok()?; let conn = db::pool().get().ok()?;
let mut stmt = conn let instances: Vec<(String, String)> = {
.prepare("SELECT url, access_token FROM gitea_instances") let mut stmt = conn.prepare("SELECT id, url FROM gitea_instances").ok()?;
.ok()?;
let rows = stmt let rows = stmt
.query_map([], |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?))) .query_map([], |r| Ok((r.get(0)?, r.get(1)?)))
.ok()?; .ok()?
for row in rows.flatten() { .filter_map(Result::ok)
let (base, token) = row; .collect();
let base = base.trim_end_matches('/'); rows
if !base.is_empty() && url.starts_with(base) { };
return Some(token); for (instance_id, base) in instances {
if gitea_url_matches(&base, url) {
return crate::commands::gitea::instance_auth(&conn, &instance_id)
.ok()
.map(|(_, token)| token);
} }
} }
None None
} }
fn gitea_url_matches(base: &str, remote: &str) -> bool {
let (Ok(base), Ok(remote)) = (tauri::Url::parse(base), tauri::Url::parse(remote)) else {
return false;
};
if !matches!(base.scheme(), "http" | "https")
|| base.scheme() != remote.scheme()
|| base.host_str() != remote.host_str()
|| base.port_or_known_default() != remote.port_or_known_default()
{
return false;
}
let prefix = base.path().trim_end_matches('/');
prefix.is_empty() || remote.path() == prefix || remote.path().starts_with(&format!("{prefix}/"))
}
#[cfg(test)]
mod credential_match_tests {
use super::gitea_url_matches;
#[test]
fn gitea_token_url_match_requires_same_origin_and_path_boundary() {
assert!(gitea_url_matches(
"https://gitea.example.com",
"https://gitea.example.com/o/r.git"
));
assert!(gitea_url_matches(
"https://gitea.example.com/git",
"https://gitea.example.com/git/o/r.git"
));
assert!(!gitea_url_matches(
"https://gitea.example.com",
"https://gitea.example.com.evil/o/r.git"
));
assert!(!gitea_url_matches(
"https://gitea.example.com/git",
"https://gitea.example.com/github/o/r.git"
));
assert!(!gitea_url_matches(
"https://gitea.example.com",
"http://gitea.example.com/o/r.git"
));
}
}
/// 凭据链:ssh agent → git credential helper(与 CLI 同一凭据源)→ Gitea 实例 token → default /// 凭据链:ssh agent → git credential helper(与 CLI 同一凭据源)→ Gitea 实例 token → default
/// 不再依赖 GitHub 登录,任何 remote(Gitea / GitHub / 其他)走系统已有凭据 /// 不再依赖 GitHub 登录,任何 remote(Gitea / GitHub / 其他)走系统已有凭据
fn make_callbacks() -> RemoteCallbacks<'static> { fn make_callbacks() -> RemoteCallbacks<'static> {

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,674 @@
//! 本机用户级凭证存储。生产 Windows 构建使用 Credential Manager;其他平台 fail closed。
#[cfg(test)]
use std::collections::HashMap;
#[cfg(test)]
use std::sync::Mutex;
fn ensure_legacy_backup(
conn: &rusqlite::Connection,
table: &str,
id_column: &str,
secret_column: &str,
id: &str,
legacy: &str,
) -> Result<(), String> {
let database_path: String = conn
.query_row("PRAGMA database_list", [], |row| row.get(2))
.map_err(|_| "凭证状态 needs_repair:无法确定数据库备份位置".to_string())?;
if database_path.is_empty() {
// 仅测试用的内存数据库无需磁盘备份。
return Ok(());
}
let path = std::path::Path::new(&database_path);
let backup = path.with_extension("pre-credential-migration.db");
if !backup.exists() {
conn.execute("VACUUM main INTO ?1", [backup.to_string_lossy().as_ref()])
.map_err(|_| "凭证状态 needs_repair:迁移前备份失败".to_string())?;
}
let backup_conn =
rusqlite::Connection::open_with_flags(&backup, rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY)
.map_err(|_| "凭证状态 needs_repair:迁移前备份不可读取".to_string())?;
let check: String = backup_conn
.query_row("PRAGMA integrity_check", [], |row| row.get(0))
.map_err(|_| "凭证状态 needs_repair:迁移前备份校验失败".to_string())?;
if check != "ok" {
return Err("凭证状态 needs_repair:迁移前备份完整性检查失败".into());
}
// 防止恢复过旧数据库后误把一份不含当前旧值的备份当作可恢复证据。
let check_sql = format!(
"SELECT EXISTS(SELECT 1 FROM {table} WHERE {id_column} = ?1 AND {secret_column} = ?2)"
);
let contains_value: bool = backup_conn
.query_row(&check_sql, rusqlite::params![id, legacy], |row| row.get(0))
.map_err(|_| "凭证状态 needs_repair:迁移前备份内容无法验证".to_string())?;
if !contains_value {
return Err("凭证状态 needs_repair:现有备份不含当前旧值;请先安排新的可恢复备份".into());
}
Ok(())
}
pub trait CredentialStore {
fn put(&self, reference: &str, value: &[u8]) -> Result<(), String>;
fn get(&self, reference: &str) -> Result<Option<Vec<u8>>, String>;
fn delete(&self, reference: &str) -> Result<(), String>;
}
#[derive(Debug, Clone, Copy)]
pub enum LegacyCredentialKind {
GiteaAccessToken,
GiteaWebhookSecret,
ApiKey,
}
pub fn reference_for(kind: LegacyCredentialKind, id: &str) -> String {
match kind {
LegacyCredentialKind::GiteaAccessToken => format!("gitea/access-token/{id}"),
LegacyCredentialKind::GiteaWebhookSecret => format!("gitea/webhook-secret/{id}"),
LegacyCredentialKind::ApiKey => format!("api-key/{id}"),
}
}
pub fn versioned_reference(kind: LegacyCredentialKind, id: &str) -> String {
format!("{}/{}", reference_for(kind, id), uuid::Uuid::new_v4())
}
pub fn put_verified(
store: &impl CredentialStore,
reference: &str,
value: &[u8],
) -> Result<(), String> {
store
.put(reference, value)
.map_err(|_| "凭证状态 needs_repair:受保护存储写入失败".to_string())?;
let verified = store.get(reference);
match verified {
Ok(Some(bytes)) if bytes == value => Ok(()),
other => {
let _ = store.delete(reference);
match other {
Err(_) => Err("凭证状态 needs_repair:写入后读取校验失败".into()),
Ok(None) => Err("凭证状态 needs_repair:写入后无法读取校验".into()),
Ok(Some(_)) => Err("凭证状态 needs_repair:写入后校验不匹配".into()),
}
}
}
}
impl LegacyCredentialKind {
fn columns(self) -> (&'static str, &'static str, &'static str, &'static str, bool) {
match self {
Self::GiteaAccessToken => (
"gitea_instances",
"id",
"access_token",
"access_token_ref",
false,
),
Self::GiteaWebhookSecret => (
"gitea_repos",
"project_id",
"webhook_secret",
"webhook_secret_ref",
true,
),
Self::ApiKey => ("api_keys", "id", "api_key", "credential_ref", false),
}
}
}
/// 读取已迁移凭证,或以 write→verify→transactional switch→clear 顺序升级旧记录。
/// 仅接受枚举指定的表/列;错误文本固定为非秘密描述。
pub fn get_or_migrate_legacy(
conn: &rusqlite::Connection,
store: &impl CredentialStore,
kind: LegacyCredentialKind,
id: &str,
) -> Result<Vec<u8>, String> {
let (table, id_column, secret_column, reference_column, nullable) = kind.columns();
let query =
format!("SELECT {secret_column}, {reference_column} FROM {table} WHERE {id_column} = ?1");
let (legacy, reference): (Option<String>, Option<String>) = conn
.query_row(&query, [id], |row| Ok((row.get(0)?, row.get(1)?)))
.map_err(|_| "凭证记录不存在或无法读取".to_string())?;
if let Some(reference) = reference.filter(|value| !value.is_empty()) {
let value = store
.get(&reference)
.map_err(|_| "凭证状态 needs_repair:受保护存储不可用".to_string())?
.ok_or_else(|| "凭证状态 needs_repair:受保护存储中缺少引用值".to_string())?;
return if value.is_empty() {
Err("凭证状态 needs_repair:受保护存储中的凭证为空".into())
} else {
Ok(value)
};
}
let legacy = legacy
.filter(|value| !value.is_empty())
.ok_or_else(|| "凭证状态 not_configured:没有可迁移的凭证".to_string())?;
ensure_legacy_backup(conn, table, id_column, secret_column, id, &legacy)?;
let reference = versioned_reference(kind, id);
let bytes = legacy.as_bytes();
put_verified(store, &reference, bytes)?;
let result = (|| {
let tx = conn
.unchecked_transaction()
.map_err(|_| "凭证状态 needs_repair:数据库切换失败".to_string())?;
let clear = if nullable { "NULL" } else { "''" };
let update = format!(
"UPDATE {table} SET {reference_column} = ?1, {secret_column} = {clear} \
WHERE {id_column} = ?2 AND {secret_column} = ?3 AND \
({reference_column} IS NULL OR {reference_column} = '')"
);
let changed = tx
.execute(&update, rusqlite::params![reference, id, legacy])
.map_err(|_| "凭证状态 needs_repair:数据库切换失败".to_string())?;
if changed != 1 {
return Err("凭证状态 needs_repair:记录在迁移期间发生变化".into());
}
tx.commit()
.map_err(|_| "凭证状态 needs_repair:数据库切换失败".to_string())
})();
if result.is_err() {
let _ = store.delete(&reference);
}
result?;
Ok(bytes.to_vec())
}
#[cfg(windows)]
#[derive(Debug, Default, Clone, Copy)]
pub struct WindowsCredentialStore;
#[cfg(windows)]
impl CredentialStore for WindowsCredentialStore {
fn put(&self, reference: &str, value: &[u8]) -> Result<(), String> {
windows_store::put(reference, value)
}
fn get(&self, reference: &str) -> Result<Option<Vec<u8>>, String> {
windows_store::get(reference)
}
fn delete(&self, reference: &str) -> Result<(), String> {
windows_store::delete(reference)
}
}
#[cfg(not(windows))]
#[derive(Debug, Default, Clone, Copy)]
pub struct WindowsCredentialStore;
#[cfg(not(windows))]
impl CredentialStore for WindowsCredentialStore {
fn put(&self, _reference: &str, _value: &[u8]) -> Result<(), String> {
Err("Windows Credential Manager 不可用".into())
}
fn get(&self, _reference: &str) -> Result<Option<Vec<u8>>, String> {
Err("Windows Credential Manager 不可用".into())
}
fn delete(&self, _reference: &str) -> Result<(), String> {
Err("Windows Credential Manager 不可用".into())
}
}
#[cfg(windows)]
mod windows_store {
use std::ffi::c_void;
use std::ptr::null_mut;
const CRED_TYPE_GENERIC: u32 = 1;
const CRED_PERSIST_LOCAL_MACHINE: u32 = 2;
const CRED_MAX_CREDENTIAL_BLOB_SIZE: usize = 5 * 512;
const ERROR_NOT_FOUND: i32 = 1168;
#[repr(C)]
struct FileTime {
low: u32,
high: u32,
}
#[repr(C)]
struct CredentialAttributeW {
keyword: *mut u16,
flags: u32,
value_size: u32,
value: *mut u8,
}
#[repr(C)]
struct CredentialW {
flags: u32,
kind: u32,
target_name: *mut u16,
comment: *mut u16,
last_written: FileTime,
credential_blob_size: u32,
credential_blob: *mut u8,
persist: u32,
attribute_count: u32,
attributes: *mut CredentialAttributeW,
target_alias: *mut u16,
user_name: *mut u16,
}
#[link(name = "Advapi32")]
unsafe extern "system" {
fn CredWriteW(credential: *const CredentialW, flags: u32) -> i32;
fn CredReadW(
target: *const u16,
kind: u32,
flags: u32,
output: *mut *mut CredentialW,
) -> i32;
fn CredDeleteW(target: *const u16, kind: u32, flags: u32) -> i32;
fn CredFree(buffer: *mut c_void);
}
fn target(reference: &str) -> Result<Vec<u16>, String> {
if reference.is_empty() || reference.len() > 240 || reference.chars().any(char::is_control)
{
return Err("凭证引用格式无效".into());
}
Ok(format!("dev-manager-tauri/{reference}")
.encode_utf16()
.chain(Some(0))
.collect())
}
pub fn put(reference: &str, value: &[u8]) -> Result<(), String> {
if value.is_empty() || value.len() > CRED_MAX_CREDENTIAL_BLOB_SIZE {
return Err("凭证值长度超出 Credential Manager 限制".into());
}
let target = target(reference)?;
let mut blob = value.to_vec();
let credential = CredentialW {
flags: 0,
kind: CRED_TYPE_GENERIC,
target_name: target.as_ptr() as *mut u16,
comment: null_mut(),
last_written: FileTime { low: 0, high: 0 },
credential_blob_size: blob.len() as u32,
credential_blob: blob.as_mut_ptr(),
persist: CRED_PERSIST_LOCAL_MACHINE,
attribute_count: 0,
attributes: null_mut(),
target_alias: null_mut(),
user_name: null_mut(),
};
let ok = unsafe { CredWriteW(&credential, 0) };
blob.fill(0);
if ok == 0 {
Err("Credential Manager 写入失败".into())
} else {
Ok(())
}
}
pub fn get(reference: &str) -> Result<Option<Vec<u8>>, String> {
let target = target(reference)?;
let mut raw = null_mut();
let ok = unsafe { CredReadW(target.as_ptr(), CRED_TYPE_GENERIC, 0, &mut raw) };
if ok == 0 {
let error = std::io::Error::last_os_error();
return if error.raw_os_error() == Some(ERROR_NOT_FOUND) {
Ok(None)
} else {
Err("Credential Manager 读取失败".into())
};
}
if raw.is_null() {
return Err("Credential Manager 返回无效凭证".into());
}
let credential = unsafe { &*raw };
let result = if credential.credential_blob_size as usize > CRED_MAX_CREDENTIAL_BLOB_SIZE {
Err("Credential Manager 凭证长度无效".into())
} else if credential.credential_blob_size > 0 && credential.credential_blob.is_null() {
Err("Credential Manager 返回无效凭证".into())
} else if credential.credential_blob_size == 0 {
Ok(Some(Vec::new()))
} else {
let bytes = unsafe {
std::slice::from_raw_parts(
credential.credential_blob,
credential.credential_blob_size as usize,
)
};
Ok(Some(bytes.to_vec()))
};
unsafe {
CredFree(raw.cast());
}
result
}
pub fn delete(reference: &str) -> Result<(), String> {
let target = target(reference)?;
let ok = unsafe { CredDeleteW(target.as_ptr(), CRED_TYPE_GENERIC, 0) };
if ok != 0 {
return Ok(());
}
if std::io::Error::last_os_error().raw_os_error() == Some(ERROR_NOT_FOUND) {
Ok(())
} else {
Err("Credential Manager 删除失败".into())
}
}
}
#[cfg(test)]
pub(crate) struct MemoryCredentialStore {
values: Mutex<HashMap<String, Vec<u8>>>,
fail_writes: bool,
fail_reads: bool,
}
#[cfg(test)]
impl MemoryCredentialStore {
pub(crate) fn new() -> Self {
Self {
values: Mutex::new(HashMap::new()),
fail_writes: false,
fail_reads: false,
}
}
pub(crate) fn failing() -> Self {
Self {
values: Mutex::new(HashMap::new()),
fail_writes: true,
fail_reads: true,
}
}
pub(crate) fn contains_value(&self, value: &[u8]) -> bool {
self.values
.lock()
.map(|values| values.values().any(|stored| stored == value))
.unwrap_or(false)
}
}
#[cfg(test)]
impl CredentialStore for MemoryCredentialStore {
fn put(&self, reference: &str, value: &[u8]) -> Result<(), String> {
if self.fail_writes {
return Err("模拟凭证存储写入失败".into());
}
self.values
.lock()
.map_err(|_| "凭证存储锁不可用".to_string())?
.insert(reference.into(), value.into());
Ok(())
}
fn get(&self, reference: &str) -> Result<Option<Vec<u8>>, String> {
if self.fail_reads {
return Err("模拟凭证存储读取失败".into());
}
Ok(self
.values
.lock()
.map_err(|_| "凭证存储锁不可用".to_string())?
.get(reference)
.cloned())
}
fn delete(&self, reference: &str) -> Result<(), String> {
self.values
.lock()
.map_err(|_| "凭证存储锁不可用".to_string())?
.remove(reference);
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::{
get_or_migrate_legacy, CredentialStore, LegacyCredentialKind, MemoryCredentialStore,
};
use rusqlite::Connection;
fn migration_db() -> Connection {
let conn = crate::db::conn_with_schema();
conn.execute("INSERT INTO gitea_instances (id, name, url, access_token) VALUES ('instance-test', 'fake', 'https://example.invalid', 'synthetic-test-token')", []).unwrap();
conn.execute(
"INSERT INTO projects (id, name) VALUES ('project-test', 'fake')",
[],
)
.unwrap();
conn.execute("INSERT INTO gitea_repos (id, instance_id, project_id, gitea_owner, gitea_repo_name, webhook_secret) VALUES ('repo-test', 'instance-test', 'project-test', 'owner', 'repo', 'synthetic-webhook-secret')", []).unwrap();
conn.execute("INSERT INTO api_keys (id, name, provider, api_key) VALUES ('key-test', 'fake', 'test', 'synthetic-api-key')", []).unwrap();
conn
}
#[test]
fn memory_adapter_supports_put_replace_get_delete_and_failures() {
let store = MemoryCredentialStore::new();
store.put("test/ref", b"first").unwrap();
assert_eq!(
store.get("test/ref").unwrap().as_deref(),
Some(b"first".as_slice())
);
store.put("test/ref", b"second").unwrap();
assert_eq!(
store.get("test/ref").unwrap().as_deref(),
Some(b"second".as_slice())
);
store.delete("test/ref").unwrap();
assert_eq!(store.get("test/ref").unwrap(), None);
let failing = MemoryCredentialStore::failing();
let error = failing.put("test/ref", b"do-not-print-this").unwrap_err();
assert!(!error.contains("do-not-print-this"));
}
#[test]
fn legacy_migration_verifies_then_switches_reference_and_clears_old_value() {
let conn = migration_db();
let store = MemoryCredentialStore::new();
let value = get_or_migrate_legacy(
&conn,
&store,
LegacyCredentialKind::GiteaAccessToken,
"instance-test",
)
.unwrap();
assert_eq!(value, b"synthetic-test-token");
let (legacy, reference): (String, String) = conn.query_row("SELECT access_token, access_token_ref FROM gitea_instances WHERE id='instance-test'", [], |row| Ok((row.get(0)?, row.get(1)?))).unwrap();
assert!(legacy.is_empty());
assert_eq!(
store.get(&reference).unwrap().as_deref(),
Some(b"synthetic-test-token".as_slice())
);
let webhook = get_or_migrate_legacy(
&conn,
&store,
LegacyCredentialKind::GiteaWebhookSecret,
"project-test",
)
.unwrap();
assert_eq!(webhook, b"synthetic-webhook-secret");
let old_webhook: Option<String> = conn
.query_row(
"SELECT webhook_secret FROM gitea_repos WHERE project_id='project-test'",
[],
|row| row.get(0),
)
.unwrap();
assert!(old_webhook.is_none());
let key =
get_or_migrate_legacy(&conn, &store, LegacyCredentialKind::ApiKey, "key-test").unwrap();
assert_eq!(key, b"synthetic-api-key");
}
#[test]
fn failed_legacy_migration_keeps_old_value_and_returns_repair_state_without_secret() {
let conn = migration_db();
let store = MemoryCredentialStore::failing();
let error = get_or_migrate_legacy(
&conn,
&store,
LegacyCredentialKind::GiteaAccessToken,
"instance-test",
)
.unwrap_err();
assert!(error.contains("needs_repair"));
assert!(!error.contains("synthetic-test-token"));
let (legacy, reference): (String, Option<String>) = conn.query_row("SELECT access_token, access_token_ref FROM gitea_instances WHERE id='instance-test'", [], |row| Ok((row.get(0)?, row.get(1)?))).unwrap();
assert_eq!(legacy, "synthetic-test-token");
assert!(reference.is_none());
}
#[test]
fn migration_database_failure_keeps_legacy_secret_and_allows_retry() {
let conn = migration_db();
let store = MemoryCredentialStore::new();
conn.execute_batch("CREATE TRIGGER fail_credential_switch BEFORE UPDATE OF access_token_ref ON gitea_instances BEGIN SELECT RAISE(ABORT, 'injected database failure'); END;").unwrap();
let error = get_or_migrate_legacy(
&conn,
&store,
LegacyCredentialKind::GiteaAccessToken,
"instance-test",
)
.unwrap_err();
assert!(error.contains("needs_repair"));
let (legacy, reference): (String, Option<String>) = conn.query_row("SELECT access_token, access_token_ref FROM gitea_instances WHERE id='instance-test'", [], |row| Ok((row.get(0)?, row.get(1)?))).unwrap();
assert_eq!(legacy, "synthetic-test-token");
assert!(reference.is_none());
conn.execute_batch("DROP TRIGGER fail_credential_switch;")
.unwrap();
assert_eq!(
get_or_migrate_legacy(
&conn,
&store,
LegacyCredentialKind::GiteaAccessToken,
"instance-test"
)
.unwrap(),
b"synthetic-test-token"
);
let (legacy, reference): (String, String) = conn.query_row("SELECT access_token, access_token_ref FROM gitea_instances WHERE id='instance-test'", [], |row| Ok((row.get(0)?, row.get(1)?))).unwrap();
assert!(legacy.is_empty());
assert_eq!(
store.get(&reference).unwrap().as_deref(),
Some(b"synthetic-test-token".as_slice())
);
}
#[test]
fn disk_migration_requires_verified_backup_and_backup_restores_old_value() {
let directory = std::env::temp_dir().join(format!(
"lian-jing-credential-backup-test-{}",
uuid::Uuid::new_v4()
));
std::fs::create_dir(&directory).unwrap();
let path = directory.join("synthetic.db");
let backup = path.with_extension("pre-credential-migration.db");
let conn = Connection::open(&path).unwrap();
conn.execute_batch("CREATE TABLE api_keys (id TEXT PRIMARY KEY, api_key TEXT NOT NULL, credential_ref TEXT);").unwrap();
conn.execute(
"INSERT INTO api_keys (id, api_key) VALUES ('k', 'synthetic-backup-key')",
[],
)
.unwrap();
let store = MemoryCredentialStore::new();
std::fs::create_dir(&backup).unwrap();
let error =
get_or_migrate_legacy(&conn, &store, LegacyCredentialKind::ApiKey, "k").unwrap_err();
assert!(error.contains("needs_repair"));
let old: String = conn
.query_row("SELECT api_key FROM api_keys WHERE id='k'", [], |row| {
row.get(0)
})
.unwrap();
assert_eq!(old, "synthetic-backup-key");
std::fs::remove_dir(&backup).unwrap();
assert_eq!(
get_or_migrate_legacy(&conn, &store, LegacyCredentialKind::ApiKey, "k").unwrap(),
b"synthetic-backup-key"
);
let live: String = conn
.query_row("SELECT api_key FROM api_keys WHERE id='k'", [], |row| {
row.get(0)
})
.unwrap();
assert!(live.is_empty());
let restored =
Connection::open_with_flags(&backup, rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY)
.unwrap();
let backup_value: String = restored
.query_row("SELECT api_key FROM api_keys WHERE id='k'", [], |row| {
row.get(0)
})
.unwrap();
assert_eq!(backup_value, "synthetic-backup-key");
conn.execute(
"INSERT INTO api_keys (id, api_key) VALUES ('later', 'synthetic-later-key')",
[],
)
.unwrap();
let stale = get_or_migrate_legacy(&conn, &store, LegacyCredentialKind::ApiKey, "later")
.unwrap_err();
assert!(stale.contains("needs_repair"));
let still_legacy: String = conn
.query_row("SELECT api_key FROM api_keys WHERE id='later'", [], |row| {
row.get(0)
})
.unwrap();
assert_eq!(still_legacy, "synthetic-later-key");
drop(restored);
drop(conn);
std::fs::remove_file(&backup).unwrap();
std::fs::remove_file(&path).unwrap();
std::fs::remove_dir(&directory).unwrap();
}
}
#[cfg(all(test, windows))]
mod windows_tests {
use super::{windows_store, CredentialStore, WindowsCredentialStore};
use std::process::Command;
const CHILD_ENV: &str = "DEV_MANAGER_CREDENTIAL_STORE_CHILD_TEST";
#[test]
fn windows_credential_manager_persists_across_processes() {
let reference = format!("isolated-test/{}", uuid::Uuid::new_v4());
let value = format!("credential-store-test-{}", uuid::Uuid::new_v4()).into_bytes();
let store = WindowsCredentialStore;
store.put(&reference, &value).unwrap();
let child = Command::new(std::env::current_exe().unwrap())
.args([
"--exact",
"credential_store::windows_tests::credential_child_probe",
"--ignored",
"--nocapture",
])
.env(CHILD_ENV, &reference)
.env(
"DEV_MANAGER_CREDENTIAL_STORE_EXPECTED",
String::from_utf8(value.clone()).unwrap(),
)
.status();
let cleanup = store.delete(&reference);
assert!(cleanup.is_ok(), "测试凭证清理失败");
assert!(
child.is_ok_and(|status| status.success()),
"Credential Manager 子进程读取测试失败"
);
}
#[test]
#[ignore]
fn credential_child_probe() {
let Ok(reference) = std::env::var(CHILD_ENV) else {
return;
};
let expected = std::env::var("DEV_MANAGER_CREDENTIAL_STORE_EXPECTED").unwrap();
assert_eq!(
windows_store::get(&reference).unwrap().as_deref(),
Some(expected.as_bytes())
);
}
}

View File

@ -860,6 +860,7 @@ fn migrate(conn: &rusqlite::Connection) {
", ",
) )
.expect("create api_keys table"); .expect("create api_keys table");
let _ = conn.execute("ALTER TABLE api_keys ADD COLUMN credential_ref TEXT", []);
// 增量迁移:宿主机部署应用 // 增量迁移:宿主机部署应用
conn.execute_batch( conn.execute_batch(
@ -1048,6 +1049,33 @@ fn migrate(conn: &rusqlite::Connection) {
", ",
) )
.expect("create gitea tables"); .expect("create gitea tables");
let _ = conn.execute(
"ALTER TABLE gitea_instances ADD COLUMN access_token_ref TEXT",
[],
);
let _ = conn.execute(
"ALTER TABLE gitea_repos ADD COLUMN webhook_secret_ref TEXT",
[],
);
}
#[cfg(test)]
pub(crate) fn init_test_pool() {
if POOL.get().is_some() {
return;
}
let manager = SqliteConnectionManager::memory();
let pool = Pool::builder()
.max_size(1)
.build(manager)
.expect("create in-memory test pool");
{
let conn = pool.get().expect("get in-memory test connection");
conn.execute_batch("PRAGMA foreign_keys=ON;")
.expect("pragma");
migrate(&conn);
}
let _ = POOL.set(pool);
} }
#[cfg(test)] #[cfg(test)]

View File

@ -1,4 +1,5 @@
mod commands; mod commands;
mod credential_store;
mod db; mod db;
mod mcp_inject; mod mcp_inject;
mod mcp_server; mod mcp_server;
@ -390,6 +391,7 @@ pub fn run() {
commands::api_keys::add_api_key, commands::api_keys::add_api_key,
commands::api_keys::edit_api_key, commands::api_keys::edit_api_key,
commands::api_keys::delete_api_key, commands::api_keys::delete_api_key,
commands::api_keys::reveal_api_key,
// host apps // host apps
commands::host_apps::get_host_apps, commands::host_apps::get_host_apps,
commands::host_apps::add_host_app, commands::host_apps::add_host_app,
@ -406,6 +408,7 @@ pub fn run() {
commands::gitea::gitea_delete_instance, commands::gitea::gitea_delete_instance,
commands::gitea::gitea_update_instance, commands::gitea::gitea_update_instance,
commands::gitea::gitea_test_connection, commands::gitea::gitea_test_connection,
commands::gitea::get_project_resource_status,
commands::gitea::gitea_list_linked_repos, commands::gitea::gitea_list_linked_repos,
commands::gitea::gitea_get_repo_link, commands::gitea::gitea_get_repo_link,
commands::gitea::gitea_create_repo, commands::gitea::gitea_create_repo,

View File

@ -122,27 +122,7 @@ pub fn read_port() -> u16 {
/// 启动 MCP HTTP 服务(同时支持 Streamable HTTP 和 SSE 两种传输) /// 启动 MCP HTTP 服务(同时支持 Streamable HTTP 和 SSE 两种传输)
pub async fn start(port: u16) { pub async fn start(port: u16) {
let sessions: Sessions = Arc::new(Mutex::new(HashMap::new())); let sessions: Sessions = Arc::new(Mutex::new(HashMap::new()));
let app = mcp_router(sessions);
let app = Router::new()
.route("/health", get(health))
// 单一全局服务器路由(lian-jing):寻址不依赖 group_id,工具显式传 project_id
.route("/mcp", post(handle_mcp).get(handle_sse_connect))
.route(
"/mcp/projects/:project_id",
post(handle_scoped_mcp).get(handle_scoped_sse_connect),
)
// SSE 消息端点:客户端通过此 URL 发送 JSON-RPC,响应经 SSE 推回
.route("/mcp/messages", post(handle_sse_message))
.route(
"/mcp/projects/:project_id/messages",
post(handle_scoped_sse_message),
)
// Gitea webhook:接收 push/pull_request 事件写入飞轮
.route(
"/webhook/gitea/:project_id",
post(crate::mcp_webhook::handle_gitea_webhook),
)
.with_state(sessions);
let addr = mcp_bind_addr(port); let addr = mcp_bind_addr(port);
match tokio::net::TcpListener::bind(addr).await { match tokio::net::TcpListener::bind(addr).await {
@ -171,6 +151,29 @@ pub async fn start(port: u16) {
} }
} }
fn mcp_router(sessions: Sessions) -> Router {
Router::new()
.route("/health", get(health))
// 单一全局服务器路由(lian-jing):寻址不依赖 group_id,工具显式传 project_id
.route("/mcp", post(handle_mcp).get(handle_sse_connect))
.route(
"/mcp/projects/:project_id",
post(handle_scoped_mcp).get(handle_scoped_sse_connect),
)
// SSE 消息端点:客户端通过此 URL 发送 JSON-RPC,响应经 SSE 推回
.route("/mcp/messages", post(handle_sse_message))
.route(
"/mcp/projects/:project_id/messages",
post(handle_scoped_sse_message),
)
// Gitea webhook:接收 push/pull_request 事件写入飞轮
.route(
"/webhook/gitea/:project_id",
post(crate::mcp_webhook::handle_gitea_webhook),
)
.with_state(sessions)
}
async fn health() -> &'static str { async fn health() -> &'static str {
"炼境 MCP Server running" "炼境 MCP Server running"
} }
@ -366,6 +369,82 @@ mod tests {
assert!(!list.iter().any(|tool| tool["name"] == "unregister_project")); assert!(!list.iter().any(|tool| tool["name"] == "unregister_project"));
} }
#[tokio::test(flavor = "multi_thread")]
async fn scoped_enterprise_fixture_reads_non_secret_resource_status_over_mcp() {
db::init_test_pool();
let listener = tokio::net::TcpListener::bind(mcp_bind_addr(0))
.await
.unwrap();
let port = listener.local_addr().unwrap().port();
{
let conn = db::pool().get().unwrap();
conn.execute("INSERT OR IGNORE INTO project_profiles (id, name) VALUES ('enterprise-fixture-profile', 'Enterprise fixture')", []).unwrap();
conn.execute("INSERT OR IGNORE INTO project_workspaces (id, profile_id) VALUES ('enterprise-fixture', 'enterprise-fixture-profile')", []).unwrap();
conn.execute(
"UPDATE settings SET value=?1 WHERE key='mcp_port'",
[port.to_string()],
)
.unwrap();
}
let sessions: Sessions = Arc::new(Mutex::new(HashMap::new()));
let server = tokio::spawn(async move {
axum::serve(listener, mcp_router(sessions)).await.unwrap();
});
let url = format!("http://127.0.0.1:{port}/mcp/projects/enterprise-fixture");
let call = |method: &str, params: Value| -> Value {
ureq::post(&url)
.send_json(json!({"jsonrpc":"2.0","id":1,"method":method,"params":params}))
.unwrap()
.into_json()
.unwrap()
};
let init = call("initialize", json!({}));
assert_eq!(
init["result"]["lianJing"]["project_id"],
"enterprise-fixture"
);
let catalog = call("tools/list", json!({}));
assert!(catalog["result"]["tools"]
.as_array()
.unwrap()
.iter()
.any(|tool| tool["name"] == "get_project_resource_status"));
let result = call(
"tools/call",
json!({"name":"get_project_resource_status","arguments":{}}),
);
assert_eq!(
result["result"]["structuredContent"]["project_id"],
"enterprise-fixture"
);
assert_eq!(
result["result"]["structuredContent"]["gitea_pr"]["state"],
"not_configured"
);
assert!(!result.to_string().contains("access_token"));
let denied = call(
"tools/call",
json!({"name":"get_project_resource_status","arguments":{"project_id":"other-project"}}),
);
assert_eq!(
denied["result"]["structuredContent"]["code"],
"scope_violation"
);
use std::io::BufRead;
let sse = ureq::get(&url).call().unwrap();
assert!(sse
.header("Content-Type")
.unwrap_or("")
.contains("text/event-stream"));
let mut line = String::new();
std::io::BufReader::new(sse.into_reader())
.read_line(&mut line)
.unwrap();
assert!(line.contains("endpoint"));
server.abort();
}
#[tokio::test] #[tokio::test]
async fn readiness_probe_does_not_satisfy_client_reload_evidence() { async fn readiness_probe_does_not_satisfy_client_reload_evidence() {
let project_id = "reload-evidence-fixture"; let project_id = "reload-evidence-fixture";

View File

@ -134,6 +134,17 @@ pub fn tools_list_result() -> Value {
"required": ["project_id", "head", "base", "title"] "required": ["project_id", "head", "base", "title"]
} }
}, },
{
"name": "get_project_resource_status",
"description": "只读查询项目 Gitea PR 与 webhook readiness;仅返回状态和来源,不返回凭证、URL 或连接参数。",
"inputSchema": {
"type": "object",
"properties": {
"project_id": { "type": "string", "description": "项目 workspace ID" }
},
"required": ["project_id"]
}
},
{ {
"name": "get_project_snapshots", "name": "get_project_snapshots",
"description": "获取项目的历史健康快照(每日一条),包含 git 提交间隔、蓝图完成率、阻塞任务数、错误情况。用于分析趋势、识别高价值改进变量。", "description": "获取项目的历史健康快照(每日一条),包含 git 提交间隔、蓝图完成率、阻塞任务数、错误情况。用于分析趋势、识别高价值改进变量。",
@ -534,6 +545,14 @@ fn structured_tool_summary(tool_name: &str, value: &Value) -> String {
value["resolver"]["status"].as_str().unwrap_or("unknown"), value["resolver"]["status"].as_str().unwrap_or("unknown"),
value["conflicts"].as_array().map(Vec::len).unwrap_or(0) value["conflicts"].as_array().map(Vec::len).unwrap_or(0)
), ),
"get_project_resource_status" => format!(
"项目资源:project_id={},Gitea PR={},webhook={}",
value["project_id"].as_str().unwrap_or("?"),
value["gitea_pr"]["state"].as_str().unwrap_or("unknown"),
value["gitea_webhook"]["state"]
.as_str()
.unwrap_or("unknown")
),
"verify_onboarding" => value["summary"] "verify_onboarding" => value["summary"]
.as_str() .as_str()
.unwrap_or("接入验收结构化结果已返回") .unwrap_or("接入验收结构化结果已返回")
@ -546,6 +565,7 @@ fn tool_success_response(tool_name: &str, text: String) -> Value {
const STRUCTURED_TOOLS: &[&str] = &[ const STRUCTURED_TOOLS: &[&str] = &[
"get_delivery_status", "get_delivery_status",
"get_toolchain_status", "get_toolchain_status",
"get_project_resource_status",
"verify_onboarding", "verify_onboarding",
]; ];
if STRUCTURED_TOOLS.contains(&tool_name) { if STRUCTURED_TOOLS.contains(&tool_name) {
@ -669,6 +689,11 @@ async fn tools_call_with_scope(params: Option<&Value>, project_scope: Option<Str
let body = args.get("body").and_then(|v| v.as_str()).unwrap_or(""); let body = args.get("body").and_then(|v| v.as_str()).unwrap_or("");
crate::commands::gitea::gitea_api_create_pull_request(pid, head, base, title, body) crate::commands::gitea::gitea_api_create_pull_request(pid, head, base, title, body)
} }
"get_project_resource_status" => {
let pid = args.get("project_id").and_then(|v| v.as_str()).unwrap_or("");
crate::commands::gitea::get_project_resource_status(pid.to_string())
.and_then(|status| serde_json::to_string(&status).map_err(|_| "项目资源状态序列化失败".to_string()))
}
"poll_now" => crate::commands::gitea::poll_all_repos() "poll_now" => crate::commands::gitea::poll_all_repos()
.map(|(polled, ingested)| format!("轮询完成:{polled} 个仓库,新 commit {ingested} 条,PR 事件已同步")), .map(|(polled, ingested)| format!("轮询完成:{polled} 个仓库,新 commit {ingested} 条,PR 事件已同步")),
"setup_webhook" => { "setup_webhook" => {
@ -2042,10 +2067,30 @@ mod tests {
.iter() .iter()
.any(|tool| tool["name"] == "unregister_project")); .any(|tool| tool["name"] == "unregister_project"));
assert!(!tools.iter().any(|tool| tool["name"] == "list_all_projects")); assert!(!tools.iter().any(|tool| tool["name"] == "list_all_projects"));
let readiness = tools
.iter()
.find(|tool| tool["name"] == "get_project_resource_status")
.unwrap();
assert_eq!(
readiness["inputSchema"]["properties"]["project_id"]["const"],
"p1"
);
for forbidden in ["get_secret", "ssh_exec", "run_sql", "http_proxy", "deploy"] {
assert!(!tools.iter().any(|tool| tool["name"] == forbidden));
}
} }
#[tokio::test] #[tokio::test]
async fn scoped_calls_reject_cross_project_and_global_write_before_io() { async fn scoped_calls_reject_cross_project_and_global_write_before_io() {
let readiness_mismatch = tools_call_for_project(
Some(&json!({"name":"get_project_resource_status","arguments":{"project_id":"p2"}})),
"p1",
)
.await;
assert_eq!(
readiness_mismatch["structuredContent"]["code"],
"scope_violation"
);
let mismatch = tools_call_for_project( let mismatch = tools_call_for_project(
Some(&json!({ Some(&json!({
"name": "get_delivery_status", "name": "get_delivery_status",

View File

@ -76,23 +76,36 @@ pub async fn handle_gitea_webhook(
headers: HeaderMap, headers: HeaderMap,
body: Bytes, body: Bytes,
) -> Response { ) -> Response {
let secret: Option<String> = db::pool().get().ok().and_then(|c| { let conn = match db::pool().get() {
c.query_row( Ok(conn) => conn,
"SELECT webhook_secret FROM gitea_repos WHERE project_id = ?1", Err(_) => {
[&project_id], return (
|r| r.get(0), StatusCode::SERVICE_UNAVAILABLE,
Json(json!({"error": "webhook 状态 unavailable"})),
) )
.ok() .into_response()
}); }
let secret = match secret { };
Some(s) => s, let secret = match webhook_secret_for_conn(
None => { &conn,
&crate::credential_store::WindowsCredentialStore,
&project_id,
) {
Ok(secret) => secret,
Err(error) if error.contains("not_configured") || error.contains("不存在") => {
return ( return (
StatusCode::NOT_FOUND, StatusCode::NOT_FOUND,
Json(json!({"error": "项目未绑定 Gitea 仓库或未设置 webhook"})), Json(json!({"error": "项目未绑定 Gitea 仓库或未设置 webhook"})),
) )
.into_response() .into_response()
} }
Err(_) => {
return (
StatusCode::SERVICE_UNAVAILABLE,
Json(json!({"error": "webhook 状态 needs_repair"})),
)
.into_response()
}
}; };
let sig = extract_signature(&headers); let sig = extract_signature(&headers);
@ -129,6 +142,20 @@ pub async fn handle_gitea_webhook(
} }
} }
fn webhook_secret_for_conn(
conn: &rusqlite::Connection,
store: &impl crate::credential_store::CredentialStore,
project_id: &str,
) -> Result<String, String> {
let bytes = crate::credential_store::get_or_migrate_legacy(
conn,
store,
crate::credential_store::LegacyCredentialKind::GiteaWebhookSecret,
project_id,
)?;
String::from_utf8(bytes).map_err(|_| "webhook 状态 needs_repair:凭证编码无效".to_string())
}
async fn handle_push(project_id: &str, body: &Bytes) -> Response { async fn handle_push(project_id: &str, body: &Bytes) -> Response {
let commits = match parse_push_payload(body) { let commits = match parse_push_payload(body) {
Ok(c) => c, Ok(c) => c,
@ -285,6 +312,45 @@ mod tests {
assert!(!verify_hmac_sha256("my-secret", body, "deadbeef")); assert!(!verify_hmac_sha256("my-secret", body, "deadbeef"));
} }
#[test]
fn legacy_webhook_migration_keeps_signature_valid_and_failure_recoverable() {
use crate::credential_store::MemoryCredentialStore;
let conn = crate::db::conn_with_schema();
conn.execute("INSERT INTO gitea_instances (id, name, url, access_token) VALUES ('i', 'test', 'https://example.invalid', 'synthetic-token')", []).unwrap();
conn.execute("INSERT INTO gitea_repos (id, instance_id, project_id, gitea_owner, gitea_repo_name, webhook_secret) VALUES ('r', 'i', 'p', 'owner', 'repo', 'synthetic-webhook-secret')", []).unwrap();
let failing = MemoryCredentialStore::failing();
let error = webhook_secret_for_conn(&conn, &failing, "p").unwrap_err();
assert!(error.contains("needs_repair"));
assert!(!error.contains("synthetic-webhook-secret"));
let legacy: String = conn
.query_row(
"SELECT webhook_secret FROM gitea_repos WHERE project_id='p'",
[],
|r| r.get(0),
)
.unwrap();
assert_eq!(legacy, "synthetic-webhook-secret");
let store = MemoryCredentialStore::new();
let secret = webhook_secret_for_conn(&conn, &store, "p").unwrap();
let body = br#"{"commits":[]}"#;
assert!(verify_hmac_sha256(
&secret,
body,
&sign("synthetic-webhook-secret", body)
));
let (legacy, reference): (Option<String>, String) = conn
.query_row(
"SELECT webhook_secret, webhook_secret_ref FROM gitea_repos WHERE project_id='p'",
[],
|r| Ok((r.get(0)?, r.get(1)?)),
)
.unwrap();
assert!(legacy.is_none());
assert!(!reference.is_empty());
assert_eq!(webhook_secret_for_conn(&conn, &store, "p").unwrap(), secret);
}
#[test] #[test]
fn parse_push_payload_extracts_commits() { fn parse_push_payload_extracts_commits() {
let body = br#"{ let body = br#"{

View File

@ -3,7 +3,7 @@ import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import ReactMarkdown from "react-markdown"; import ReactMarkdown from "react-markdown";
import remarkGfm from "remark-gfm"; import remarkGfm from "remark-gfm";
import { import {
getApiKeys, addApiKey, editApiKey, deleteApiKey, getApiKeys, addApiKey, editApiKey, deleteApiKey, revealApiKey,
type ApiKey, type SaveApiKeyInput, type ApiKey, type SaveApiKeyInput,
} from "../../lib/commands"; } from "../../lib/commands";
@ -43,7 +43,7 @@ function CopyBtn({ value }: { value: string }) {
// ── 模态框表单 ──────────────────────────────────────────────────────────────── // ── 模态框表单 ────────────────────────────────────────────────────────────────
function ApiKeyModal({ initial, onSave, onClose, saving }: { function ApiKeyModal({ initial, onSave, onClose, saving }: {
initial?: ApiKey; initial?: ApiKey & { api_key?: string };
onSave: (input: SaveApiKeyInput) => void; onSave: (input: SaveApiKeyInput) => void;
onClose: () => void; onClose: () => void;
saving: boolean; saving: boolean;
@ -99,7 +99,7 @@ function ApiKeyModal({ initial, onSave, onClose, saving }: {
<label className="text-[11px] text-gray-400 text-right self-start mt-2">备注</label> <label className="text-[11px] text-gray-400 text-right self-start mt-2">备注</label>
<div className="space-y-1"> <div className="space-y-1">
<textarea value={notes} onChange={(e) => setNotes(e.target.value)} <textarea value={notes} onChange={(e) => setNotes(e.target.value)}
placeholder={"支持 Markdown 语法,例如:\n# 使用方式\n- `export API_KEY=\"sk-xxx\"`\n- 启用 provider"} rows={4} placeholder={"支持 Markdown 语法;请勿在备注中粘贴密钥或连接串"} rows={4}
className="w-full text-xs px-2.5 py-2 rounded border border-gray-200 bg-gray-50 focus:outline-none focus:border-blue-300 resize-y min-h-[72px] font-mono" /> className="w-full text-xs px-2.5 py-2 rounded border border-gray-200 bg-gray-50 focus:outline-none focus:border-blue-300 resize-y min-h-[72px] font-mono" />
<span className="text-[10px] text-gray-300">支持 Markdown:标题、列表、代码块、表格等</span> <span className="text-[10px] text-gray-300">支持 Markdown:标题、列表、代码块、表格等</span>
</div> </div>
@ -131,9 +131,25 @@ function ApiKeyCard({ item, onEdit, onDelete }: {
const [showKey, setShowKey] = useState(false); const [showKey, setShowKey] = useState(false);
const [confirmDelete, setConfirmDelete] = useState(false); const [confirmDelete, setConfirmDelete] = useState(false);
const maskedKey = item.api_key.length > 8 const [secret, setSecret] = useState<string>();
? item.api_key.slice(0, 4) + "••••••••" + item.api_key.slice(-4) const [error, setError] = useState<string>();
: "••••••••"; const reveal = async () => {
try {
setSecret(await revealApiKey(item.id));
setShowKey(true);
setError(undefined);
} catch (cause) {
setError(String(cause));
}
};
const copy = async () => {
try {
await navigator.clipboard.writeText(secret ?? await revealApiKey(item.id));
setError(undefined);
} catch (cause) {
setError(String(cause));
}
};
return ( return (
<div className="bg-white border border-gray-200 rounded-xl overflow-hidden shadow-sm"> <div className="bg-white border border-gray-200 rounded-xl overflow-hidden shadow-sm">
@ -193,14 +209,16 @@ function ApiKeyCard({ item, onEdit, onDelete }: {
<div className="flex items-center gap-3"> <div className="flex items-center gap-3">
<span className="text-[11px] text-gray-400 w-16 shrink-0 text-right">API Key</span> <span className="text-[11px] text-gray-400 w-16 shrink-0 text-right">API Key</span>
<span className="text-xs font-mono text-gray-700 flex-1 min-w-0 truncate"> <span className="text-xs font-mono text-gray-700 flex-1 min-w-0 truncate">
{showKey ? item.api_key : maskedKey} {showKey && secret ? secret : "••••••••"}
</span> </span>
<button onClick={() => setShowKey((v) => !v)} <button onClick={() => { if (showKey) setShowKey(false); else void reveal(); }}
className="text-[10px] text-gray-400 hover:text-gray-600 transition-colors shrink-0"> className="text-[10px] text-gray-400 hover:text-gray-600 transition-colors shrink-0">
{showKey ? "隐藏" : "显示"} {showKey ? "隐藏" : "显示"}
</button> </button>
<CopyBtn value={item.api_key} /> <button onClick={() => void copy()}
className="shrink-0 px-1.5 py-0.5 rounded text-[11px] border border-gray-200 text-gray-400 hover:text-blue-500">复制</button>
</div> </div>
{error && <p role="alert" className="text-xs text-red-600">凭证读取失败:{error}</p>}
{/* Base URL */} {/* Base URL */}
{item.base_url && ( {item.base_url && (
@ -229,7 +247,8 @@ function ApiKeyCard({ item, onEdit, onDelete }: {
export function ApiKeysPanel() { export function ApiKeysPanel() {
const qc = useQueryClient(); const qc = useQueryClient();
const [showModal, setShowModal] = useState(false); const [showModal, setShowModal] = useState(false);
const [editingItem, setEditingItem] = useState<ApiKey | null>(null); const [editingItem, setEditingItem] = useState<(ApiKey & { api_key: string }) | null>(null);
const [editError, setEditError] = useState<string>();
const { data: keys = [] } = useQuery({ const { data: keys = [] } = useQuery({
queryKey: ["api-keys"], queryKey: ["api-keys"],
@ -269,6 +288,7 @@ export function ApiKeysPanel() {
</div> </div>
{/* 列表 */} {/* 列表 */}
{editError && <p role="alert" className="text-xs text-red-600">无法编辑凭证:{editError}</p>}
{keys.length === 0 && ( {keys.length === 0 && (
<div className="text-center py-24 text-gray-400"> <div className="text-center py-24 text-gray-400">
<p className="text-4xl mb-4">🔑</p> <p className="text-4xl mb-4">🔑</p>
@ -281,7 +301,14 @@ export function ApiKeysPanel() {
<ApiKeyCard <ApiKeyCard
key={k.id} key={k.id}
item={k} item={k}
onEdit={() => setEditingItem(k)} onEdit={() => { void (async () => {
try {
setEditingItem({ ...k, api_key: await revealApiKey(k.id) });
setEditError(undefined);
} catch (cause) {
setEditError(String(cause));
}
})(); }}
onDelete={() => deleteMut.mutate(k.id)} onDelete={() => deleteMut.mutate(k.id)}
/> />
))} ))}

View File

@ -1374,7 +1374,6 @@ export interface ApiKey {
id: string; id: string;
name: string; name: string;
provider: string; provider: string;
api_key: string;
base_url?: string; base_url?: string;
model?: string; model?: string;
notes?: string; notes?: string;
@ -1403,6 +1402,9 @@ export const editApiKey = (id: string, input: SaveApiKeyInput) =>
export const deleteApiKey = (id: string) => export const deleteApiKey = (id: string) =>
invoke<void>("delete_api_key", { id }); invoke<void>("delete_api_key", { id });
export const revealApiKey = (id: string) =>
invoke<string>("reveal_api_key", { id });
// ── SSH Config & VS Code Remote ────────────────────────────────────────────── // ── SSH Config & VS Code Remote ──────────────────────────────────────────────
export interface SshConfigStatus { export interface SshConfigStatus {
@ -1745,7 +1747,6 @@ export interface GiteaInstance {
id: string; id: string;
name: string; name: string;
url: string; url: string;
access_token: string;
is_default: boolean; is_default: boolean;
created_at?: string; created_at?: string;
} }
@ -1801,6 +1802,20 @@ export const giteaUpdateInstance = (
export const giteaTestConnection = (instanceId: string) => export const giteaTestConnection = (instanceId: string) =>
invoke<GiteaUser>("gitea_test_connection", { instanceId }); invoke<GiteaUser>("gitea_test_connection", { instanceId });
export interface ResourceReadiness {
state: "configured" | "not_configured" | "unavailable" | "needs_repair";
source: "protected_store" | "legacy_pending_migration" | "gitea_binding";
}
export interface ProjectResourceStatus {
project_id: string;
gitea_pr: ResourceReadiness;
gitea_webhook: ResourceReadiness;
}
export const getProjectResourceStatus = (projectId: string) =>
invoke<ProjectResourceStatus>("get_project_resource_status", { projectId });
export const giteaListLinkedRepos = () => export const giteaListLinkedRepos = () =>
invoke<GiteaLinkedRepo[]>("gitea_list_linked_repos"); invoke<GiteaLinkedRepo[]>("gitea_list_linked_repos");
@ -1812,7 +1827,6 @@ export interface GiteaRepoLink {
gitea_owner: string; gitea_owner: string;
gitea_repo_name: string; gitea_repo_name: string;
clone_url?: string; clone_url?: string;
webhook_secret?: string;
webhook_id?: number; webhook_id?: number;
linked_at?: string; linked_at?: string;
} }