@@ -253,10 +253,18 @@ func (c *conn) HandleStagingDelete(ctx context.Context, presignedUrl string, hea
253
253
return driver .ResultNoRows , nil
254
254
}
255
255
256
- func localPathIsAllowed (ctx context.Context , localFile string ) bool {
257
- stagingAllowedLocalPaths := driverctx .StagingPathsFromContext (ctx )
256
+ func localPathIsAllowed (stagingAllowedLocalPaths []string , localFile string ) bool {
258
257
for i := range stagingAllowedLocalPaths {
259
- path := stagingAllowedLocalPaths [i ]
258
+ // Convert both filepaths to absolute paths to avoid potential issues.
259
+ //
260
+ path , err := filepath .Abs (stagingAllowedLocalPaths [i ])
261
+ if err != nil {
262
+ return false
263
+ }
264
+ localFile , err := filepath .Abs (localFile )
265
+ if err != nil {
266
+ return false
267
+ }
260
268
relativePath , err := filepath .Rel (path , localFile )
261
269
if err != nil {
262
270
return false
@@ -275,13 +283,16 @@ func (c *conn) ExecStagingOperation(
275
283
var sqlRow []driver.Value
276
284
colNames := row .Columns ()
277
285
sqlRow = make ([]driver.Value , len (colNames ))
278
- row .Next (sqlRow )
286
+ err := row .Next (sqlRow )
287
+ if err != nil {
288
+ return nil , dbsqlerrint .NewDriverError (ctx , "Error fetching staging operation results" , err )
289
+ }
279
290
var stringValues []string = make ([]string , 4 )
280
291
for i := range stringValues {
281
292
if s , ok := sqlRow [i ].(string ); ok {
282
293
stringValues [i ] = s
283
294
} else {
284
- return nil , fmt . Errorf ( "local file operations are restricted to paths within the configured staging_allowed_local_path" )
295
+ return nil , dbsqlerrint . NewDriverError ( ctx , "Received unexpected response from the server." , nil )
285
296
}
286
297
}
287
298
operation := stringValues [0 ]
@@ -292,18 +303,19 @@ func (c *conn) ExecStagingOperation(
292
303
return nil , err
293
304
}
294
305
localFile := stringValues [3 ]
306
+ stagingAllowedLocalPaths := driverctx .StagingPathsFromContext (ctx )
295
307
switch operation {
296
308
case "PUT" :
297
- if localPathIsAllowed (ctx , localFile ) {
309
+ if localPathIsAllowed (stagingAllowedLocalPaths , localFile ) {
298
310
return c .HandleStagingPut (ctx , presignedUrl , headers , localFile )
299
311
} else {
300
- return nil , fmt . Errorf ( "local file operations are restricted to paths within the configured staging_allowed_local_path" )
312
+ return nil , dbsqlerrint . NewDriverError ( ctx , "local file operations are restricted to paths within the configured stagingAllowedLocalPath" , nil )
301
313
}
302
314
case "GET" :
303
- if localPathIsAllowed (ctx , localFile ) {
315
+ if localPathIsAllowed (stagingAllowedLocalPaths , localFile ) {
304
316
return c .HandleStagingGet (ctx , presignedUrl , headers , localFile )
305
317
} else {
306
- return nil , dbsqlerrint .NewDriverError (ctx , "local file operations are restricted to paths within the configured staging_allowed_local_path " , nil )
318
+ return nil , dbsqlerrint .NewDriverError (ctx , "local file operations are restricted to paths within the configured stagingAllowedLocalPath " , nil )
307
319
}
308
320
case "DELETE" :
309
321
return c .HandleStagingDelete (ctx , presignedUrl , headers )
0 commit comments