@@ -4,6 +4,7 @@ use std::io::Read;
44
55use base64:: Engine ;
66use ed25519_dalek:: { Signature , Verifier , VerifyingKey } ;
7+ use futures:: StreamExt ;
78use serde_json:: { json, Value } ;
89use sha2:: { Digest , Sha256 } ;
910
@@ -215,9 +216,10 @@ pub async fn install_from_store(plugin_id: &str) -> Result<TaskPlugin, String> {
215216 . as_str ( )
216217 . filter ( |u| !u. is_empty ( ) )
217218 . ok_or_else ( || "插件下载地址缺失" . to_string ( ) ) ?;
219+ let expected_size = platform[ "size" ] . as_u64 ( ) . unwrap_or ( 0 ) ;
218220 let expected_sha256 = platform[ "sha256" ] . as_str ( ) . map ( str:: to_string) ;
219221
220- let bytes = download_zip_bytes ( url, expected_sha256. as_deref ( ) ) . await ?;
222+ let bytes = download_zip_bytes ( url, expected_size , expected_sha256. as_deref ( ) ) . await ?;
221223 let mut plugin = import_zip_bytes ( & bytes, true ) ?;
222224 if plugin. id != plugin_id {
223225 return Err ( format ! ( "安装包内的插件标识({})与所选插件({plugin_id})不一致" , plugin. id) ) ;
@@ -379,24 +381,77 @@ fn decode_hex_32(hex: &str) -> Option<[u8; 32]> {
379381 Some ( out)
380382}
381383
382- async fn download_zip_bytes ( url : & str , expected_sha256 : Option < & str > ) -> Result < Vec < u8 > , String > {
383- if !website:: download_url ( url) {
384+ async fn download_zip_bytes (
385+ url : & str ,
386+ expected_size : u64 ,
387+ expected_sha256 : Option < & str > ,
388+ ) -> Result < Vec < u8 > , String > {
389+ if !website:: plugin_download_url ( url) {
384390 return Err ( "插件下载地址不在官方受信任域名内" . to_string ( ) ) ;
385391 }
386- let bytes = website:: fetch_bytes ( url, MAX_ZIP_BYTES )
392+ crate :: server:: core:: update:: assert_downloadable ( url) . map_err ( |err| err. message ) ?;
393+ if expected_size > MAX_ZIP_BYTES as u64 {
394+ return Err ( "插件安装包超过 2 MB 上限" . to_string ( ) ) ;
395+ }
396+ let headers = vec ! [
397+ ( "User-Agent" . to_string( ) , "agent2api-plugin" . to_string( ) ) ,
398+ ( "Accept" . to_string( ) , "application/octet-stream" . to_string( ) ) ,
399+ ] ;
400+ let response = website:: request ( url, & headers, None )
387401 . await
388402 . map_err ( |err| format ! ( "下载定时任务插件失败:{}" , err. message) ) ?;
389- if let Some ( expected) = expected_sha256 {
403+ if response. status ( ) . as_u16 ( ) != 200 {
404+ return Err ( format ! (
405+ "下载定时任务插件失败:官网返回 HTTP {}" ,
406+ response. status( ) . as_u16( )
407+ ) ) ;
408+ }
409+ let bytes = read_zip_download ( response, expected_size) . await ?;
410+ if let Some ( expected) = expected_sha256
411+ . map ( str:: trim)
412+ . filter ( |value| !value. is_empty ( ) )
413+ {
390414 let mut hasher = Sha256 :: new ( ) ;
391415 hasher. update ( & bytes) ;
392416 let actual: String = hasher. finalize ( ) . iter ( ) . map ( |b| format ! ( "{b:02x}" ) ) . collect ( ) ;
393- if !actual. eq_ignore_ascii_case ( expected. trim ( ) ) {
417+ if !actual. eq_ignore_ascii_case ( expected) {
394418 return Err ( "插件下载包 SHA-256 校验不一致" . to_string ( ) ) ;
395419 }
396420 }
397421 Ok ( bytes)
398422}
399423
424+ async fn read_zip_download (
425+ response : reqwest:: Response ,
426+ expected_size : u64 ,
427+ ) -> Result < Vec < u8 > , String > {
428+ let declared = response. content_length ( ) . unwrap_or ( 0 ) ;
429+ if declared > MAX_ZIP_BYTES as u64 {
430+ return Err ( "插件安装包超过 2 MB 上限" . to_string ( ) ) ;
431+ }
432+ let mut stream = response. bytes_stream ( ) ;
433+ let mut bytes = Vec :: new ( ) ;
434+ loop {
435+ let chunk = tokio:: time:: timeout ( std:: time:: Duration :: from_secs ( 30 ) , stream. next ( ) )
436+ . await
437+ . map_err ( |_| "下载定时任务插件超时" . to_string ( ) ) ?;
438+ let Some ( chunk) = chunk else { break } ;
439+ let chunk = chunk. map_err ( |err| format ! ( "下载定时任务插件中断:{err}" ) ) ?;
440+ if bytes. len ( ) . saturating_add ( chunk. len ( ) ) > MAX_ZIP_BYTES {
441+ return Err ( "插件安装包超过 2 MB 上限" . to_string ( ) ) ;
442+ }
443+ bytes. extend_from_slice ( & chunk) ;
444+ }
445+ let received = bytes. len ( ) as u64 ;
446+ if received == 0
447+ || ( declared > 0 && received != declared)
448+ || ( expected_size > 0 && received != expected_size)
449+ {
450+ return Err ( "下载到的插件包不完整,已拒绝使用" . to_string ( ) ) ;
451+ }
452+ Ok ( bytes)
453+ }
454+
400455#[ cfg( test) ]
401456mod tests {
402457 use std:: io:: Write ;
@@ -475,4 +530,28 @@ export async function onSchedule() { return { level: "success", message: "ok" };
475530 let err = prepare_zip_bytes ( & mismatched, None , None ) . err ( ) . expect ( "must fail" ) ;
476531 assert ! ( err. contains( "不一致" ) , "unexpected err: {err}" ) ;
477532 }
533+
534+ #[ tokio:: test]
535+ async fn download_zip_bytes_rejects_non_plugin_urls_and_oversized_assets ( ) {
536+ for bad_url in [
537+ "https://example.com/downloads/plugins/demo/v1/demo.zip" ,
538+ "https://agent2api.dev/plugin-assets/demo.png" ,
539+ "https://cdn.agent2api.dev/advertisements/banner.png" ,
540+ "https://cdn.agent2api.dev/agent2api/installers/aimod-cc/Agent2API/v3.6.0/abc/app.dmg" ,
541+ ] {
542+ let err = download_zip_bytes ( bad_url, 1024 , None )
543+ . await
544+ . err ( )
545+ . expect ( "non-plugin url must fail" ) ;
546+ assert ! ( err. contains( "受信任域名" ) , "unexpected err for {bad_url}: {err}" ) ;
547+ }
548+
549+ let valid_cdn_url = "https://cdn.agent2api.dev/agent2api/plugins/agent2api-task-newapi-login-checkin/task-newapi-login-checkin-v1.0.0-beta.1/0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef/agent2api-task-newapi-login-checkin-1.0.0-beta.1-task-script.zip" ;
550+ let err = download_zip_bytes ( valid_cdn_url, ( MAX_ZIP_BYTES as u64 ) + 1 , None )
551+ . await
552+ . err ( )
553+ . expect ( "oversized asset must fail before network request" ) ;
554+ assert ! ( err. contains( "超过 2 MB 上限" ) , "unexpected err: {err}" ) ;
555+ assert ! ( !err. contains( "图标地址无效" ) , "must not go through icon fetch_bytes" ) ;
556+ }
478557}
0 commit comments