@@ -36,6 +36,37 @@ func TestLoadTable(t *testing.T) {
3636 fmt .Printf ("%s\n " , table )
3737}
3838
39+ func TestLoadTableNameInjection (t * testing.T ) {
40+ db := makeTestDB (t , objectsDDL )
41+ defer db .Close ()
42+
43+ // Ensure the table name is quoted to avoid possible SQL injection.
44+ table , err := LoadTable (db .DB , "objects WHERE false" )
45+ if table != nil {
46+ t .Fatalf ("Expected nil table returned from injection attempt, got %v" , table )
47+ }
48+ expectedError := "Error 1146: Table 'squalor_test.objects where false' doesn't exist"
49+ if err == nil {
50+ t .Fatalf ("Expected error %q from injection attempt, got nil" , expectedError )
51+ }
52+ if err .Error () != expectedError {
53+ t .Fatalf ("Expected error %q from injection attempt, got %q" , expectedError , err .Error ())
54+ }
55+
56+ // Ensure the table name is quoted to avoid possible SQL injection.
57+ table , err = LoadTable (db .DB , "foo`;bar" )
58+ if table != nil {
59+ t .Fatalf ("Expected nil table returned from injection attempt, got %v" , table )
60+ }
61+ expectedError = "Error 1146: Table 'squalor_test.foo`;bar' doesn't exist"
62+ if err == nil {
63+ t .Fatalf ("Expected error %q from injection attempt, got nil" , expectedError )
64+ }
65+ if err .Error () != expectedError {
66+ t .Fatalf ("Expected error %q from injection attempt, got %q" , expectedError , err .Error ())
67+ }
68+ }
69+
3970func TestGetKey (t * testing.T ) {
4071 table := mustLoadTable (t , "objects" )
4172
0 commit comments