@@ -147,6 +147,7 @@ describe('request complexity', () => {
147147 await reconfigureServer ( { } ) ;
148148 const config = Config . get ( 'test' ) ;
149149 expect ( config . requestComplexity ) . toEqual ( {
150+ allowRegex : true ,
150151 batchRequestLimit : - 1 ,
151152 includeDepth : - 1 ,
152153 includeCount : - 1 ,
@@ -540,4 +541,174 @@ describe('request complexity', () => {
540541 ) . toBeResolved ( ) ;
541542 } ) ;
542543 } ) ;
544+
545+ describe ( 'allowRegex' , ( ) => {
546+ let config ;
547+
548+ beforeEach ( async ( ) => {
549+ await reconfigureServer ( {
550+ requestComplexity : { allowRegex : false } ,
551+ } ) ;
552+ config = Config . get ( 'test' ) ;
553+ } ) ;
554+
555+ it ( 'should reject $regex query when allowRegex is false (unauthenticated)' , async ( ) => {
556+ const where = { username : { $regex : 'test' } } ;
557+ await expectAsync (
558+ rest . find ( config , auth . nobody ( config ) , '_User' , where )
559+ ) . toBeRejectedWith (
560+ jasmine . objectContaining ( {
561+ message : '$regex operator is not allowed' ,
562+ } )
563+ ) ;
564+ } ) ;
565+
566+ it ( 'should reject $regex query when allowRegex is false (authenticated user)' , async ( ) => {
567+ const user = new Parse . User ( ) ;
568+ user . setUsername ( 'testuser' ) ;
569+ user . setPassword ( 'testpass' ) ;
570+ await user . signUp ( ) ;
571+ const userAuth = new auth . Auth ( {
572+ config,
573+ isMaster : false ,
574+ user,
575+ } ) ;
576+ const where = { username : { $regex : 'test' } } ;
577+ await expectAsync (
578+ rest . find ( config , userAuth , '_User' , where )
579+ ) . toBeRejectedWith (
580+ jasmine . objectContaining ( {
581+ message : '$regex operator is not allowed' ,
582+ } )
583+ ) ;
584+ } ) ;
585+
586+ it ( 'should allow $regex query when allowRegex is false with master key' , async ( ) => {
587+ const where = { username : { $regex : 'test' } } ;
588+ await expectAsync (
589+ rest . find ( config , auth . master ( config ) , '_User' , where )
590+ ) . toBeResolved ( ) ;
591+ } ) ;
592+
593+ it ( 'should allow $regex query when allowRegex is true (default)' , async ( ) => {
594+ await reconfigureServer ( {
595+ requestComplexity : { allowRegex : true } ,
596+ } ) ;
597+ config = Config . get ( 'test' ) ;
598+ const where = { username : { $regex : 'test' } } ;
599+ await expectAsync (
600+ rest . find ( config , auth . nobody ( config ) , '_User' , where )
601+ ) . toBeResolved ( ) ;
602+ } ) ;
603+
604+ it ( 'should reject $regex inside $or when allowRegex is false' , async ( ) => {
605+ const where = {
606+ $or : [
607+ { username : { $regex : 'test' } } ,
608+ { username : 'exact' } ,
609+ ] ,
610+ } ;
611+ await expectAsync (
612+ rest . find ( config , auth . nobody ( config ) , '_User' , where )
613+ ) . toBeRejectedWith (
614+ jasmine . objectContaining ( {
615+ message : '$regex operator is not allowed' ,
616+ } )
617+ ) ;
618+ } ) ;
619+
620+ it ( 'should reject $regex inside $and when allowRegex is false' , async ( ) => {
621+ const where = {
622+ $and : [
623+ { username : { $regex : 'test' } } ,
624+ { username : 'exact' } ,
625+ ] ,
626+ } ;
627+ await expectAsync (
628+ rest . find ( config , auth . nobody ( config ) , '_User' , where )
629+ ) . toBeRejectedWith (
630+ jasmine . objectContaining ( {
631+ message : '$regex operator is not allowed' ,
632+ } )
633+ ) ;
634+ } ) ;
635+
636+ it ( 'should reject $regex inside $nor when allowRegex is false' , async ( ) => {
637+ const where = {
638+ $nor : [
639+ { username : { $regex : 'test' } } ,
640+ ] ,
641+ } ;
642+ await expectAsync (
643+ rest . find ( config , auth . nobody ( config ) , '_User' , where )
644+ ) . toBeRejectedWith (
645+ jasmine . objectContaining ( {
646+ message : '$regex operator is not allowed' ,
647+ } )
648+ ) ;
649+ } ) ;
650+
651+ it ( 'should allow $regex by default when allowRegex is not configured' , async ( ) => {
652+ await reconfigureServer ( { } ) ;
653+ config = Config . get ( 'test' ) ;
654+ const where = { username : { $regex : 'test' } } ;
655+ await expectAsync (
656+ rest . find ( config , auth . nobody ( config ) , '_User' , where )
657+ ) . toBeResolved ( ) ;
658+ } ) ;
659+
660+ it ( 'should allow $regex with maintenance key when allowRegex is false' , async ( ) => {
661+ const where = { username : { $regex : 'test' } } ;
662+ await expectAsync (
663+ rest . find ( config , auth . maintenance ( config ) , '_User' , where )
664+ ) . toBeResolved ( ) ;
665+ } ) ;
666+
667+ describe ( 'LiveQuery' , ( ) => {
668+ beforeEach ( async ( ) => {
669+ await reconfigureServer ( {
670+ requestComplexity : { allowRegex : false } ,
671+ liveQuery : { classNames : [ 'TestObject' ] } ,
672+ startLiveQueryServer : true ,
673+ } ) ;
674+ config = Config . get ( 'test' ) ;
675+ } ) ;
676+
677+ afterEach ( async ( ) => {
678+ const client = await Parse . CoreManager . getLiveQueryController ( ) . getDefaultLiveQueryClient ( ) ;
679+ if ( client ) {
680+ await client . close ( ) ;
681+ }
682+ } ) ;
683+
684+ it ( 'should reject LiveQuery subscription with $regex when allowRegex is false' , async ( ) => {
685+ const query = new Parse . Query ( 'TestObject' ) ;
686+ query . matches ( 'field' , / t e s t / ) ;
687+ await expectAsync ( query . subscribe ( ) ) . toBeRejectedWith (
688+ jasmine . objectContaining ( { code : Parse . Error . INVALID_QUERY } )
689+ ) ;
690+ } ) ;
691+
692+ it ( 'should reject LiveQuery subscription with $regex inside $or when allowRegex is false' , async ( ) => {
693+ const query = new Parse . Query ( 'TestObject' ) ;
694+ query . _where = {
695+ $or : [
696+ { field : { $regex : 'test' } } ,
697+ { field : 'exact' } ,
698+ ] ,
699+ } ;
700+ await expectAsync ( query . subscribe ( ) ) . toBeRejectedWith (
701+ jasmine . objectContaining ( { code : Parse . Error . INVALID_QUERY } )
702+ ) ;
703+ } ) ;
704+
705+ it ( 'should allow LiveQuery subscription without $regex when allowRegex is false' , async ( ) => {
706+ const query = new Parse . Query ( 'TestObject' ) ;
707+ query . equalTo ( 'field' , 'test' ) ;
708+ const subscription = await query . subscribe ( ) ;
709+ expect ( subscription ) . toBeDefined ( ) ;
710+ subscription . unsubscribe ( ) ;
711+ } ) ;
712+ } ) ;
713+ } ) ;
543714} ) ;
0 commit comments