@@ -2,6 +2,7 @@ import * as t from "tap";
22import { getContext , runWithContext , type Context } from "../agent/Context" ;
33import { Postgres } from "./Postgres" ;
44import { createTestAgent } from "../helpers/createTestAgent" ;
5+ import { isWindowsCi } from "../helpers/isWindowsCi" ;
56
67const context : Context = {
78 remoteAddress : "::1" ,
@@ -18,123 +19,129 @@ const context: Context = {
1819 route : "/posts/:id" ,
1920} ;
2021
21- t . test ( "it inspects query method calls and blocks if needed" , async ( t ) => {
22- const agent = createTestAgent ( ) ;
23- agent . start ( [ new Postgres ( ) ] ) ;
22+ t . test (
23+ "it inspects query method calls and blocks if needed" ,
24+ {
25+ skip : isWindowsCi ? "Skip on Windows CI" : false ,
26+ } ,
27+ async ( t ) => {
28+ const agent = createTestAgent ( ) ;
29+ agent . start ( [ new Postgres ( ) ] ) ;
2430
25- const { Client } = require ( "pg" ) as typeof import ( "pg" ) ;
26- const client = new Client ( {
27- user : "root" ,
28- host : "127.0.0.1" ,
29- database : "main_db" ,
30- password : "password" ,
31- port : 27016 ,
32- } ) ;
33- await client . connect ( ) ;
31+ const { Client } = require ( "pg" ) as typeof import ( "pg" ) ;
32+ const client = new Client ( {
33+ user : "root" ,
34+ host : "127.0.0.1" ,
35+ database : "main_db" ,
36+ password : "password" ,
37+ port : 27016 ,
38+ } ) ;
39+ await client . connect ( ) ;
3440
35- try {
36- await client . query ( `
41+ try {
42+ await client . query ( `
3743 CREATE TABLE IF NOT EXISTS cats (
3844 petname varchar(255)
3945 );
4046 ` ) ;
41- await client . query ( "TRUNCATE cats" ) ;
42-
43- t . same ( ( await client . query ( "SELECT petname FROM cats;" ) ) . rows , [ ] ) ;
44- t . same (
45- ( await client . query ( { text : "SELECT petname FROM cats;" } ) ) . rows ,
46- [ ]
47- ) ;
48- t . same (
49- (
50- await runWithContext ( context , ( ) => {
51- return client . query ( "SELECT petname FROM cats;" ) ;
52- } )
53- ) . rows ,
54- [ ]
55- ) ;
56- t . same (
57- (
58- await runWithContext ( context , ( ) => {
59- return client . query ( { text : "SELECT petname FROM cats;" } ) ;
60- } )
61- ) . rows ,
62- [ ]
63- ) ;
47+ await client . query ( "TRUNCATE cats" ) ;
6448
65- const error = await t . rejects ( async ( ) => {
66- await runWithContext ( context , ( ) => {
67- return client . query ( "-- should be blocked" ) ;
68- } ) ;
69- } ) ;
70- if ( error instanceof Error ) {
49+ t . same ( ( await client . query ( "SELECT petname FROM cats;" ) ) . rows , [ ] ) ;
7150 t . same (
72- error . message ,
73- "Zen has blocked an SQL injection: pg.query(...) originating from body.myTitle"
51+ ( await client . query ( { text : "SELECT petname FROM cats;" } ) ) . rows ,
52+ [ ]
7453 ) ;
75- }
76-
77- const error2 = await t . rejects ( async ( ) => {
78- await runWithContext ( context , ( ) => {
79- return client . query ( { text : "-- should be blocked" } ) ;
80- } ) ;
81- } ) ;
82- if ( error2 instanceof Error ) {
8354 t . same (
84- error2 . message ,
85- "Zen has blocked an SQL injection: pg.query(...) originating from body.myTitle"
55+ (
56+ await runWithContext ( context , ( ) => {
57+ return client . query ( "SELECT petname FROM cats;" ) ;
58+ } )
59+ ) . rows ,
60+ [ ]
8661 ) ;
87- }
88-
89- const undefinedQueryError = await t . rejects ( async ( ) => {
90- await runWithContext ( context , ( ) => {
91- // @ts -expect-error Test
92- return client . query ( null ) ;
93- } ) ;
94- } ) ;
95- if ( undefinedQueryError instanceof Error ) {
9662 t . same (
97- undefinedQueryError . message ,
98- "Client was passed a null or undefined query"
63+ (
64+ await runWithContext ( context , ( ) => {
65+ return client . query ( { text : "SELECT petname FROM cats;" } ) ;
66+ } )
67+ ) . rows ,
68+ [ ]
9969 ) ;
100- }
10170
102- await runWithContext (
103- {
104- remoteAddress : "::1" ,
105- method : "POST" ,
106- url : "http://localhost:4000/" ,
107- query : { } ,
108- headers : { } ,
109- body : { } ,
110- cookies : { } ,
111- source : "express" ,
112- route : "/posts/:id" ,
113- routeParams : { } ,
114- } ,
115- ( ) => {
116- return client . query ( "-- This is a comment" ) ;
71+ const error = await t . rejects ( async ( ) => {
72+ await runWithContext ( context , ( ) => {
73+ return client . query ( "-- should be blocked" ) ;
74+ } ) ;
75+ } ) ;
76+ if ( error instanceof Error ) {
77+ t . same (
78+ error . message ,
79+ "Zen has blocked an SQL injection: pg.query(...) originating from body.myTitle"
80+ ) ;
11781 }
118- ) ;
11982
120- // Check if context is available in the callback
121- runWithContext ( context , ( ) => {
122- client . query ( "SELECT petname FROM cats;" , ( error , result ) => {
123- t . match ( getContext ( ) , context ) ;
83+ const error2 = await t . rejects ( async ( ) => {
84+ await runWithContext ( context , ( ) => {
85+ return client . query ( { text : "-- should be blocked" } ) ;
86+ } ) ;
87+ } ) ;
88+ if ( error2 instanceof Error ) {
89+ t . same (
90+ error2 . message ,
91+ "Zen has blocked an SQL injection: pg.query(...) originating from body.myTitle"
92+ ) ;
93+ }
12494
125- try {
126- client . query ( "-- should be blocked" , ( ) => { } ) ;
127- } catch ( error : any ) {
128- t . match (
129- error . message ,
130- / Z e n h a s b l o c k e d a n S Q L i n j e c t i o n : p g .q u e r y \( \. \. \. \) o r i g i n a t i n g f r o m b o d y \. m y T i t l e /
131- ) ;
95+ const undefinedQueryError = await t . rejects ( async ( ) => {
96+ await runWithContext ( context , ( ) => {
97+ // @ts -expect-error Test
98+ return client . query ( null ) ;
99+ } ) ;
100+ } ) ;
101+ if ( undefinedQueryError instanceof Error ) {
102+ t . same (
103+ undefinedQueryError . message ,
104+ "Client was passed a null or undefined query"
105+ ) ;
106+ }
107+
108+ await runWithContext (
109+ {
110+ remoteAddress : "::1" ,
111+ method : "POST" ,
112+ url : "http://localhost:4000/" ,
113+ query : { } ,
114+ headers : { } ,
115+ body : { } ,
116+ cookies : { } ,
117+ source : "express" ,
118+ route : "/posts/:id" ,
119+ routeParams : { } ,
120+ } ,
121+ ( ) => {
122+ return client . query ( "-- This is a comment" ) ;
132123 }
124+ ) ;
125+
126+ // Check if context is available in the callback
127+ runWithContext ( context , ( ) => {
128+ client . query ( "SELECT petname FROM cats;" , ( error , result ) => {
129+ t . match ( getContext ( ) , context ) ;
130+
131+ try {
132+ client . query ( "-- should be blocked" , ( ) => { } ) ;
133+ } catch ( error : any ) {
134+ t . match (
135+ error . message ,
136+ / Z e n h a s b l o c k e d a n S Q L i n j e c t i o n : p g .q u e r y \( \. \. \. \) o r i g i n a t i n g f r o m b o d y \. m y T i t l e /
137+ ) ;
138+ }
139+ } ) ;
133140 } ) ;
134- } ) ;
135- } catch ( error : any ) {
136- t . fail ( error ) ;
137- } finally {
138- await client . end ( ) ;
141+ } catch ( error : any ) {
142+ t . fail ( error ) ;
143+ } finally {
144+ await client . end ( ) ;
145+ }
139146 }
140- } ) ;
147+ ) ;
0 commit comments