json2sql 1.0.11 → 1.0.13
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/json2sql/{input_policy.rb → query_policy.rb} +42 -34
- data/lib/json2sql/select_model.rb +40 -5
- data/lib/json2sql/version.rb +1 -1
- data/lib/json2sql.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 6282183f873d4f4e32d0c0819c43ffe20ce53dbabd71bb44ba26b362edbaf9d7
|
|
4
|
+
data.tar.gz: 1aa18874f4bbfc9f1ac395b357babb98f3884f577f2fe8d3d81d7f647cbfc2ba
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 7e84fdd24a2efa2eb464d85df3a3ec3b8370d204bb4d64676dcb953e2777bb34d3bc0807eb0e2fc9c65ffb167911ce89a3c3606a7bcb66d20698b0e617801f81
|
|
7
|
+
data.tar.gz: 9b6f1b82866c8ff0d96af65439ed0a2b6f96592ecde63187b1d91ed27a513d92a3f57077c06862e2582f31342c62f18dabed2f08577a067f358760e5e07161c4
|
|
@@ -6,7 +6,9 @@ module Json2sql
|
|
|
6
6
|
# mode: :allow (default) — only tables listed in `tables:` are accessible.
|
|
7
7
|
# Tables absent from `tables:` are blocked entirely.
|
|
8
8
|
# Empty `tables:` = no restriction.
|
|
9
|
-
# :deny — all tables pass
|
|
9
|
+
# :deny — all tables pass. After column filtering, tables
|
|
10
|
+
# with no remaining accessible columns are removed.
|
|
11
|
+
# Same rule applies to children and parents.
|
|
10
12
|
# tables: Per-table configuration Hash (recursive):
|
|
11
13
|
# { table_name => { columns: [...],
|
|
12
14
|
# children: { child_table => { columns: [...], ... } },
|
|
@@ -25,7 +27,7 @@ module Json2sql
|
|
|
25
27
|
# user-supplied values — primary IDOR guard.
|
|
26
28
|
#
|
|
27
29
|
# Usage:
|
|
28
|
-
# policy = Json2sql::
|
|
30
|
+
# policy = Json2sql::QueryPolicy.new(
|
|
29
31
|
# mode: :allow,
|
|
30
32
|
# tables: {
|
|
31
33
|
# orders: {
|
|
@@ -39,7 +41,7 @@ module Json2sql
|
|
|
39
41
|
# safe_input = policy.apply(raw_params)
|
|
40
42
|
# sql = Json2sql::SelectRunner.build(safe_input)
|
|
41
43
|
|
|
42
|
-
class
|
|
44
|
+
class QueryPolicy
|
|
43
45
|
|
|
44
46
|
def initialize(mode: :allow, tables: {})
|
|
45
47
|
|
|
@@ -54,73 +56,79 @@ module Json2sql
|
|
|
54
56
|
def apply(input)
|
|
55
57
|
|
|
56
58
|
input = Json2sql.normalize(input)
|
|
59
|
+
|
|
60
|
+
filter_tables(input, @tables)
|
|
57
61
|
|
|
58
|
-
input
|
|
62
|
+
input.each { |table, params| sanitize_table(params, @tables[table]) }
|
|
59
63
|
|
|
60
|
-
input.
|
|
64
|
+
input.reject! { |_, params| empty_columns?(params) }
|
|
61
65
|
|
|
62
66
|
input
|
|
63
67
|
end
|
|
64
68
|
|
|
65
69
|
private
|
|
66
70
|
|
|
67
|
-
def filter_tables(input)
|
|
68
|
-
|
|
69
|
-
return input if @mode == :deny || @tables.empty?
|
|
70
|
-
|
|
71
|
-
input.select { |table, _| @tables.key?(table) }
|
|
72
|
-
end
|
|
73
|
-
|
|
74
71
|
def sanitize_table(params, config)
|
|
75
72
|
|
|
76
73
|
return unless params.is_a?(Hash)
|
|
77
74
|
|
|
75
|
+
return unless config.is_a?(Hash)
|
|
76
|
+
|
|
78
77
|
filter_columns(params, config)
|
|
79
78
|
|
|
80
79
|
inject_where(params, config)
|
|
81
80
|
|
|
82
81
|
%w[children parents].each do |relation|
|
|
83
82
|
|
|
84
|
-
|
|
83
|
+
filter_relations(params, config, relation)
|
|
85
84
|
|
|
86
|
-
|
|
85
|
+
next unless params[relation].is_a?(Hash)
|
|
87
86
|
|
|
88
|
-
|
|
87
|
+
tables = config[relation].is_a?(Hash) ? config[relation] : {}
|
|
89
88
|
|
|
90
|
-
|
|
89
|
+
params[relation].each { |table, params| sanitize_table(params, tables[table]) }
|
|
91
90
|
|
|
92
|
-
|
|
91
|
+
params[relation].reject! { |_, params| empty_columns?(params) }
|
|
93
92
|
|
|
94
|
-
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
95
|
|
|
96
|
-
|
|
96
|
+
def empty_columns?(params)
|
|
97
97
|
|
|
98
|
-
|
|
98
|
+
params.is_a?(Hash) && params["columns"].is_a?(Array) && params["columns"].empty?
|
|
99
|
+
end
|
|
99
100
|
|
|
100
|
-
|
|
101
|
+
def filter_tables(params, config)
|
|
101
102
|
|
|
102
|
-
|
|
103
|
+
return if @mode == :deny
|
|
103
104
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
105
|
+
return unless params.is_a?(Hash)
|
|
106
|
+
|
|
107
|
+
tables = config.is_a?(Hash) ? config : {}
|
|
107
108
|
|
|
109
|
+
return if tables.empty?
|
|
110
|
+
|
|
111
|
+
params.select! { |table, _| tables.key?(table) }
|
|
112
|
+
end
|
|
113
|
+
|
|
108
114
|
# Filters children/parents relations in :allow mode.
|
|
109
|
-
# Only relations present as keys in config[
|
|
110
|
-
# If config[
|
|
111
|
-
#
|
|
115
|
+
# Only relations present as keys in config[relation] pass through.
|
|
116
|
+
# If config[relation] is absent or not a Hash, relations are untouched.
|
|
117
|
+
# No-op in :deny mode.
|
|
118
|
+
|
|
119
|
+
def filter_relations(params, config, relation)
|
|
112
120
|
|
|
113
|
-
|
|
121
|
+
return if @mode == :deny
|
|
114
122
|
|
|
115
|
-
|
|
123
|
+
param_tables = params[relation]
|
|
116
124
|
|
|
117
|
-
return unless
|
|
125
|
+
return unless param_tables.is_a?(Hash)
|
|
118
126
|
|
|
119
|
-
|
|
127
|
+
config_tables = config[relation]
|
|
120
128
|
|
|
121
|
-
return unless
|
|
129
|
+
return unless config_tables.is_a?(Hash)
|
|
122
130
|
|
|
123
|
-
params[
|
|
131
|
+
params[relation] = param_tables.select { |table, _| config_tables.key?(table) }
|
|
124
132
|
end
|
|
125
133
|
|
|
126
134
|
# Filters "columns" using mode (:allow or :deny).
|
|
@@ -325,15 +325,24 @@ module Json2sql
|
|
|
325
325
|
|
|
326
326
|
function = Sanitizer.keyword(column["function"].to_s)
|
|
327
327
|
|
|
328
|
-
|
|
328
|
+
params = column["params"]
|
|
329
329
|
|
|
330
|
-
|
|
330
|
+
return build_function_cast(params) if function.upcase == "CAST"
|
|
331
|
+
|
|
332
|
+
build_function_default(function, params)
|
|
333
|
+
end
|
|
331
334
|
|
|
332
|
-
|
|
335
|
+
def build_function_default(function, params)
|
|
333
336
|
|
|
334
|
-
|
|
337
|
+
expression = +""
|
|
338
|
+
|
|
339
|
+
expression << function << "("
|
|
340
|
+
|
|
341
|
+
glue = false
|
|
335
342
|
|
|
336
|
-
|
|
343
|
+
if params.is_a?(Array)
|
|
344
|
+
|
|
345
|
+
params.each do |param|
|
|
337
346
|
|
|
338
347
|
next unless param.is_a?(String) || param.is_a?(Symbol)
|
|
339
348
|
|
|
@@ -350,6 +359,32 @@ module Json2sql
|
|
|
350
359
|
expression
|
|
351
360
|
end
|
|
352
361
|
|
|
362
|
+
# Supports function payload: {"function"=>"CAST", "params"=>["id", "CHAR"]}
|
|
363
|
+
# and generic cast targets like SIGNED/UNSIGNED/CHAR.
|
|
364
|
+
|
|
365
|
+
def build_function_cast(params)
|
|
366
|
+
|
|
367
|
+
return +"NULL" unless params.is_a?(Array) && params.length >= 2
|
|
368
|
+
|
|
369
|
+
source = params[0]
|
|
370
|
+
|
|
371
|
+
return +"NULL" unless source.is_a?(String) || source.is_a?(Symbol)
|
|
372
|
+
|
|
373
|
+
cast_type = Sanitizer.keyword(params[1].to_s).upcase
|
|
374
|
+
|
|
375
|
+
return +"NULL" if cast_type.empty?
|
|
376
|
+
|
|
377
|
+
expression = +"CAST("
|
|
378
|
+
|
|
379
|
+
expression << Sanitizer.keyword_wrap(@table) << "."
|
|
380
|
+
|
|
381
|
+
expression << Sanitizer.keyword_wrap(source.to_s)
|
|
382
|
+
|
|
383
|
+
expression << " AS " << cast_type << ")"
|
|
384
|
+
|
|
385
|
+
expression
|
|
386
|
+
end
|
|
387
|
+
|
|
353
388
|
def build_order(params)
|
|
354
389
|
|
|
355
390
|
order = params["order"]
|
data/lib/json2sql/version.rb
CHANGED
data/lib/json2sql.rb
CHANGED
|
@@ -10,7 +10,7 @@ require_relative "json2sql/update_model"
|
|
|
10
10
|
require_relative "json2sql/update_runner"
|
|
11
11
|
require_relative "json2sql/delete_model"
|
|
12
12
|
require_relative "json2sql/delete_runner"
|
|
13
|
-
require_relative "json2sql/
|
|
13
|
+
require_relative "json2sql/query_policy"
|
|
14
14
|
|
|
15
15
|
# Json2sql — SQL builder that generates MySQL/MariaDB query strings from
|
|
16
16
|
# plain Ruby Hashes (or parsed JSON).
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: json2sql
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0.
|
|
4
|
+
version: 1.0.13
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Tiago da Silva
|
|
@@ -49,9 +49,9 @@ files:
|
|
|
49
49
|
- lib/json2sql.rb
|
|
50
50
|
- lib/json2sql/delete_model.rb
|
|
51
51
|
- lib/json2sql/delete_runner.rb
|
|
52
|
-
- lib/json2sql/input_policy.rb
|
|
53
52
|
- lib/json2sql/insert_model.rb
|
|
54
53
|
- lib/json2sql/insert_runner.rb
|
|
54
|
+
- lib/json2sql/query_policy.rb
|
|
55
55
|
- lib/json2sql/sanitizer.rb
|
|
56
56
|
- lib/json2sql/select_model.rb
|
|
57
57
|
- lib/json2sql/select_runner.rb
|