vitess.io/vitess@v0.16.2/go/vt/vtgate/errorsanitizer/error_sanitizer_test.go (about) 1 /* 2 Copyright 2022 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package errorsanitizer 18 19 import ( 20 "testing" 21 22 "github.com/stretchr/testify/assert" 23 ) 24 25 func TestErrorNormalization(t *testing.T) { 26 testCases := []struct { 27 name, input, output string 28 }{ 29 { 30 "no change", 31 "nothing to normalize here", 32 "nothing to normalize here", 33 }, 34 { 35 "no truncation after 'code ='", 36 `target: mars.-.primary: vttablet: rpc error: code = InvalidArgument desc = Unknown system variable 'query_response_time_stats' (errno 1193) (sqlstate HY000) (CallerID: admin): Sql: \"select @@query_response_time_stats from dual\", BindVars: {}: 2760`, 37 `target: mars.-.primary: vttablet: rpc error: code = InvalidArgument desc = Unknown system variable 'query_response_time_stats' (errno 1193) (sqlstate HY000) (CallerID: admin)`, 38 }, 39 { 40 "replace after 'Duplicate entry'", 41 `target: keep3rv1.-.primary: vttablet: Duplicate entry '0' for key 'stats.id' (errno 1062) (sqlstate 23000) (CallerID: admin): Sql: \"insert into stats(jobs, work_done, keepers, rewarded_kp3r, bonded_kp3r) values (:v1, :v2, :v3, :v4, :v5)\", BindVars: {v1: \"type:VARBINARY value:\\\"0\\\"\"v2: \"type:VARBINARY value:\\\"1\\\"\"v3: \"type:VARBINARY value:\\\"0\\\"\"v4: \"type:VARBINARY value:\\\"0\\\"\"v5: \"type:VARBINARY value:\\\"0\\\"\"}`, 42 `target: keep3rv1.-.primary: vttablet: Duplicate entry '<val>' for key 'stats.id' (errno 1062) (sqlstate 23000) (CallerID: admin)`, 43 }, 44 { 45 "malicious 'Duplicate entry'", // the entry value `this ' for key` tries to trick the regexp 46 `target: keep3rv1.-.primary: vttablet: Duplicate entry 'this ' for key' for key 'stats.id' (errno 1062) (sqlstate 23000) (CallerID: admin): Sql: \"insert into stats(jobs, work_done, keepers, rewarded_kp3r, bonded_kp3r) values (:v1, :v2, :v3, :v4, :v5)\", BindVars: {v1: \"type:VARBINARY value:\\\"0\\\"\"v2: \"type:VARBINARY value:\\\"1\\\"\"v3: \"type:VARBINARY value:\\\"0\\\"\"v4: \"type:VARBINARY value:\\\"0\\\"\"v5: \"type:VARBINARY value:\\\"0\\\"\"}`, 47 `target: keep3rv1.-.primary: vttablet: Duplicate entry '<val>' for key 'stats.id' (errno 1062) (sqlstate 23000) (CallerID: admin)`, 48 }, 49 { 50 "malicious key name", // the key name `column ' for key` actually does trick the regexp 51 `target: keep3rv1.-.primary: vttablet: Duplicate entry 'this' for key 'column ' for key' (errno 1062) (sqlstate 23000) (CallerID: admin): Sql: \"insert into stats(jobs, work_done, keepers, rewarded_kp3r, bonded_kp3r) values (:v1, :v2, :v3, :v4, :v5)\", BindVars: {v1: \"type:VARBINARY value:\\\"0\\\"\"v2: \"type:VARBINARY value:\\\"1\\\"\"v3: \"type:VARBINARY value:\\\"0\\\"\"v4: \"type:VARBINARY value:\\\"0\\\"\"v5: \"type:VARBINARY value:\\\"0\\\"\"}`, 52 `target: keep3rv1.-.primary: vttablet: Duplicate entry '<val>' for key' (errno 1062) (sqlstate 23000) (CallerID: admin)`, 53 // should be: 54 // `target: keep3rv1.-.primary: vttablet: Duplicate entry '<val>' for key 'column ' for key' (errno 1062) (sqlstate 23000) (CallerID: admin)`, 55 }, 56 { 57 "truncates after 'syntax error'", 58 `syntax error at position 42 near 'WHERE'`, 59 `syntax error at position 42`, 60 }, 61 { 62 "removes Sql after colon", 63 `target: workmade.-.primary: vttablet: Data too long for column 'signupIpAddress' at row 1 (errno 1406) (sqlstate 22001) (CallerID: admin): Sql: \"insert into User(id, firstName, lastName, password, phone, email, citizenship, dob, address_street1, address_city, address_state, address_zip, address_country, piiSsn, signupIpAddress, createdAt, updatedAt) values (:v1, :v2, :v3, :v4, :v5, :v6, :v7, :v8, :v9, :v10, :v11, :v12, :v13, :v14, :v15, :v16, :v17)\", BindVars: {v1: \"type:VARBINARY value:\\\"$guid\\\"\"v10: \"type:VARBINARY value:\\\"$city\\\"\"v11: \"type:VARBINARY value:\\\"$state\\\"\"v12: \"type:VARBINARY value:\\\"$zip\\\"\"v13: \"type:VARBINARY value:\\\"$country\\\"\"v14: \"type:VARBINARY value:\\\"$tok\\\"\"v15: \"type:VARBINARY value:\\\"$ips\\\"\"v16: \"type:VARCHAR value:\\\"$ts\\\"\"v17: \"type:VARCHAR value:\\\"$ts\\\"\"v2: \"type:VARBINARY value:\\\"$firstname\\\"\"v3: \"type:VARBINARY value:\\\"$lastname\\\"\"v4: \"type:VARBINARY value:\\\"$blob\\\"\"v5: \"type:VARBINARY value:\\\"$phone\\\"\"v6: \"type:VARBINARY value:\\\"$email\\\"\"v7: \"type:VARBINARY value:\\\"$country\\\"\"v8: \"type:VARBINARY value:\\\"$birthday\\\"\"v9: \"type:VARBINARY value:\\\"$address\\\"\"}: 1`, 64 `target: workmade.-.primary: vttablet: Data too long for column 'signupIpAddress' at row 1 (errno 1406) (sqlstate 22001) (CallerID: admin)`, 65 }, 66 { 67 "removes Sql after comma", 68 `target: workmade.-.primary: vttablet: Data too long for column 'signupIpAddress' at row 1 (errno 1406) (sqlstate 22001) (CallerID: admin): Junk: "whatever", Sql: \"insert into User(id, firstName, lastName, password, phone, email, citizenship, dob, address_street1, address_city, address_state, address_zip, address_country, piiSsn, signupIpAddress, createdAt, updatedAt) values (:v1, :v2, :v3, :v4, :v5, :v6, :v7, :v8, :v9, :v10, :v11, :v12, :v13, :v14, :v15, :v16, :v17)\", BindVars: {v1: \"type:VARBINARY value:\\\"$guid\\\"\"v10: \"type:VARBINARY value:\\\"$city\\\"\"v11: \"type:VARBINARY value:\\\"$state\\\"\"v12: \"type:VARBINARY value:\\\"$zip\\\"\"v13: \"type:VARBINARY value:\\\"$country\\\"\"v14: \"type:VARBINARY value:\\\"$tok\\\"\"v15: \"type:VARBINARY value:\\\"$ips\\\"\"v16: \"type:VARCHAR value:\\\"$ts\\\"\"v17: \"type:VARCHAR value:\\\"$ts\\\"\"v2: \"type:VARBINARY value:\\\"$firstname\\\"\"v3: \"type:VARBINARY value:\\\"$lastname\\\"\"v4: \"type:VARBINARY value:\\\"$blob\\\"\"v5: \"type:VARBINARY value:\\\"$phone\\\"\"v6: \"type:VARBINARY value:\\\"$email\\\"\"v7: \"type:VARBINARY value:\\\"$country\\\"\"v8: \"type:VARBINARY value:\\\"$birthday\\\"\"v9: \"type:VARBINARY value:\\\"$address\\\"\"}: 1`, 69 `target: workmade.-.primary: vttablet: Data too long for column 'signupIpAddress' at row 1 (errno 1406) (sqlstate 22001) (CallerID: admin): Junk: "whatever"`, 70 }, 71 { 72 "removes BindVars after colon", 73 `target: workmade.-.primary: vttablet: Data too long for column 'signupIpAddress' at row 1 (errno 1406) (sqlstate 22001) (CallerID: admin): BindVars: {v1: \"type:VARBINARY value:\\\"$guid\\\"\"v10: \"type:VARBINARY value:\\\"$city\\\"\"v11: \"type:VARBINARY value:\\\"$state\\\"\"v12: \"type:VARBINARY value:\\\"$zip\\\"\"v13: \"type:VARBINARY value:\\\"$country\\\"\"v14: \"type:VARBINARY value:\\\"$tok\\\"\"v15: \"type:VARBINARY value:\\\"$ips\\\"\"v16: \"type:VARCHAR value:\\\"$ts\\\"\"v17: \"type:VARCHAR value:\\\"$ts\\\"\"v2: \"type:VARBINARY value:\\\"$firstname\\\"\"v3: \"type:VARBINARY value:\\\"$lastname\\\"\"v4: \"type:VARBINARY value:\\\"$blob\\\"\"v5: \"type:VARBINARY value:\\\"$phone\\\"\"v6: \"type:VARBINARY value:\\\"$email\\\"\"v7: \"type:VARBINARY value:\\\"$country\\\"\"v8: \"type:VARBINARY value:\\\"$birthday\\\"\"v9: \"type:VARBINARY value:\\\"$address\\\"\"}: 1`, 74 `target: workmade.-.primary: vttablet: Data too long for column 'signupIpAddress' at row 1 (errno 1406) (sqlstate 22001) (CallerID: admin)`, 75 }, 76 { 77 "removes BindVars after comma", 78 `target: workmade.-.primary: vttablet: Data too long for column 'signupIpAddress' at row 1 (errno 1406) (sqlstate 22001) (CallerID: admin): Junk: "whatever", BindVars: {v1: \"type:VARBINARY value:\\\"$guid\\\"\"v10: \"type:VARBINARY value:\\\"$city\\\"\"v11: \"type:VARBINARY value:\\\"$state\\\"\"v12: \"type:VARBINARY value:\\\"$zip\\\"\"v13: \"type:VARBINARY value:\\\"$country\\\"\"v14: \"type:VARBINARY value:\\\"$tok\\\"\"v15: \"type:VARBINARY value:\\\"$ips\\\"\"v16: \"type:VARCHAR value:\\\"$ts\\\"\"v17: \"type:VARCHAR value:\\\"$ts\\\"\"v2: \"type:VARBINARY value:\\\"$firstname\\\"\"v3: \"type:VARBINARY value:\\\"$lastname\\\"\"v4: \"type:VARBINARY value:\\\"$blob\\\"\"v5: \"type:VARBINARY value:\\\"$phone\\\"\"v6: \"type:VARBINARY value:\\\"$email\\\"\"v7: \"type:VARBINARY value:\\\"$country\\\"\"v8: \"type:VARBINARY value:\\\"$birthday\\\"\"v9: \"type:VARBINARY value:\\\"$address\\\"\"}: 1`, 79 `target: workmade.-.primary: vttablet: Data too long for column 'signupIpAddress' at row 1 (errno 1406) (sqlstate 22001) (CallerID: admin): Junk: "whatever"`, 80 }, 81 { 82 "truncates very long strings", 83 "Doloribus quo ullam labore nostrum nihil dolore nemo. Ad molestiae ab at dolores et. Iusto adipisci tempora et quia blanditiis et. Velit alias eos quia et velit. Impedit ipsa itaque facilis repellendus. Quidem fuga sit voluptas minus. Neque amet et necessitatibus voluptatum. Voluptatem eum consequatur et dolor. Nulla deserunt quia cum ea hic architecto. Eum et sed quo et officia nostrum eos. Nam quisquam et dolor repellat. Ea aperiam iste et. Sint commodi non ut non occaecati velit. Architecto et fuga alias blanditiis consequatur qui ipsa magnam. Ea velit mollitia sed eligendi dolor et. Commodi et non sint optio asperiores. Et dolores id corrupti voluptatum quasi voluptatem ipsam voluptatem. Dolorum et natus fugit. Ad id ea laudantium adipisci molestiae ratione eum quisquam.", 84 "Doloribus quo ullam labore nostrum nihil dolore nemo. Ad molestiae ab at dolores et. Iusto adipisci tempora et quia blanditiis et. Velit alias eos quia et velit. Impedit ipsa itaque facilis repellendus. Quidem fuga sit voluptas minus. Neque amet et necessitatibus voluptatum. Voluptatem eum consequatur et dolor. Nulla deserunt quia cum ea hic architecto. Eum et sed quo et officia ", 85 }, 86 { 87 "removes 'incorrect' string value literals", 88 "target: targ.-.primary: vttablet: rpc error: code = InvalidArgument desc = Incorrect string value: '\\xC3?A' for column 'name' at row 253 (errno 1366) (sqlstate HY000) (CallerID: admin)", 89 "target: targ.-.primary: vttablet: rpc error: code = InvalidArgument desc = Incorrect string value: '<val>' for column 'name' at row 253 (errno 1366) (sqlstate HY000) (CallerID: admin)", 90 }, 91 { 92 "removes 'incorrect' datetime value literals", 93 "target: targ.-.primary: vttablet: rpc error: code = InvalidArgument desc = Incorrect datetime value: '1991-09-29 NULL' for column 'startdate' at row 1 (errno 1292) (sqlstate 22007) (CallerID: admin)", 94 "target: targ.-.primary: vttablet: rpc error: code = InvalidArgument desc = Incorrect datetime value: '<val>' for column 'startdate' at row 1 (errno 1292) (sqlstate 22007) (CallerID: admin)", 95 }, 96 } 97 for _, tc := range testCases { 98 t.Run(tc.name, func(t *testing.T) { 99 op := NormalizeError(tc.input) 100 assert.Equal(t, tc.output, op) 101 }) 102 } 103 }