pgsqlarbiter 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/pgsqlarbiter/analysis.rb +20 -3
- data/lib/pgsqlarbiter/analyzer.rb +1009 -356
- data/lib/pgsqlarbiter/arbiter.rb +81 -14
- data/lib/pgsqlarbiter/default_query_functions.rb +11 -3
- data/lib/pgsqlarbiter/default_query_types.rb +55 -0
- data/lib/pgsqlarbiter/error.rb +5 -0
- data/lib/pgsqlarbiter/keywords.rb +525 -58
- data/lib/pgsqlarbiter/lexer.rb +303 -216
- data/lib/pgsqlarbiter/parser.rb +55275 -0
- data/lib/pgsqlarbiter/schema.rb +235 -0
- data/lib/pgsqlarbiter/token.rb +14 -23
- data/lib/pgsqlarbiter/verdict.rb +32 -4
- data/lib/pgsqlarbiter/version.rb +1 -1
- data/lib/pgsqlarbiter.rb +34 -11
- metadata +19 -2
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "set"
|
|
4
|
+
|
|
5
|
+
module Pgsqlarbiter
|
|
6
|
+
# What exists in the database: the columns of every table and view, and
|
|
7
|
+
# optionally the result columns of functions that are used in +FROM+.
|
|
8
|
+
#
|
|
9
|
+
# The analyzer needs this to tell a column from a function. PostgreSQL treats
|
|
10
|
+
# +x.name+ as the call +name(x)+ when +x+ has no column +name+, so without the
|
|
11
|
+
# column lists a function call can hide in what looks like a column reference.
|
|
12
|
+
#
|
|
13
|
+
# A schema describes the database; it grants nothing. What a query may use is
|
|
14
|
+
# decided by the {Arbiter}'s whitelists.
|
|
15
|
+
#
|
|
16
|
+
# Every column of a table must be listed, including columns that no query will
|
|
17
|
+
# be allowed to use. Names are matched exactly as queries write them, after
|
|
18
|
+
# PostgreSQL's case folding: +users+ and +public.users+ are separate entries.
|
|
19
|
+
#
|
|
20
|
+
# Keep the schema current. A column that is missing from it is denied, which
|
|
21
|
+
# is safe. A column that is listed but no longer exists is treated as a
|
|
22
|
+
# column while PostgreSQL would look for a function of that name.
|
|
23
|
+
#
|
|
24
|
+
# @example
|
|
25
|
+
# schema = Pgsqlarbiter::Schema.new(
|
|
26
|
+
# {"users" => %w[id name email password_hash], "orders" => %w[id user_id total]},
|
|
27
|
+
# functions: {"monthly_report" => %w[month total], "active_user_ids" => :scalar}
|
|
28
|
+
# )
|
|
29
|
+
class Schema
|
|
30
|
+
# Columns that every table has.
|
|
31
|
+
SYSTEM_COLUMNS = %w[tableoid xmin cmin xmax cmax ctid].freeze
|
|
32
|
+
|
|
33
|
+
# A query that describes the connected database as one JSON document for
|
|
34
|
+
# {Schema.load}. Run it as the role, and with the +search_path+, that the
|
|
35
|
+
# restricted queries will use.
|
|
36
|
+
#
|
|
37
|
+
# Tables, views, materialized views and foreign tables outside the system
|
|
38
|
+
# schemas are listed as +schema.name+, and also as the bare +name+ when the
|
|
39
|
+
# +search_path+ finds them that way.
|
|
40
|
+
#
|
|
41
|
+
# Functions are listed with the columns they return in +FROM+, PostgreSQL's
|
|
42
|
+
# own functions included, as +schema.name+ and as the bare +name+. A name
|
|
43
|
+
# is listed only when every function it can reach returns the same columns.
|
|
44
|
+
# PostgreSQL chooses among the functions of one name by the arguments of
|
|
45
|
+
# each call, so nothing can be said about a name whose functions differ, or
|
|
46
|
+
# about one that returns whatever type it is given (+unnest+).
|
|
47
|
+
#
|
|
48
|
+
# Names containing a dot, and columns named +*+, are left out because
|
|
49
|
+
# pgsqlarbiter cannot represent them; queries that use them are denied.
|
|
50
|
+
QUERY = <<~SQL
|
|
51
|
+
WITH rels AS (
|
|
52
|
+
SELECT c.oid, n.nspname, c.relname, pg_catalog.pg_table_is_visible(c.oid) AS visible,
|
|
53
|
+
(SELECT coalesce(pg_catalog.jsonb_agg(a.attname ORDER BY a.attnum), '[]'::jsonb)
|
|
54
|
+
FROM pg_catalog.pg_attribute a
|
|
55
|
+
WHERE a.attrelid = c.oid AND a.attnum > 0 AND NOT a.attisdropped
|
|
56
|
+
AND a.attname <> '*' AND pg_catalog.strpos(a.attname, '.') = 0) AS columns
|
|
57
|
+
FROM pg_catalog.pg_class c
|
|
58
|
+
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
|
|
59
|
+
WHERE c.relkind IN ('r', 'v', 'm', 'f', 'p')
|
|
60
|
+
AND n.nspname <> 'pg_catalog' AND n.nspname <> 'information_schema'
|
|
61
|
+
AND n.nspname !~ '^pg_toast' AND n.nspname !~ '^pg_temp'
|
|
62
|
+
AND pg_catalog.strpos(n.nspname, '.') = 0 AND pg_catalog.strpos(c.relname, '.') = 0
|
|
63
|
+
),
|
|
64
|
+
funcs AS (
|
|
65
|
+
SELECT n.nspname, p.proname, pg_catalog.pg_function_is_visible(p.oid) AS visible,
|
|
66
|
+
CASE
|
|
67
|
+
-- Several OUT or TABLE parameters: a row of those columns.
|
|
68
|
+
WHEN o.outputs > 1 THEN o.names
|
|
69
|
+
-- A composite type: its attributes.
|
|
70
|
+
WHEN t.typtype = 'c'
|
|
71
|
+
THEN (SELECT CASE WHEN bool_and(a.attname <> '*' AND pg_catalog.strpos(a.attname, '.') = 0)
|
|
72
|
+
THEN pg_catalog.jsonb_agg(a.attname ORDER BY a.attnum) END
|
|
73
|
+
FROM pg_catalog.pg_attribute a
|
|
74
|
+
WHERE a.attrelid = t.typrelid AND a.attnum > 0 AND NOT a.attisdropped)
|
|
75
|
+
-- A single value: named after its one OUT parameter if that has
|
|
76
|
+
-- a name, otherwise after the alias. A polymorphic or record
|
|
77
|
+
-- result is neither known to be a value nor a row.
|
|
78
|
+
WHEN t.typtype IN ('b', 'e', 'r', 'm')
|
|
79
|
+
THEN CASE WHEN o.outputs = 1 AND o.names IS NOT NULL THEN o.names
|
|
80
|
+
WHEN o.outputs = 1 AND o.unnamed = 0 THEN NULL
|
|
81
|
+
ELSE '"scalar"'::jsonb END
|
|
82
|
+
END AS shape
|
|
83
|
+
FROM pg_catalog.pg_proc p
|
|
84
|
+
JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace
|
|
85
|
+
JOIN pg_catalog.pg_type t ON t.oid = p.prorettype
|
|
86
|
+
CROSS JOIN LATERAL (
|
|
87
|
+
SELECT count(*) AS outputs,
|
|
88
|
+
count(*) FILTER (WHERE coalesce(x.name, '') = '') AS unnamed,
|
|
89
|
+
CASE WHEN count(*) > 0
|
|
90
|
+
AND bool_and(coalesce(x.name, '') <> '' AND x.name <> '*' AND pg_catalog.strpos(x.name, '.') = 0)
|
|
91
|
+
THEN pg_catalog.jsonb_agg(x.name ORDER BY x.ord) END AS names
|
|
92
|
+
FROM ROWS FROM (pg_catalog.unnest(p.proargmodes), pg_catalog.unnest(p.proargnames))
|
|
93
|
+
WITH ORDINALITY AS x(mode, name, ord)
|
|
94
|
+
WHERE x.mode IN ('o', 'b', 't')
|
|
95
|
+
) o
|
|
96
|
+
WHERE p.prokind = 'f' AND pg_catalog.strpos(p.proname, '.') = 0
|
|
97
|
+
AND n.nspname !~ '^pg_toast' AND n.nspname !~ '^pg_temp'
|
|
98
|
+
),
|
|
99
|
+
named_funcs AS (
|
|
100
|
+
SELECT nspname || '.' || proname AS name, shape FROM funcs WHERE pg_catalog.strpos(nspname, '.') = 0
|
|
101
|
+
UNION ALL
|
|
102
|
+
SELECT proname, shape FROM funcs WHERE visible
|
|
103
|
+
),
|
|
104
|
+
agreed_funcs AS (
|
|
105
|
+
SELECT name, min(shape::text)::jsonb AS shape
|
|
106
|
+
FROM named_funcs
|
|
107
|
+
GROUP BY name
|
|
108
|
+
HAVING count(*) = count(shape) AND count(DISTINCT shape::text) = 1
|
|
109
|
+
)
|
|
110
|
+
SELECT pg_catalog.jsonb_build_object(
|
|
111
|
+
'tables', coalesce((SELECT pg_catalog.jsonb_object_agg(name, columns) FROM (
|
|
112
|
+
SELECT nspname || '.' || relname AS name, columns FROM rels
|
|
113
|
+
UNION ALL
|
|
114
|
+
SELECT relname, columns FROM rels WHERE visible) t), '{}'::jsonb),
|
|
115
|
+
'functions', coalesce((SELECT pg_catalog.jsonb_object_agg(name, shape) FROM agreed_funcs), '{}'::jsonb)
|
|
116
|
+
)::text
|
|
117
|
+
SQL
|
|
118
|
+
|
|
119
|
+
# Build a schema from the JSON document produced by {QUERY}.
|
|
120
|
+
#
|
|
121
|
+
# @param json [String, Hash] the document, or the Hash parsed from it
|
|
122
|
+
# @return [Schema]
|
|
123
|
+
def self.load(json)
|
|
124
|
+
if json.is_a?(String)
|
|
125
|
+
require "json"
|
|
126
|
+
json = JSON.parse(json)
|
|
127
|
+
end
|
|
128
|
+
raise ArgumentError, "schema document must be an object" unless json.is_a?(Hash)
|
|
129
|
+
|
|
130
|
+
functions = (json["functions"] || {}).transform_values do |shape|
|
|
131
|
+
if shape.nil?
|
|
132
|
+
:unknown
|
|
133
|
+
else
|
|
134
|
+
(shape == "scalar") ? :scalar : shape
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
new(json["tables"] || {}, functions: functions)
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# @param tables [Hash{String => Array<String>}] every column of every table
|
|
141
|
+
# and view, by table name
|
|
142
|
+
# @param functions [Hash{String => Array<String>, Symbol}] for functions
|
|
143
|
+
# used in +FROM+, PostgreSQL's own included: the names of the columns the
|
|
144
|
+
# function returns, +:scalar+ when it returns a single value that takes
|
|
145
|
+
# the name of its alias, or +:unknown+. Nothing is assumed about a
|
|
146
|
+
# function that is not listed: it can still be used in +FROM+, but its
|
|
147
|
+
# columns can only be referred to through a column alias list
|
|
148
|
+
# (+FROM f() AS t(a, b)+). A name stands for every function a call of it
|
|
149
|
+
# can reach, so declare columns only when all of them return the same;
|
|
150
|
+
# {QUERY} works that out from the catalog
|
|
151
|
+
# @raise [ArgumentError] if a name cannot be represented
|
|
152
|
+
def initialize(tables, functions: {})
|
|
153
|
+
raise ArgumentError, "tables must be a Hash of table name to column names" unless tables.is_a?(Hash)
|
|
154
|
+
raise ArgumentError, "functions must be a Hash" unless functions.is_a?(Hash)
|
|
155
|
+
|
|
156
|
+
@columns = {}
|
|
157
|
+
@column_sets = {}
|
|
158
|
+
tables.each do |table, columns|
|
|
159
|
+
table = name!(table, "table")
|
|
160
|
+
raise ArgumentError, "columns of #{table.inspect} must be an Array" unless columns.respond_to?(:to_ary)
|
|
161
|
+
|
|
162
|
+
columns = columns.to_ary.map { |column| column_name!(column, table) }.freeze
|
|
163
|
+
@columns[table] = columns
|
|
164
|
+
@column_sets[table] = Set.new(columns).merge(SYSTEM_COLUMNS).freeze
|
|
165
|
+
end
|
|
166
|
+
@functions = {}
|
|
167
|
+
functions.each do |function, shape|
|
|
168
|
+
function = name!(function, "function")
|
|
169
|
+
@functions[function] =
|
|
170
|
+
if shape == :scalar || shape == :unknown
|
|
171
|
+
shape
|
|
172
|
+
elsif shape.respond_to?(:to_ary)
|
|
173
|
+
shape.to_ary.map { |column| column_name!(column, function) }.freeze
|
|
174
|
+
else
|
|
175
|
+
raise ArgumentError, "result of function #{function.inspect} must be :scalar, :unknown or an Array of column names"
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
@columns.freeze
|
|
179
|
+
@column_sets.freeze
|
|
180
|
+
@functions.freeze
|
|
181
|
+
freeze
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
# @return [Array<String>] the names of all tables
|
|
185
|
+
def tables
|
|
186
|
+
@columns.keys
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
# @return [Boolean] whether the table is described by this schema
|
|
190
|
+
def table?(table)
|
|
191
|
+
@columns.key?(table)
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
# @return [Array<String>, nil] the table's columns as given, or +nil+ for
|
|
195
|
+
# an unknown table
|
|
196
|
+
def columns(table)
|
|
197
|
+
@columns[table]
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
# @return [Set<String>, nil] the table's columns together with the system
|
|
201
|
+
# columns every table has, or +nil+ for an unknown table
|
|
202
|
+
def all_columns(table)
|
|
203
|
+
@column_sets[table]
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
# @return [Boolean] whether the table has the column
|
|
207
|
+
def column?(table, column)
|
|
208
|
+
@column_sets[table]&.include?(column) || false
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
# @return [Array<String>, Symbol, nil] the declared result of a function:
|
|
212
|
+
# column names, +:scalar+, +:unknown+, or +nil+ when it is not listed
|
|
213
|
+
def function_columns(function)
|
|
214
|
+
@functions[function]
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
private
|
|
218
|
+
|
|
219
|
+
def name!(name, kind)
|
|
220
|
+
name = name.to_s if name.is_a?(Symbol)
|
|
221
|
+
raise ArgumentError, "#{kind} name must be a non-empty String, got #{name.inspect}" unless name.is_a?(String) && !name.empty?
|
|
222
|
+
|
|
223
|
+
name.dup.freeze
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
def column_name!(column, owner)
|
|
227
|
+
column = name!(column, "column")
|
|
228
|
+
if column == "*" || column.include?(".")
|
|
229
|
+
raise ArgumentError, "column #{column.inspect} of #{owner.inspect} cannot be represented; leave it out " \
|
|
230
|
+
"of the schema and queries that use it will be denied"
|
|
231
|
+
end
|
|
232
|
+
column
|
|
233
|
+
end
|
|
234
|
+
end
|
|
235
|
+
end
|
data/lib/pgsqlarbiter/token.rb
CHANGED
|
@@ -4,31 +4,22 @@ module Pgsqlarbiter
|
|
|
4
4
|
# Immutable token produced by the {Lexer}.
|
|
5
5
|
#
|
|
6
6
|
# @!attribute [r] type
|
|
7
|
-
# @return [Symbol]
|
|
7
|
+
# @return [Symbol, String] the grammar terminal: a Symbol named as in
|
|
8
|
+
# PostgreSQL's grammar (+:SELECT+, +:IDENT+, +:SCONST+, ...) or, for
|
|
9
|
+
# single-character tokens, the character itself (+"("+, +","+, ...)
|
|
8
10
|
# @!attribute [r] value
|
|
9
|
-
# @return [String
|
|
11
|
+
# @return [String] the token's value: the folded identifier, the lower-case
|
|
12
|
+
# keyword, the literal's content, or the operator text
|
|
10
13
|
# @!attribute [r] position
|
|
11
|
-
# @return [Integer]
|
|
14
|
+
# @return [Integer] byte offset of the token in the original SQL string
|
|
12
15
|
Token = Data.define(:type, :value, :position)
|
|
13
16
|
|
|
14
|
-
#
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
LPAREN = :lparen
|
|
23
|
-
RPAREN = :rparen
|
|
24
|
-
LBRACKET = :lbracket
|
|
25
|
-
RBRACKET = :rbracket
|
|
26
|
-
COMMA = :comma
|
|
27
|
-
DOT = :dot
|
|
28
|
-
SEMICOLON = :semicolon
|
|
29
|
-
STAR = :star
|
|
30
|
-
TYPECAST = :typecast
|
|
31
|
-
OP = :op
|
|
32
|
-
EOF = :eof
|
|
33
|
-
end
|
|
17
|
+
# A node of the concrete syntax tree built by the {Parser}: one per grammar
|
|
18
|
+
# production that was applied.
|
|
19
|
+
#
|
|
20
|
+
# @!attribute [r] name
|
|
21
|
+
# @return [Symbol] the production's nonterminal, named as in PostgreSQL's grammar
|
|
22
|
+
# @!attribute [r] children
|
|
23
|
+
# @return [Array<Node, Token>] the production's right-hand side, in order
|
|
24
|
+
Node = Struct.new(:name, :children)
|
|
34
25
|
end
|
data/lib/pgsqlarbiter/verdict.rb
CHANGED
|
@@ -11,15 +11,33 @@ module Pgsqlarbiter
|
|
|
11
11
|
# @!attribute [r] allowed
|
|
12
12
|
# @return [Boolean] +true+ when the query passes all checks
|
|
13
13
|
# @!attribute [r] statement_type_allowed
|
|
14
|
-
# @return [Boolean] +true+ when
|
|
14
|
+
# @return [Boolean] +true+ when every statement type the query needs is in
|
|
15
|
+
# the whitelist
|
|
15
16
|
# @!attribute [r] statement_type
|
|
16
|
-
# @return [Symbol] the
|
|
17
|
+
# @return [Symbol] the type of the statement itself
|
|
18
|
+
# @!attribute [r] disallowed_statement_types
|
|
19
|
+
# @return [Array<Symbol>] statement types the query needs that are not
|
|
20
|
+
# whitelisted. This can include types other than {#statement_type}: a
|
|
21
|
+
# +SELECT+ with a +DELETE+ in its +WITH+ clause needs +:delete+
|
|
17
22
|
# @!attribute [r] disallowed_tables
|
|
18
23
|
# @return [Array<String>] tables referenced by the query that are not whitelisted
|
|
24
|
+
# @!attribute [r] disallowed_columns
|
|
25
|
+
# @return [Array<String>] columns of allowed tables that the query uses and
|
|
26
|
+
# that are not whitelisted, as +table.column+. +table.*+ means the query
|
|
27
|
+
# uses every column of a table whose columns are restricted
|
|
19
28
|
# @!attribute [r] disallowed_functions
|
|
20
29
|
# @return [Array<String>] functions called by the query that are not whitelisted
|
|
30
|
+
# @!attribute [r] disallowed_types
|
|
31
|
+
# @return [Array<String>] types named by the query that are not whitelisted
|
|
21
32
|
Verdict = Data.define(:allowed, :statement_type_allowed, :statement_type,
|
|
22
|
-
|
|
33
|
+
:disallowed_statement_types, :disallowed_tables, :disallowed_columns, :disallowed_functions,
|
|
34
|
+
:disallowed_types) do
|
|
35
|
+
def initialize(allowed:, statement_type_allowed:, statement_type:, disallowed_tables:, disallowed_functions:,
|
|
36
|
+
disallowed_statement_types: nil, disallowed_types: [].freeze, disallowed_columns: [].freeze)
|
|
37
|
+
disallowed_statement_types ||= (statement_type_allowed ? [] : [statement_type]).freeze
|
|
38
|
+
super
|
|
39
|
+
end
|
|
40
|
+
|
|
23
41
|
alias_method :allowed?, :allowed
|
|
24
42
|
alias_method :statement_type_allowed?, :statement_type_allowed
|
|
25
43
|
|
|
@@ -28,9 +46,19 @@ module Pgsqlarbiter
|
|
|
28
46
|
# @return [Array<String>] frozen list of reason strings
|
|
29
47
|
def reasons
|
|
30
48
|
r = []
|
|
31
|
-
r << "statement type :#{
|
|
49
|
+
disallowed_statement_types.each { |t| r << "statement type :#{t} is not allowed" }
|
|
32
50
|
disallowed_tables.each { |t| r << "table #{t.inspect} is not allowed" }
|
|
51
|
+
disallowed_columns.each do |c|
|
|
52
|
+
table, _, column = c.rpartition(".")
|
|
53
|
+
r << if column == "*"
|
|
54
|
+
"using every column of table #{table.inspect} (*, a whole-row reference, or an implicit column list) " \
|
|
55
|
+
"is not allowed because its columns are restricted"
|
|
56
|
+
else
|
|
57
|
+
"column #{c.inspect} is not allowed"
|
|
58
|
+
end
|
|
59
|
+
end
|
|
33
60
|
disallowed_functions.each { |f| r << "function #{f.inspect} is not allowed" }
|
|
61
|
+
disallowed_types.each { |t| r << "type #{t.inspect} is not allowed" }
|
|
34
62
|
r.freeze
|
|
35
63
|
end
|
|
36
64
|
end
|
data/lib/pgsqlarbiter/version.rb
CHANGED
data/lib/pgsqlarbiter.rb
CHANGED
|
@@ -5,8 +5,11 @@ require_relative "pgsqlarbiter/error"
|
|
|
5
5
|
require_relative "pgsqlarbiter/token"
|
|
6
6
|
require_relative "pgsqlarbiter/keywords"
|
|
7
7
|
require_relative "pgsqlarbiter/analysis"
|
|
8
|
+
require_relative "pgsqlarbiter/schema"
|
|
8
9
|
require_relative "pgsqlarbiter/default_query_functions"
|
|
10
|
+
require_relative "pgsqlarbiter/default_query_types"
|
|
9
11
|
require_relative "pgsqlarbiter/lexer"
|
|
12
|
+
require_relative "pgsqlarbiter/parser"
|
|
10
13
|
require_relative "pgsqlarbiter/analyzer"
|
|
11
14
|
require_relative "pgsqlarbiter/verdict"
|
|
12
15
|
require_relative "pgsqlarbiter/arbiter"
|
|
@@ -17,15 +20,19 @@ require_relative "pgsqlarbiter/arbiter"
|
|
|
17
20
|
# single-statement DML queries are executed and all referenced tables, views, and
|
|
18
21
|
# functions are whitelisted.
|
|
19
22
|
module Pgsqlarbiter
|
|
20
|
-
# Analyze a SQL query and extract its statement type, referenced tables
|
|
23
|
+
# Analyze a SQL query and extract its statement type(s), referenced tables and columns,
|
|
24
|
+
# function calls, and type names.
|
|
21
25
|
#
|
|
22
26
|
# @param sql [String] the SQL query to analyze
|
|
23
|
-
# @
|
|
24
|
-
# @
|
|
27
|
+
# @param schema [Schema] the tables and columns that exist in the database
|
|
28
|
+
# @return [Analysis] analysis result
|
|
29
|
+
# @raise [LexError] if the SQL cannot be tokenized
|
|
30
|
+
# @raise [ParseError] if the SQL is not valid, supported syntax
|
|
25
31
|
# @raise [MultipleStatementsError] if the SQL contains more than one statement
|
|
32
|
+
# @raise [UnresolvedNameError] if a column or table alias cannot be resolved
|
|
26
33
|
# @raise [DisallowedStatementError] if the statement type is not a supported DML type
|
|
27
|
-
def self.analyze(sql)
|
|
28
|
-
Analyzer.new.analyze(sql)
|
|
34
|
+
def self.analyze(sql, schema:)
|
|
35
|
+
Analyzer.new(schema).analyze(sql)
|
|
29
36
|
end
|
|
30
37
|
|
|
31
38
|
# Judge a SQL query against the given restrictions, returning a {Verdict} that
|
|
@@ -35,18 +42,26 @@ module Pgsqlarbiter
|
|
|
35
42
|
# checks with the same rules, prefer creating an {Arbiter} directly.
|
|
36
43
|
#
|
|
37
44
|
# @param sql [String] the SQL query to judge
|
|
38
|
-
# @param
|
|
45
|
+
# @param schema [Schema] the tables and columns that exist in the database
|
|
46
|
+
# @param allowed_tables [Array<String>, Hash] allowed table and view names, or a Hash of
|
|
47
|
+
# table name to +:all+ or the allowed column names
|
|
39
48
|
# @param allowed_statement_types [Array<Symbol>] allowed statement types
|
|
40
49
|
# (default: +[:select]+). Valid types: +:select+, +:insert+, +:update+,
|
|
41
50
|
# +:delete+, +:merge+, +:values+
|
|
42
51
|
# @param allowed_functions [Set<String>, Array<String>] allowed function names
|
|
43
52
|
# (default: {DEFAULT_QUERY_FUNCTIONS})
|
|
53
|
+
# @param allowed_types [Set<String>, Array<String>] allowed type names
|
|
54
|
+
# (default: {DEFAULT_QUERY_TYPES})
|
|
44
55
|
# @return [Verdict] detailed result with {Verdict#allowed?}, individual check
|
|
45
56
|
# results, and {Verdict#reasons}
|
|
57
|
+
# @raise [ArgumentError] if a whitelisted table or column is not in the schema
|
|
58
|
+
# @raise [UnresolvedNameError] if a column or table alias cannot be resolved
|
|
46
59
|
# @raise [MultipleStatementsError] if the SQL contains more than one statement
|
|
47
60
|
# @raise [DisallowedStatementError] if the statement type is not a supported DML type
|
|
48
|
-
def self.judge(sql, allowed_tables:, allowed_statement_types: [:select],
|
|
49
|
-
|
|
61
|
+
def self.judge(sql, schema:, allowed_tables:, allowed_statement_types: [:select],
|
|
62
|
+
allowed_functions: DEFAULT_QUERY_FUNCTIONS, allowed_types: DEFAULT_QUERY_TYPES)
|
|
63
|
+
Arbiter.new(schema: schema, allowed_statement_types: allowed_statement_types, allowed_tables: allowed_tables,
|
|
64
|
+
allowed_functions: allowed_functions, allowed_types: allowed_types).judge(sql)
|
|
50
65
|
end
|
|
51
66
|
|
|
52
67
|
# Check whether a SQL query is allowed under the given restrictions.
|
|
@@ -55,16 +70,24 @@ module Pgsqlarbiter
|
|
|
55
70
|
# checks with the same rules, prefer creating an {Arbiter} directly.
|
|
56
71
|
#
|
|
57
72
|
# @param sql [String] the SQL query to check
|
|
58
|
-
# @param
|
|
73
|
+
# @param schema [Schema] the tables and columns that exist in the database
|
|
74
|
+
# @param allowed_tables [Array<String>, Hash] allowed table and view names, or a Hash of
|
|
75
|
+
# table name to +:all+ or the allowed column names
|
|
59
76
|
# @param allowed_statement_types [Array<Symbol>] allowed statement types
|
|
60
77
|
# (default: +[:select]+). Valid types: +:select+, +:insert+, +:update+,
|
|
61
78
|
# +:delete+, +:merge+, +:values+
|
|
62
79
|
# @param allowed_functions [Set<String>, Array<String>] allowed function names
|
|
63
80
|
# (default: {DEFAULT_QUERY_FUNCTIONS})
|
|
81
|
+
# @param allowed_types [Set<String>, Array<String>] allowed type names
|
|
82
|
+
# (default: {DEFAULT_QUERY_TYPES})
|
|
64
83
|
# @return [Boolean] +true+ if the query is allowed, +false+ otherwise
|
|
84
|
+
# @raise [ArgumentError] if a whitelisted table or column is not in the schema
|
|
85
|
+
# @raise [UnresolvedNameError] if a column or table alias cannot be resolved
|
|
65
86
|
# @raise [MultipleStatementsError] if the SQL contains more than one statement
|
|
66
87
|
# @raise [DisallowedStatementError] if the statement type is not a supported DML type
|
|
67
|
-
def self.allow?(sql, allowed_tables:, allowed_statement_types: [:select],
|
|
68
|
-
|
|
88
|
+
def self.allow?(sql, schema:, allowed_tables:, allowed_statement_types: [:select],
|
|
89
|
+
allowed_functions: DEFAULT_QUERY_FUNCTIONS, allowed_types: DEFAULT_QUERY_TYPES)
|
|
90
|
+
Arbiter.new(schema: schema, allowed_statement_types: allowed_statement_types, allowed_tables: allowed_tables,
|
|
91
|
+
allowed_functions: allowed_functions, allowed_types: allowed_types).allow?(sql)
|
|
69
92
|
end
|
|
70
93
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: pgsqlarbiter
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- pgsqlarbiter contributors
|
|
@@ -23,6 +23,20 @@ dependencies:
|
|
|
23
23
|
- - "~>"
|
|
24
24
|
- !ruby/object:Gem::Version
|
|
25
25
|
version: '5.0'
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: racc
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - '='
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: 1.8.1
|
|
33
|
+
type: :development
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - '='
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: 1.8.1
|
|
26
40
|
- !ruby/object:Gem::Dependency
|
|
27
41
|
name: rake
|
|
28
42
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -48,9 +62,12 @@ files:
|
|
|
48
62
|
- lib/pgsqlarbiter/analyzer.rb
|
|
49
63
|
- lib/pgsqlarbiter/arbiter.rb
|
|
50
64
|
- lib/pgsqlarbiter/default_query_functions.rb
|
|
65
|
+
- lib/pgsqlarbiter/default_query_types.rb
|
|
51
66
|
- lib/pgsqlarbiter/error.rb
|
|
52
67
|
- lib/pgsqlarbiter/keywords.rb
|
|
53
68
|
- lib/pgsqlarbiter/lexer.rb
|
|
69
|
+
- lib/pgsqlarbiter/parser.rb
|
|
70
|
+
- lib/pgsqlarbiter/schema.rb
|
|
54
71
|
- lib/pgsqlarbiter/token.rb
|
|
55
72
|
- lib/pgsqlarbiter/verdict.rb
|
|
56
73
|
- lib/pgsqlarbiter/version.rb
|
|
@@ -74,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
74
91
|
- !ruby/object:Gem::Version
|
|
75
92
|
version: '0'
|
|
76
93
|
requirements: []
|
|
77
|
-
rubygems_version: 4.0.
|
|
94
|
+
rubygems_version: 4.0.20
|
|
78
95
|
specification_version: 4
|
|
79
96
|
summary: SQL query permission system for PostgreSQL
|
|
80
97
|
test_files: []
|