Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pkg/client/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (

var (
// List of keywords that are not allowed in read-only mode
reRestrictedKeywords = regexp.MustCompile(`(?mi)\s?(CREATE|INSERT|UPDATE|DROP|DELETE|TRUNCATE|GRANT|OPEN|IMPORT|COPY)\s`)
reRestrictedKeywords = regexp.MustCompile(`(?mi)\s?(CREATE|INSERT|UPDATE|DROP|DELETE|TRUNCATE|GRANT|OPEN|IMPORT|COPY)\s`)
reRestrictedFunctions = regexp.MustCompile(`(?mi)(pg_cancel_backend|pg_terminate_backend)\s*\(`)

// Comment regular expressions
reSlashComment = regexp.MustCompile(`(?m)/\*.+\*/`)
Expand Down Expand Up @@ -85,7 +86,7 @@ func containsRestrictedKeywords(str string) bool {
str = reSlashComment.ReplaceAllString(str, "")
str = reDashComment.ReplaceAllString(str, "")

return reRestrictedKeywords.MatchString(str)
return reRestrictedKeywords.MatchString(str) || reRestrictedFunctions.MatchString(str)
}

func hasBinary(data string, checkLen int) bool {
Expand Down
26 changes: 26 additions & 0 deletions pkg/client/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,32 @@ func TestGetMajorMinorVersion(t *testing.T) {
}
}

func TestContainsRestrictedKeywords(t *testing.T) {
examples := []struct {
input string
result bool
}{
{"SELECT 1", false},
{"SELECT * FROM users", false},
{"CREATE TABLE foo (id int)", true},
{"INSERT INTO foo VALUES (1)", true},
{"DROP TABLE foo", true},
{"DELETE FROM foo", true},
{"SELECT pg_cancel_backend(1234)", true},
{"SELECT pg_terminate_backend(1234)", true},
{"select pg_cancel_backend( 1234 )", true},
{"select pg_terminate_backend( 1234 )", true},
{"SELECT PG_CANCEL_BACKEND(1234)", true},
{"SELECT PG_TERMINATE_BACKEND(1234)", true},
}

for _, ex := range examples {
t.Run(ex.input, func(t *testing.T) {
assert.Equal(t, ex.result, containsRestrictedKeywords(ex.input))
})
}
}

func TestCheckVersionRequirement(t *testing.T) {
examples := []struct {
client string
Expand Down
Loading