@@ -80,32 +80,36 @@ describe('static parameter queries', () => {
8080 expect ( query . getStaticBucketIds ( normalizeTokenParameters ( { user_id : 'user1' } ) ) ) . toEqual ( [ 'mybucket["user1"]' ] ) ;
8181 } ) ;
8282
83- describe ( '[un]authenticatedRequestParameters' , function ( ) {
84- function makeTest (
85- sql : string ,
86- usesAuthenticatedRequestParameters : boolean ,
87- usesUnauthenticatedRequestParameters : boolean
88- ) {
83+ describe ( 'dangerous queries' , function ( ) {
84+ function testDangerousQuery ( sql : string ) {
8985 test ( sql , function ( ) {
90- const query = SqlParameterQuery . fromSql ( 'mybucket' , sql ) as StaticSqlParameterQuery ;
86+ const query = SqlParameterQuery . fromSql ( 'mybucket' , sql ) as SqlParameterQuery ;
87+ expect ( query . errors ) . toMatchObject ( [
88+ {
89+ message : 'Pontially dangerous query based on unauthenticated client parameters'
90+ }
91+ ] ) ;
92+ expect ( query . usesDangerousRequestParameters ) . toEqual ( true ) ;
93+ } ) ;
94+ }
95+ function testSafeQuery ( sql : string ) {
96+ test ( sql , function ( ) {
97+ const query = SqlParameterQuery . fromSql ( 'mybucket' , sql ) as SqlParameterQuery ;
9198 expect ( query . errors ) . toEqual ( [ ] ) ;
92- expect ( query . hasAuthenticatedBucketParameters ) . toEqual ( usesAuthenticatedRequestParameters ) ;
93- expect ( query . usesUnauthenticatedRequestParameters ) . toEqual ( usesUnauthenticatedRequestParameters ) ;
99+ expect ( query . usesDangerousRequestParameters ) . toEqual ( false ) ;
94100 } ) ;
95101 }
96102
97- makeTest ( 'select request.user_id() as user_id' , true , false ) ;
98- makeTest ( "select request.parameters() ->> 'project_id' as project_id" , false , true ) ;
99- makeTest ( "select request.user_id() as user_id, request.parameters() ->> 'project_id' as project_id" , true , true ) ;
100- makeTest ( "select where request.parameters() ->> 'include_comments'" , false , true ) ;
101- makeTest ( "select where request.jwt() ->> 'role' = 'authenticated'" , false , false ) ;
102- makeTest ( "select request.user_id() as user_id where request.jwt() ->> 'role' = 'authenticated'" , true , false ) ;
103+ testSafeQuery ( 'select request.user_id() as user_id' ) ;
104+ testDangerousQuery ( "select request.parameters() ->> 'project_id' as project_id" ) ;
105+ testSafeQuery ( "select request.user_id() as user_id, request.parameters() ->> 'project_id' as project_id" ) ;
106+ testDangerousQuery ( "select where request.parameters() ->> 'include_comments'" ) ;
107+ testSafeQuery ( "select where request.jwt() ->> 'role' = 'authenticated'" ) ;
108+ testSafeQuery ( "select request.user_id() as user_id where request.jwt() ->> 'role' = 'authenticated'" ) ;
103109 // Does use token parameters, but is still considered dangerous
104110 // Any authenticated user can select an arbitrary project_id
105- makeTest (
106- "select request.parameters() ->> 'project_id' as project_id where request.jwt() ->> 'role' = 'authenticated'" ,
107- false ,
108- true
111+ testDangerousQuery (
112+ "select request.parameters() ->> 'project_id' as project_id where request.jwt() ->> 'role' = 'authenticated'"
109113 ) ;
110114 } ) ;
111115} ) ;
0 commit comments