@@ -12,6 +12,8 @@ pub mod imports;
1212
1313pub type Result < T > = std:: result:: Result < T , String > ;
1414const LIMIT : u64 = 8 * 1024 * 1024 ;
15+ pub const DEFAULT_HOST_COMMAND_OUTPUT_BYTES : u64 = 4096 ;
16+ pub const MAX_HOST_COMMAND_OUTPUT_BYTES : u64 = 1024 * 1024 ;
1517
1618#[ derive( Clone , Debug , Deserialize , Serialize , PartialEq , Eq ) ]
1719#[ serde( rename_all = "camelCase" , deny_unknown_fields) ]
@@ -36,6 +38,12 @@ pub struct HostCommand {
3638 pub id : String ,
3739 pub program : String ,
3840 pub args : Vec < String > ,
41+ #[ serde(
42+ default ,
43+ rename = "maxOutputBytes" ,
44+ skip_serializing_if = "Option::is_none"
45+ ) ]
46+ pub max_output_bytes : Option < u64 > ,
3947}
4048impl Manifest {
4149 pub fn validate ( & self ) -> Result < ( ) > {
@@ -60,6 +68,9 @@ impl Manifest {
6068 byte. is_ascii_alphanumeric ( ) || matches ! ( byte, b'-' | b'_' | b'.' )
6169 } )
6270 || command. args . len ( ) > 16
71+ || command
72+ . max_output_bytes
73+ . is_some_and ( |limit| !( 1 ..=MAX_HOST_COMMAND_OUTPUT_BYTES ) . contains ( & limit) )
6374 || command
6475 . args
6576 . iter ( )
@@ -553,7 +564,20 @@ impl Manager {
553564 if manifest. id != snapshot. 2 {
554565 return Err ( "Reloaded plugin manifest ID changed; import it as a new plugin" . into ( ) ) ;
555566 }
556- if manifest. host . clone ( ) . unwrap_or_default ( ) != snapshot. 3 {
567+ // Compare effective access without changing the stored manifest representation.
568+ let effective_grants = |mut grants : HostGrants | {
569+ for command in & mut grants. commands {
570+ command. max_output_bytes = Some (
571+ command
572+ . max_output_bytes
573+ . unwrap_or ( DEFAULT_HOST_COMMAND_OUTPUT_BYTES ) ,
574+ ) ;
575+ }
576+ grants
577+ } ;
578+ if effective_grants ( manifest. host . clone ( ) . unwrap_or_default ( ) )
579+ != effective_grants ( snapshot. 3 )
580+ {
557581 return Err ( "Host access changed; use Load from folder to review it" . into ( ) ) ;
558582 }
559583 let revision = hash ( & bytes) ;
@@ -761,7 +785,7 @@ fn atomic_write(path: &Path, bytes: &[u8]) -> Result<()> {
761785
762786#[ cfg( test) ]
763787mod tests {
764- use super :: { artifact_from_text, Manager , Manifest } ;
788+ use super :: { artifact_from_text, Manager , Manifest , MAX_HOST_COMMAND_OUTPUT_BYTES } ;
765789 use std:: fs;
766790
767791 #[ test]
@@ -780,6 +804,27 @@ mod tests {
780804 }
781805 } ) ;
782806 assert ! ( artifact_from_text( & manifest. to_string( ) , "export const x = 1" . into( ) ) . is_ok( ) ) ;
807+ let old: Manifest = serde_json:: from_value ( manifest. clone ( ) ) . unwrap ( ) ;
808+ assert_eq ! (
809+ old. host. as_ref( ) . unwrap( ) . commands[ 0 ] . max_output_bytes,
810+ None
811+ ) ;
812+ assert_eq ! ( serde_json:: to_value( & old) . unwrap( ) , manifest) ;
813+ for limit in [ 1 , 4096 , MAX_HOST_COMMAND_OUTPUT_BYTES ] {
814+ let mut valid = manifest. clone ( ) ;
815+ valid[ "host" ] [ "commands" ] [ 0 ] [ "maxOutputBytes" ] = serde_json:: json!( limit) ;
816+ assert ! ( artifact_from_text( & valid. to_string( ) , "export const x = 1" . into( ) ) . is_ok( ) ) ;
817+ }
818+ for limit in [
819+ serde_json:: json!( 0 ) ,
820+ serde_json:: json!( -1 ) ,
821+ serde_json:: json!( 1.5 ) ,
822+ serde_json:: json!( MAX_HOST_COMMAND_OUTPUT_BYTES + 1 ) ,
823+ ] {
824+ let mut invalid = manifest. clone ( ) ;
825+ invalid[ "host" ] [ "commands" ] [ 0 ] [ "maxOutputBytes" ] = limit;
826+ assert ! ( artifact_from_text( & invalid. to_string( ) , "export const x = 1" . into( ) ) . is_err( ) ) ;
827+ }
783828 for invalid_origin in [
784829 "http://api.example.com" ,
785830 "https://api.example.com/path" ,
@@ -837,7 +882,7 @@ mod tests {
837882
838883 fs:: write (
839884 source. join ( "manifest.json" ) ,
840- r#"{"id":"example.page","name":"Example","apiVersion":1,"host":{"commands":[{"id":"second","program":"example-cli","args":["status","--json"]}],"networkOrigins":["https://two.example"]}}"# ,
885+ r#"{"id":"example.page","name":"Example","apiVersion":1,"host":{"commands":[{"id":"second","program":"example-cli","args":["status","--json"],"maxOutputBytes":65536 }],"networkOrigins":["https://two.example"]}}"# ,
841886 )
842887 . unwrap ( ) ;
843888 let second = manager
@@ -851,6 +896,12 @@ mod tests {
851896 assert ! ( manager. host_grants( "example.page" , & first) . is_err( ) ) ;
852897 let grants = manager. host_grants ( "example.page" , & second) . unwrap ( ) ;
853898 assert_eq ! ( grants. commands[ 0 ] . id, "second" ) ;
899+ assert_eq ! ( grants. commands[ 0 ] . max_output_bytes, Some ( 65536 ) ) ;
900+ let reopened = Manager :: open ( Some ( temp. path ( ) . into ( ) ) , "test" , false ) . unwrap ( ) ;
901+ assert_eq ! (
902+ reopened. host_grants( "example.page" , & second) . unwrap( ) ,
903+ grants
904+ ) ;
854905 assert_eq ! ( grants. network_origins, [ "https://two.example" ] ) ;
855906 manager. change ( "disable" , "example.page" ) . unwrap ( ) ;
856907 assert ! ( manager. host_grants( "example.page" , & second) . is_err( ) ) ;
0 commit comments