@@ -496,6 +496,132 @@ describe("McpCatalog.convertTool bounds untrusted server input", () => {
496496 expect ( ( ) => new Ajv2020 ( { strict : false } ) . compile ( schema ) ) . not . toThrow ( )
497497 } )
498498
499+ test ( "never emits a schema map whose hostile keys inject a prototype" , ( ) => {
500+ const schema = emitted (
501+ JSON . parse (
502+ '{"type":"object","properties":{"__proto__":{"type":"string"},"constructor":{"type":"number"},"prototype":{"type":"boolean"}},"$defs":{"__proto__":{"type":"string"}}}' ,
503+ ) ,
504+ )
505+ const properties = schema . properties as Record < string , unknown >
506+ const defs = schema . $defs as Record < string , unknown >
507+
508+ expect ( Object . hasOwn ( properties , "__proto__" ) ) . toBe ( true )
509+ expect ( Object . hasOwn ( properties , "constructor" ) ) . toBe ( true )
510+ expect ( Object . hasOwn ( properties , "prototype" ) ) . toBe ( true )
511+ expect ( Object . hasOwn ( defs , "__proto__" ) ) . toBe ( true )
512+ expect ( Object . getPrototypeOf ( properties ) ) . not . toBe ( Object . prototype )
513+ expect ( Object . keys ( Object . prototype ) ) . toEqual ( [ ] )
514+ expect ( ( ) => new Ajv2020 ( { strict : false } ) . compile ( schema ) ) . not . toThrow ( )
515+ } )
516+
517+ test ( "drops keyword values that make the emitted document uncompilable" , ( ) => {
518+ const hostile : Array < [ string , Record < string , unknown > ] > = [
519+ [ "invalid $anchor" , { type : "object" , properties : { p : { $anchor : "not valid!" } } } ] ,
520+ [ "invalid $dynamicAnchor" , { type : "object" , properties : { p : { $dynamicAnchor : "1bad" } } } ] ,
521+ [ "invalid root $anchor" , { $anchor : "bad name" , type : "object" } ] ,
522+ [ "invalid anchor in $defs" , { type : "object" , $defs : { D : { $anchor : "1 bad" , type : "string" } } } ] ,
523+ [ "$anchor wrong type" , { type : "object" , properties : { p : { $anchor : 5 } } } ] ,
524+ [ "$schema wrong type" , { $schema : 5 , type : "object" } ] ,
525+ [ "$schema bogus" , { $schema : "bogus" , type : "object" } ] ,
526+ [ "$schema unresolvable dialect" , { $schema : "https://json-schema.org/draft/2019-09/schema" , type : "object" } ] ,
527+ [ "nested bogus $schema" , { type : "object" , properties : { p : { $schema : "bogus" } } } ] ,
528+ [ "$vocabulary wrong type" , { type : "object" , $vocabulary : 5 } ] ,
529+ [
530+ "$vocabulary non-boolean member" ,
531+ { type : "object" , $vocabulary : { "https://json-schema.org/draft/2020-12/vocab/core" : 5 } } ,
532+ ] ,
533+ [ "invalid patternProperties key" , { type : "object" , patternProperties : { "(" : { type : "string" } } } ] ,
534+ [ "invalid nested patternProperties key" , { type : "object" , properties : { p : { patternProperties : { "[a" : { } } } } } ] ,
535+ [ "non-string title" , { type : "object" , title : 7 } ] ,
536+ [ "non-string description" , { type : "object" , description : [ ] } ] ,
537+ [ "non-string nested title" , { type : "object" , properties : { p : { title : 7 } } } ] ,
538+ [
539+ "malformed urn $id" ,
540+ {
541+ $id : "urn:root" ,
542+ type : "object" ,
543+ $defs : { A : { type : "string" } } ,
544+ properties : { a : { $ref : "#/$defs/A" } } ,
545+ } ,
546+ ] ,
547+ [ "interior fragment $id" , { $id : "foo#bar" , type : "object" } ] ,
548+ [ "$schema inside contentSchema" , { type : "object" , properties : { p : { contentSchema : { $schema : 5 } } } } ] ,
549+ [
550+ "invalid anchor inside contentSchema" ,
551+ { type : "object" , properties : { p : { contentSchema : { $anchor : "no good" } } } } ,
552+ ] ,
553+ [ "title inside contentSchema" , { type : "object" , properties : { p : { contentSchema : { title : 7 } } } } ] ,
554+ [
555+ "hostile keywords inside $defs and allOf members" ,
556+ {
557+ type : "object" ,
558+ $defs : { D : { $schema : "bogus" , title : 7 , patternProperties : { "(" : { } } } } ,
559+ properties : { p : { allOf : [ { $anchor : "bad name" } , { $vocabulary : 5 } ] } } ,
560+ } ,
561+ ] ,
562+ ]
563+
564+ for ( const [ label , input ] of hostile ) {
565+ const schema = emitted ( input )
566+ try {
567+ new Ajv2020 ( { strict : false } ) . compile ( schema )
568+ } catch ( error ) {
569+ throw new Error ( `${ label } : ${ ( error as Error ) . message } \n${ JSON . stringify ( schema ) } ` )
570+ }
571+ }
572+ } )
573+
574+ test ( "preserves resolvable dialects, anchors, ids and string metadata" , ( ) => {
575+ const schema = emitted ( {
576+ $schema : "https://json-schema.org/draft/2020-12/schema" ,
577+ $id : "https://example.com/schema.json" ,
578+ title : "Tool" ,
579+ description : "A tool" ,
580+ type : "object" ,
581+ properties : {
582+ p : {
583+ $anchor : "good-anchor_1" ,
584+ patternProperties : { "^a" : { type : "string" } } ,
585+ } ,
586+ urn : { $id : "urn:example:x" , type : "string" } ,
587+ } ,
588+ $vocabulary : { "https://json-schema.org/draft/2020-12/vocab/core" : true , dropped : 5 } ,
589+ } )
590+
591+ expect ( schema . $schema ) . toBe ( "https://json-schema.org/draft/2020-12/schema" )
592+ expect ( schema . $id ) . toBe ( "https://example.com/schema.json" )
593+ expect ( schema . title ) . toBe ( "Tool" )
594+ expect ( schema . description ) . toBe ( "A tool" )
595+ expect ( schema . $vocabulary ) . toEqual ( { "https://json-schema.org/draft/2020-12/vocab/core" : true } )
596+ const props = schema . properties as Record < string , Record < string , unknown > >
597+ expect ( props . p . $anchor ) . toBe ( "good-anchor_1" )
598+ expect ( props . p . patternProperties ) . toEqual ( { "^a" : { type : "string" } } )
599+ expect ( props . urn . $id ) . toBe ( "urn:example:x" )
600+ expect ( ( ) => new Ajv2020 ( { strict : false } ) . compile ( schema ) ) . not . toThrow ( )
601+ } )
602+
603+ test ( "sanitizes the same keyword families at every container" , ( ) => {
604+ const schema = emitted ( {
605+ type : "object" ,
606+ $defs : { D : { title : 7 , $anchor : "bad name" , patternProperties : { "(" : { } } } } ,
607+ patternProperties : { "[a" : { title : 7 } , "^ok" : { $anchor : "fine_1" } } ,
608+ properties : { p : { contentSchema : { $schema : 5 , title : 7 } , items : { $vocabulary : 5 } } } ,
609+ } )
610+ const defs = schema . $defs as Record < string , Record < string , unknown > >
611+ const patterns = schema . patternProperties as Record < string , Record < string , unknown > >
612+ const p = ( schema . properties as Record < string , Record < string , unknown > > ) . p
613+
614+ expect ( defs . D . title ) . toBeUndefined ( )
615+ expect ( defs . D . $anchor ) . toBeUndefined ( )
616+ expect ( defs . D . patternProperties ) . toEqual ( { } )
617+ expect ( Object . hasOwn ( patterns , "[a" ) ) . toBe ( false )
618+ expect ( patterns [ "^ok" ] . $anchor ) . toBe ( "fine_1" )
619+ expect ( ( p . contentSchema as Record < string , unknown > ) . $schema ) . toBeUndefined ( )
620+ expect ( ( p . contentSchema as Record < string , unknown > ) . title ) . toBeUndefined ( )
621+ expect ( ( p . items as Record < string , unknown > ) . $vocabulary ) . toBeUndefined ( )
622+ expect ( ( ) => new Ajv2020 ( { strict : false } ) . compile ( schema ) ) . not . toThrow ( )
623+ } )
624+
499625 test ( "accepts only refs that resolve from the top-level document" , ( ) => {
500626 const schema = emitted ( {
501627 type : "object" ,
0 commit comments