forest_liana 9.22.12 → 9.22.14
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/app/controllers/forest_liana/actions_controller.rb +10 -0
- data/app/services/forest_liana/ability/permission.rb +19 -0
- data/app/services/forest_liana/query_stat_getter.rb +4 -7
- data/app/services/forest_liana/single_record_selection.rb +11 -0
- data/app/services/forest_liana/utils/context_variables_injector.rb +15 -12
- data/app/services/forest_liana/utils/live_query_injector.rb +136 -0
- data/lib/forest_liana/version.rb +1 -1
- data/spec/requests/actions_controller_spec.rb +41 -0
- data/spec/services/forest_liana/ability/permission_spec.rb +74 -0
- data/spec/services/forest_liana/query_stat_getter_spec.rb +267 -0
- data/spec/services/forest_liana/search_query_builder_spec.rb +1 -0
- data/spec/services/forest_liana/utils/live_query_injector_spec.rb +273 -0
- metadata +8 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 0ea33256d7cec4221e9c94bbe46f8bc4f7caaef29854b237cff1961aed4809dd
|
|
4
|
+
data.tar.gz: c80925513502ae243fabd0d0c6840483634940882226ccb1e6c686c0cbc52553
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f21a026ae94a6061ba804462b9e681dc4e43fa57ace54b627ca06fcec199d1ddbeebdbc701c94e4820d3e00de8bab105dc6acd3614db4f855318592b6957ab81
|
|
7
|
+
data.tar.gz: 0f8579fab129cd9ba90747c9c37a22de38bf4aa0bf89a210e4cee5a806129a9aee1b1adb5da22c8ede9925047478cf18fbceb79b394f5d5323b27f13a2e1ce08
|
|
@@ -47,6 +47,12 @@ module ForestLiana
|
|
|
47
47
|
{:field_changed => found_field_changed, :fields => fields, :params => params, :user => forest_user}
|
|
48
48
|
end
|
|
49
49
|
|
|
50
|
+
def ensure_single_record_selection(action, attributes)
|
|
51
|
+
return if SingleRecordSelection.allowed?(action, attributes)
|
|
52
|
+
|
|
53
|
+
raise ForestLiana::Errors::HTTP422Error.new(SingleRecordSelection::MESSAGE)
|
|
54
|
+
end
|
|
55
|
+
|
|
50
56
|
def handle_result(result, action)
|
|
51
57
|
if result.nil? || !result.is_a?(Array)
|
|
52
58
|
return render status: 500, json: { error: 'Error in smart action load hook: hook must return an array of fields' }
|
|
@@ -104,6 +110,8 @@ module ForestLiana
|
|
|
104
110
|
if !action
|
|
105
111
|
render status: 500, json: {error: 'Error in smart action load hook: cannot retrieve action from collection'}
|
|
106
112
|
else
|
|
113
|
+
ensure_single_record_selection(action, load_request)
|
|
114
|
+
|
|
107
115
|
# Get the smart action hook load context
|
|
108
116
|
context = get_smart_action_load_ctx(action.fields)
|
|
109
117
|
|
|
@@ -132,6 +140,8 @@ module ForestLiana
|
|
|
132
140
|
return render status: 500, json: {error: 'Error in smart action change hook: fields params must be an array'}
|
|
133
141
|
end
|
|
134
142
|
|
|
143
|
+
ensure_single_record_selection(action, change_request)
|
|
144
|
+
|
|
135
145
|
# Get the smart action hook change context
|
|
136
146
|
context = get_smart_action_change_ctx(change_request[:fields], change_request[:changed_field])
|
|
137
147
|
|
|
@@ -35,6 +35,7 @@ module ForestLiana
|
|
|
35
35
|
end
|
|
36
36
|
|
|
37
37
|
def is_smart_action_authorized?(user, collection, parameters, endpoint, http_method)
|
|
38
|
+
ensure_single_record_selection(collection, parameters, endpoint, http_method)
|
|
38
39
|
return true unless has_permission_system?
|
|
39
40
|
|
|
40
41
|
user_data = get_user_data(user['id'])
|
|
@@ -348,6 +349,24 @@ module ForestLiana
|
|
|
348
349
|
actions
|
|
349
350
|
end
|
|
350
351
|
|
|
352
|
+
def ensure_single_record_selection(collection, parameters, endpoint, http_method)
|
|
353
|
+
schema_action = find_action_from_endpoint(ForestLiana.name_for(collection), endpoint, http_method)
|
|
354
|
+
return unless schema_action&.type == 'single'
|
|
355
|
+
|
|
356
|
+
selections = [parameters[:data][:attributes], signed_approval_attributes(parameters)].compact
|
|
357
|
+
return if selections.all? { |attributes| SingleRecordSelection.allowed?(schema_action, attributes) }
|
|
358
|
+
|
|
359
|
+
raise ForestLiana::Errors::HTTP422Error.new(SingleRecordSelection::MESSAGE)
|
|
360
|
+
end
|
|
361
|
+
|
|
362
|
+
def signed_approval_attributes(parameters)
|
|
363
|
+
return unless parameters[:data][:attributes][:signed_approval_request]
|
|
364
|
+
|
|
365
|
+
RequestPermission.decodeSignedApprovalRequest(parameters)[:data][:attributes]
|
|
366
|
+
rescue JWT::DecodeError
|
|
367
|
+
nil
|
|
368
|
+
end
|
|
369
|
+
|
|
351
370
|
def find_action_from_endpoint(collection_name, endpoint, http_method)
|
|
352
371
|
collection = ForestLiana.apimap.find { |collection| collection.name.to_s == collection_name }
|
|
353
372
|
|
|
@@ -13,14 +13,11 @@ module ForestLiana
|
|
|
13
13
|
end
|
|
14
14
|
|
|
15
15
|
def perform
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
LiveQueryChecker.new(raw_query, 'Live Query Chart').validate()
|
|
16
|
+
query = @params['query'].strip
|
|
17
|
+
LiveQueryChecker.new(query, 'Live Query Chart').validate()
|
|
20
18
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
end
|
|
19
|
+
context_variables = Utils::ContextVariables.new(nil, nil, @params['contextVariables'])
|
|
20
|
+
raw_query = Utils::LiveQueryInjector.inject(query, context_variables, @params['record_id'])
|
|
24
21
|
result = ActiveRecord::Base.connection.execute(raw_query)
|
|
25
22
|
|
|
26
23
|
case @params['type']
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
module ForestLiana
|
|
2
|
+
module SingleRecordSelection
|
|
3
|
+
MESSAGE = 'This action can only be triggered on one record at a time.'
|
|
4
|
+
|
|
5
|
+
def self.allowed?(action, attributes)
|
|
6
|
+
return true unless action&.type == 'single'
|
|
7
|
+
|
|
8
|
+
!attributes[:all_records] && Array(attributes[:ids]).length == 1
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
end
|
|
@@ -1,30 +1,33 @@
|
|
|
1
1
|
module ForestLiana
|
|
2
2
|
module Utils
|
|
3
3
|
class ContextVariablesInjector
|
|
4
|
+
PLACEHOLDER = /{{([^}]+)}}/
|
|
4
5
|
|
|
5
6
|
def self.inject_context_in_value(value, context_variables)
|
|
6
7
|
inject_context_in_value_custom(value) do |context_variable_key|
|
|
7
|
-
|
|
8
|
-
if value.nil?
|
|
9
|
-
available_keys = context_variables.respond_to?(:keys) ? context_variables.keys.join(', ') : 'unknown'
|
|
10
|
-
available_context = context_variables.inspect
|
|
11
|
-
error_message = "Unknown context variable: '#{context_variable_key}'. " \
|
|
12
|
-
"Please check the query for any typos. " \
|
|
13
|
-
"Available context variables: #{available_keys}. "
|
|
14
|
-
raise error_message
|
|
15
|
-
end
|
|
16
|
-
value.is_a?(Array) || value.is_a?(Hash) ? value.to_json : value.to_s
|
|
8
|
+
context_value(context_variable_key, context_variables)
|
|
17
9
|
end
|
|
18
10
|
end
|
|
19
11
|
|
|
12
|
+
def self.context_value(context_variable_key, context_variables)
|
|
13
|
+
value = context_variables.get_value(context_variable_key)
|
|
14
|
+
if value.nil?
|
|
15
|
+
available_keys = context_variables.respond_to?(:keys) ? context_variables.keys.join(', ') : 'unknown'
|
|
16
|
+
error_message = "Unknown context variable: '#{context_variable_key}'. " \
|
|
17
|
+
"Please check the query for any typos. " \
|
|
18
|
+
"Available context variables: #{available_keys}. "
|
|
19
|
+
raise error_message
|
|
20
|
+
end
|
|
21
|
+
value.is_a?(Array) || value.is_a?(Hash) ? value.to_json : value.to_s
|
|
22
|
+
end
|
|
23
|
+
|
|
20
24
|
def self.inject_context_in_value_custom(value)
|
|
21
25
|
return value unless value.is_a?(String)
|
|
22
26
|
|
|
23
27
|
value_with_context_variables_injected = value
|
|
24
|
-
regex = /{{([^}]+)}}/
|
|
25
28
|
encountered_variables = []
|
|
26
29
|
|
|
27
|
-
while (match =
|
|
30
|
+
while (match = PLACEHOLDER.match(value_with_context_variables_injected))
|
|
28
31
|
context_variable_key = match[1]
|
|
29
32
|
|
|
30
33
|
value_with_context_variables_injected = value_with_context_variables_injected.gsub(
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
require 'strscan'
|
|
2
|
+
|
|
3
|
+
module ForestLiana
|
|
4
|
+
module Utils
|
|
5
|
+
class LiveQueryInjector
|
|
6
|
+
PLACEHOLDER = Regexp.union(ContextVariablesInjector::PLACEHOLDER, '?')
|
|
7
|
+
VERBATIM_VALUE = /\A(?:\d+(?:\.\d+)?(?:,\d+(?:\.\d+)?)*|true|false)\z/i
|
|
8
|
+
CHARACTERS_CLOSING_QUOTED_TEXT = /["`'\\$\]\r\n\0]/
|
|
9
|
+
LINE_COMMENT = /--[^\n]*/
|
|
10
|
+
MYSQL_LINE_COMMENT = /--(?=[[:space:][:cntrl:]]|\z)[^\n]*/
|
|
11
|
+
BLOCK_COMMENT = %r{/\*.*?(?:\*/|\z)}m
|
|
12
|
+
NESTED_BLOCK_COMMENT = %r{(?<comment>/\*(?:[^/*]|/(?!\*)|\*(?!/)|\g<comment>)*(?:\*/|\z))}m
|
|
13
|
+
BRACKET_QUOTED = /\[[^\]]*\]?/
|
|
14
|
+
WORD = /[[:alpha:]_][[:alnum:]_$]*/
|
|
15
|
+
STRING_CONTINUATION = /[ \t\f\r]*\n[[:space:]]*(?=')/
|
|
16
|
+
DOLLAR_QUOTED = /\$([[:alpha:]_][[:alnum:]_]*|)\$.*?(?:\$\1\$|\z)/m
|
|
17
|
+
|
|
18
|
+
def self.inject(query, context_variables, record_id, connection = ActiveRecord::Base.connection)
|
|
19
|
+
new(context_variables, record_id, connection).inject(query)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def initialize(context_variables, record_id, connection)
|
|
23
|
+
@context_variables = context_variables
|
|
24
|
+
@record_id = record_id
|
|
25
|
+
@connection = connection
|
|
26
|
+
@mysql = connection.adapter_name.match?(/mysql|trilogy/i)
|
|
27
|
+
@postgresql = connection.adapter_name.match?(/postg/i)
|
|
28
|
+
@bracket_quotes = connection.adapter_name.match?(/sqlite|sqlserver/i)
|
|
29
|
+
@nested_comments = @postgresql || connection.adapter_name.match?(/sqlserver/i)
|
|
30
|
+
@backslash_escapes = @mysql && mysql_backslash_escapes?
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def inject(query)
|
|
34
|
+
scanner = StringScanner.new(query)
|
|
35
|
+
injected = +''
|
|
36
|
+
injected << next_token(scanner) until scanner.eos?
|
|
37
|
+
injected
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
|
|
42
|
+
def next_token(scanner)
|
|
43
|
+
if (comment = scan_comment(scanner))
|
|
44
|
+
comment
|
|
45
|
+
elsif (word = scanner.scan(WORD))
|
|
46
|
+
return word unless @postgresql && word.casecmp?('e') && scanner.peek(1) == "'"
|
|
47
|
+
|
|
48
|
+
word + inject_in_string(scan_escape_string(scanner), backslash_escapes: true, escape_backslashes: true)
|
|
49
|
+
elsif (string = scanner.scan(quoted_pattern("'", backslash_escapes: @backslash_escapes)))
|
|
50
|
+
inject_in_string(string, backslash_escapes: @backslash_escapes, escape_backslashes: false)
|
|
51
|
+
elsif (quoted = scan_quoted_text(scanner))
|
|
52
|
+
inject_in_quoted_text(quoted)
|
|
53
|
+
elsif (placeholder = scanner.scan(PLACEHOLDER))
|
|
54
|
+
inject_in_code(placeholder)
|
|
55
|
+
else
|
|
56
|
+
scanner.getch
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def mysql_backslash_escapes?
|
|
61
|
+
!@connection.select_value('SELECT @@SESSION.sql_mode').to_s.include?('NO_BACKSLASH_ESCAPES')
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def scan_comment(scanner)
|
|
65
|
+
scanner.scan(@mysql ? MYSQL_LINE_COMMENT : LINE_COMMENT) ||
|
|
66
|
+
scanner.scan(@nested_comments ? NESTED_BLOCK_COMMENT : BLOCK_COMMENT) ||
|
|
67
|
+
(@mysql && scanner.scan(/#[^\n]*/))
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def scan_escape_string(scanner)
|
|
71
|
+
escape_string = scanner.scan(quoted_pattern("'", backslash_escapes: true))
|
|
72
|
+
while (continuation = scanner.scan(STRING_CONTINUATION))
|
|
73
|
+
escape_string << continuation << scanner.scan(quoted_pattern("'", backslash_escapes: true))
|
|
74
|
+
end
|
|
75
|
+
escape_string
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def scan_quoted_text(scanner)
|
|
79
|
+
scanner.scan(quoted_pattern('"', backslash_escapes: @backslash_escapes)) ||
|
|
80
|
+
scanner.scan(quoted_pattern('`')) ||
|
|
81
|
+
(@postgresql && scanner.scan(DOLLAR_QUOTED)) ||
|
|
82
|
+
(@bracket_quotes && scanner.scan(BRACKET_QUOTED))
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def quoted_pattern(quote, backslash_escapes: false)
|
|
86
|
+
escaped = Regexp.escape(quote)
|
|
87
|
+
content = backslash_escapes ? "[^#{escaped}\\\\]|\\\\.|#{escaped}#{escaped}" : "[^#{escaped}]|#{escaped}#{escaped}"
|
|
88
|
+
/#{escaped}(?:#{content})*#{escaped}?/m
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def inject_in_code(placeholder)
|
|
92
|
+
replace_placeholders(placeholder) do |text|
|
|
93
|
+
text.match?(VERBATIM_VALUE) ? text : @connection.quote(text)
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def inject_in_string(string, backslash_escapes:, escape_backslashes:)
|
|
98
|
+
replace_placeholders(string) do |text, placeholder, preceding_text|
|
|
99
|
+
if backslash_escapes && preceding_text[/\\*\z/].length.odd?
|
|
100
|
+
raise ForestLiana::Errors::LiveQueryError.new(
|
|
101
|
+
"Live Query Chart: #{placeholder} cannot follow a backslash inside a string."
|
|
102
|
+
)
|
|
103
|
+
end
|
|
104
|
+
escape_backslashes ? text.gsub(/[\\']/) { |character| character * 2 } : @connection.quote_string(text)
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def inject_in_quoted_text(quoted)
|
|
109
|
+
replace_placeholders(quoted) do |text, placeholder|
|
|
110
|
+
if text.match?(CHARACTERS_CLOSING_QUOTED_TEXT)
|
|
111
|
+
raise ForestLiana::Errors::LiveQueryError.new(
|
|
112
|
+
"Live Query Chart: The value of #{placeholder} cannot contain quotes, brackets, backslashes, " \
|
|
113
|
+
'dollar signs or line breaks when it is written inside a quoted identifier or a dollar-quoted string.'
|
|
114
|
+
)
|
|
115
|
+
end
|
|
116
|
+
text
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def replace_placeholders(text)
|
|
121
|
+
text.gsub(PLACEHOLDER) do |placeholder|
|
|
122
|
+
context_variable_key = Regexp.last_match(1)
|
|
123
|
+
preceding_text = Regexp.last_match.pre_match
|
|
124
|
+
if context_variable_key
|
|
125
|
+
value = ContextVariablesInjector.context_value(context_variable_key, @context_variables)
|
|
126
|
+
yield(value, placeholder, preceding_text)
|
|
127
|
+
elsif @record_id.nil?
|
|
128
|
+
placeholder
|
|
129
|
+
else
|
|
130
|
+
yield(@record_id.to_s, placeholder, preceding_text)
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
end
|
data/lib/forest_liana/version.rb
CHANGED
|
@@ -217,6 +217,47 @@ describe 'Requesting Actions routes', :type => :request do
|
|
|
217
217
|
end
|
|
218
218
|
end
|
|
219
219
|
|
|
220
|
+
describe 'on a single-record action' do
|
|
221
|
+
let(:single_record_error) { 'This action can only be triggered on one record at a time.' }
|
|
222
|
+
|
|
223
|
+
before do
|
|
224
|
+
action = island.actions.find { |action| action.name == 'my_action' }
|
|
225
|
+
allow(action).to receive(:type).and_return('single')
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
def hook_params(attributes)
|
|
229
|
+
JSON.dump(data: { attributes: { collection_name: 'Island' }.merge(attributes) })
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
[
|
|
233
|
+
['two ids', { ids: [1, 2] }],
|
|
234
|
+
['no id', { ids: [] }],
|
|
235
|
+
['a select-all', { ids: [1], all_records: true }],
|
|
236
|
+
].each do |description, selection|
|
|
237
|
+
it "should refuse to load the form given #{description}" do
|
|
238
|
+
post '/forest/actions/my_action/hooks/load', params: hook_params(selection), headers: headers
|
|
239
|
+
|
|
240
|
+
expect(response.status).to eq(422)
|
|
241
|
+
expect(response.body).to include(single_record_error)
|
|
242
|
+
end
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
it 'should refuse a change given two ids' do
|
|
246
|
+
post '/forest/actions/my_action/hooks/change',
|
|
247
|
+
params: hook_params(ids: [1, 2], fields: [{ field: 'foo', hook: 'on_foo_changed' }], changed_field: 'foo'),
|
|
248
|
+
headers: headers
|
|
249
|
+
|
|
250
|
+
expect(response.status).to eq(422)
|
|
251
|
+
expect(response.body).to include(single_record_error)
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
it 'should load the form on exactly one id' do
|
|
255
|
+
post '/forest/actions/my_action/hooks/load', params: hook_params(ids: [1]), headers: headers
|
|
256
|
+
|
|
257
|
+
expect(response.status).to eq(200)
|
|
258
|
+
end
|
|
259
|
+
end
|
|
260
|
+
|
|
220
261
|
describe 'call /change' do
|
|
221
262
|
it 'should respond 200' do
|
|
222
263
|
action = island.actions.select { |action| action.name == 'my_action' }.first
|
|
@@ -428,6 +428,80 @@ module ForestLiana
|
|
|
428
428
|
expect {dummy_class.is_smart_action_authorized?(user, String, parameters, '/forest/actions/my_action', 'POST')}
|
|
429
429
|
.to raise_error(ForestLiana::Ability::Exceptions::UnknownCollection, 'The collection String doesn\'t exist')
|
|
430
430
|
end
|
|
431
|
+
|
|
432
|
+
it 'should run a bulk action on several ids' do
|
|
433
|
+
parameters[:data][:attributes][:ids] = ['1', '2']
|
|
434
|
+
|
|
435
|
+
expect(dummy_class.is_smart_action_authorized?(user, Island, parameters, '/forest/actions/my_action', 'POST')).to equal true
|
|
436
|
+
end
|
|
437
|
+
|
|
438
|
+
describe 'with a single-record action' do
|
|
439
|
+
let(:single_record_error) { 'This action can only be triggered on one record at a time.' }
|
|
440
|
+
|
|
441
|
+
before do
|
|
442
|
+
island_action = ForestLiana.apimap.find { |collection| collection.name.to_s == 'Island' }
|
|
443
|
+
.actions.find { |action| action.name == 'my_action' }
|
|
444
|
+
allow(island_action).to receive(:type).and_return('single')
|
|
445
|
+
end
|
|
446
|
+
|
|
447
|
+
it 'should return true on exactly one id' do
|
|
448
|
+
expect(dummy_class.is_smart_action_authorized?(user, Island, parameters, '/forest/actions/my_action', 'POST')).to equal true
|
|
449
|
+
end
|
|
450
|
+
|
|
451
|
+
[
|
|
452
|
+
['two ids', { ids: ['1', '2'] }],
|
|
453
|
+
['no id', { ids: [] }],
|
|
454
|
+
['a select-all', { ids: ['1'], all_records: true }],
|
|
455
|
+
].each do |description, selection|
|
|
456
|
+
it "should refuse #{description} before checking permissions" do
|
|
457
|
+
parameters[:data][:attributes].merge!(selection)
|
|
458
|
+
expect(SmartActionChecker).not_to receive(:new)
|
|
459
|
+
|
|
460
|
+
expect { dummy_class.is_smart_action_authorized?(user, Island, parameters, '/forest/actions/my_action', 'POST') }
|
|
461
|
+
.to raise_error(ForestLiana::Errors::HTTP422Error, single_record_error)
|
|
462
|
+
end
|
|
463
|
+
end
|
|
464
|
+
|
|
465
|
+
def sign(ids)
|
|
466
|
+
JWT.encode(
|
|
467
|
+
{ data: { attributes: parameters[:data][:attributes].to_h.merge(ids: ids, requester_id: 2) } },
|
|
468
|
+
ForestLiana.env_secret,
|
|
469
|
+
'HS256'
|
|
470
|
+
)
|
|
471
|
+
end
|
|
472
|
+
|
|
473
|
+
it 'should refuse to approve a request naming several ids' do
|
|
474
|
+
parameters[:data][:attributes][:signed_approval_request] = sign(['1', '2'])
|
|
475
|
+
expect(SmartActionChecker).not_to receive(:new)
|
|
476
|
+
|
|
477
|
+
expect { dummy_class.is_smart_action_authorized?(user, Island, parameters, '/forest/actions/my_action', 'POST') }
|
|
478
|
+
.to raise_error(ForestLiana::Errors::HTTP422Error, single_record_error)
|
|
479
|
+
end
|
|
480
|
+
|
|
481
|
+
it 'should refuse several ids sent beside a signed request naming one' do
|
|
482
|
+
parameters[:data][:attributes][:ids] = ['1', '2']
|
|
483
|
+
parameters[:data][:attributes][:signed_approval_request] = sign(['1'])
|
|
484
|
+
expect(SmartActionChecker).not_to receive(:new)
|
|
485
|
+
|
|
486
|
+
expect { dummy_class.is_smart_action_authorized?(user, Island, parameters, '/forest/actions/my_action', 'POST') }
|
|
487
|
+
.to raise_error(ForestLiana::Errors::HTTP422Error, single_record_error)
|
|
488
|
+
end
|
|
489
|
+
|
|
490
|
+
it 'should leave an undecodable signed request to the permission check' do
|
|
491
|
+
parameters[:data][:attributes][:signed_approval_request] = 'not-a-jwt'
|
|
492
|
+
expect(Permission::SmartActionChecker).to receive(:new).and_return(instance_double(Permission::SmartActionChecker, can_execute?: true))
|
|
493
|
+
|
|
494
|
+
expect(dummy_class.is_smart_action_authorized?(user, Island, parameters, '/forest/actions/my_action', 'POST')).to equal true
|
|
495
|
+
end
|
|
496
|
+
|
|
497
|
+
it 'should refuse several ids without a permission system' do
|
|
498
|
+
Rails.cache.write('forest.has_permission', false)
|
|
499
|
+
parameters[:data][:attributes][:ids] = ['1', '2']
|
|
500
|
+
|
|
501
|
+
expect { dummy_class.is_smart_action_authorized?(user, Island, parameters, '/forest/actions/my_action', 'POST') }
|
|
502
|
+
.to raise_error(ForestLiana::Errors::HTTP422Error, single_record_error)
|
|
503
|
+
end
|
|
504
|
+
end
|
|
431
505
|
end
|
|
432
506
|
|
|
433
507
|
describe 'when the server doesn\'t return an success response' do
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
module ForestLiana
|
|
2
|
+
describe QueryStatGetter do
|
|
3
|
+
let!(:aragorn) { User.create!(name: 'Aragorn') }
|
|
4
|
+
let!(:obrien) { User.create!(name: "O'Brien") }
|
|
5
|
+
let!(:sam) { User.create!(name: 'Sam') }
|
|
6
|
+
|
|
7
|
+
def value_of(query, record_id: nil, context_variables: nil)
|
|
8
|
+
params = { 'type' => 'Value', 'query' => query }
|
|
9
|
+
params['record_id'] = record_id unless record_id.nil?
|
|
10
|
+
params['contextVariables'] = context_variables unless context_variables.nil?
|
|
11
|
+
|
|
12
|
+
getter = QueryStatGetter.new(params)
|
|
13
|
+
getter.perform
|
|
14
|
+
getter.record.value[:countCurrent]
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
describe 'record id' do
|
|
18
|
+
it 'matches the record whose integer id replaces ?' do
|
|
19
|
+
expect(value_of('SELECT COUNT(*) AS value FROM users WHERE id = ?', record_id: aragorn.id)).to eq(1)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
it 'matches the record whose integer id is sent as a string' do
|
|
23
|
+
expect(value_of('SELECT COUNT(*) AS value FROM users WHERE id = ?', record_id: aragorn.id.to_s)).to eq(1)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
it 'matches a string id written inside quotes in the query' do
|
|
27
|
+
expect(value_of("SELECT COUNT(*) AS value FROM users WHERE name = '?'", record_id: 'Aragorn')).to eq(1)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
it 'matches a string id that contains a quote' do
|
|
31
|
+
expect(value_of("SELECT COUNT(*) AS value FROM users WHERE name = '?'", record_id: "O'Brien")).to eq(1)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
it 'matches a string id written without quotes in the query' do
|
|
35
|
+
expect(value_of('SELECT COUNT(*) AS value FROM users WHERE name = ?', record_id: 'Aragorn')).to eq(1)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
it 'matches a UUID id written without quotes in the query' do
|
|
39
|
+
uuid = '3f2504e0-4f89-11d3-9a0c-0305e82c3301'
|
|
40
|
+
User.create!(name: uuid)
|
|
41
|
+
|
|
42
|
+
expect(value_of('SELECT COUNT(*) AS value FROM users WHERE name = ?', record_id: uuid)).to eq(1)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
it 'compares a non-numeric record id as a single value' do
|
|
46
|
+
expect(value_of('SELECT COUNT(*) AS value FROM users WHERE id = ?', record_id: '0 OR 1=1')).to eq(0)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
it 'keeps a record id containing quotes inside its string literal' do
|
|
50
|
+
expect(
|
|
51
|
+
value_of("SELECT COUNT(*) AS value FROM users WHERE name = '?'", record_id: "x' OR '1'='1")
|
|
52
|
+
).to eq(0)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it 'compares a record id holding a semicolon as a single value' do
|
|
56
|
+
expect(value_of('SELECT COUNT(*) AS value FROM users WHERE id = ?', record_id: '1; DELETE FROM users')).to eq(0)
|
|
57
|
+
expect(User.count).to eq(3)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
it 'matches a string id holding a semicolon' do
|
|
61
|
+
User.create!(name: 'a;b')
|
|
62
|
+
|
|
63
|
+
expect(value_of("SELECT COUNT(*) AS value FROM users WHERE name = '?'", record_id: 'a;b')).to eq(1)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
it 'refuses a query that chains statements before any value is injected' do
|
|
67
|
+
expect {
|
|
68
|
+
value_of('SELECT COUNT(*) AS value FROM users WHERE id = ?; DELETE FROM users', record_id: 1)
|
|
69
|
+
}.to raise_error(ForestLiana::Errors::LiveQueryError, /You cannot chain SQL/)
|
|
70
|
+
expect(User.count).to eq(3)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
it 'leaves ? untouched when no record id is sent' do
|
|
74
|
+
expect(value_of("SELECT COUNT(*) AS value FROM users WHERE name <> '?'")).to eq(3)
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
describe 'context variables' do
|
|
79
|
+
it 'matches the record whose integer id is injected' do
|
|
80
|
+
expect(
|
|
81
|
+
value_of(
|
|
82
|
+
'SELECT COUNT(*) AS value FROM users WHERE id = {{users.selectedRecord.id}}',
|
|
83
|
+
context_variables: { 'users.selectedRecord.id' => aragorn.id.to_s }
|
|
84
|
+
)
|
|
85
|
+
).to eq(1)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
it 'matches every id of a comma-separated list of integers' do
|
|
89
|
+
expect(
|
|
90
|
+
value_of(
|
|
91
|
+
'SELECT COUNT(*) AS value FROM users WHERE id IN ({{users.selectedRecords.id}})',
|
|
92
|
+
context_variables: { 'users.selectedRecords.id' => "#{aragorn.id},#{sam.id}" }
|
|
93
|
+
)
|
|
94
|
+
).to eq(2)
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
it 'matches a string written inside quotes in the query' do
|
|
98
|
+
expect(
|
|
99
|
+
value_of(
|
|
100
|
+
"SELECT COUNT(*) AS value FROM users WHERE name = '{{dropdown.selectedValue}}'",
|
|
101
|
+
context_variables: { 'dropdown.selectedValue' => 'Aragorn' }
|
|
102
|
+
)
|
|
103
|
+
).to eq(1)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
it 'matches a string that contains a quote' do
|
|
107
|
+
expect(
|
|
108
|
+
value_of(
|
|
109
|
+
"SELECT COUNT(*) AS value FROM users WHERE name = '{{dropdown.selectedValue}}'",
|
|
110
|
+
context_variables: { 'dropdown.selectedValue' => "O'Brien" }
|
|
111
|
+
)
|
|
112
|
+
).to eq(1)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
it 'matches a string written without quotes in the query' do
|
|
116
|
+
expect(
|
|
117
|
+
value_of(
|
|
118
|
+
'SELECT COUNT(*) AS value FROM users WHERE name = {{dropdown.selectedValue}}',
|
|
119
|
+
context_variables: { 'dropdown.selectedValue' => 'Aragorn' }
|
|
120
|
+
)
|
|
121
|
+
).to eq(1)
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
it 'matches a string injected in the middle of a string literal' do
|
|
125
|
+
expect(
|
|
126
|
+
value_of(
|
|
127
|
+
"SELECT COUNT(*) AS value FROM users WHERE name LIKE '%{{search.value}}%'",
|
|
128
|
+
context_variables: { 'search.value' => 'rag' }
|
|
129
|
+
)
|
|
130
|
+
).to eq(1)
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
it 'injects a boolean as a boolean' do
|
|
134
|
+
expect(
|
|
135
|
+
value_of(
|
|
136
|
+
'SELECT COUNT(*) AS value FROM users WHERE {{toggle.value}}',
|
|
137
|
+
context_variables: { 'toggle.value' => 'true' }
|
|
138
|
+
)
|
|
139
|
+
).to eq(3)
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
it 'injects the same variable everywhere it appears' do
|
|
143
|
+
expect(
|
|
144
|
+
value_of(
|
|
145
|
+
"SELECT COUNT(*) AS value FROM users WHERE id = {{users.selectedRecord.id}} OR name = '{{users.selectedRecord.id}}'",
|
|
146
|
+
context_variables: { 'users.selectedRecord.id' => sam.id.to_s }
|
|
147
|
+
)
|
|
148
|
+
).to eq(1)
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
it 'compares a non-numeric value as a single value' do
|
|
152
|
+
expect(
|
|
153
|
+
value_of(
|
|
154
|
+
'SELECT COUNT(*) AS value FROM users WHERE id = {{users.selectedRecord.id}}',
|
|
155
|
+
context_variables: { 'users.selectedRecord.id' => '0 OR 1=1' }
|
|
156
|
+
)
|
|
157
|
+
).to eq(0)
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
it 'compares a value holding a second SELECT as a single value' do
|
|
161
|
+
expect(
|
|
162
|
+
value_of(
|
|
163
|
+
'SELECT COUNT(*) AS value FROM users WHERE id = {{users.selectedRecord.id}}',
|
|
164
|
+
context_variables: { 'users.selectedRecord.id' => '0 UNION SELECT 99 ORDER BY 1 DESC' }
|
|
165
|
+
)
|
|
166
|
+
).to eq(0)
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
it 'keeps a value containing quotes inside its string literal' do
|
|
170
|
+
expect(
|
|
171
|
+
value_of(
|
|
172
|
+
"SELECT COUNT(*) AS value FROM users WHERE name = '{{dropdown.selectedValue}}'",
|
|
173
|
+
context_variables: { 'dropdown.selectedValue' => "x' OR '1'='1" }
|
|
174
|
+
)
|
|
175
|
+
).to eq(0)
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
it 'keeps a value containing quotes inside the string literal it is injected into' do
|
|
179
|
+
expect(
|
|
180
|
+
value_of(
|
|
181
|
+
"SELECT COUNT(*) AS value FROM users WHERE name LIKE '%{{search.value}}%'",
|
|
182
|
+
context_variables: { 'search.value' => "%' OR '1'='1" }
|
|
183
|
+
)
|
|
184
|
+
).to eq(0)
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
it 'leaves variables written in a comment untouched' do
|
|
188
|
+
expect(
|
|
189
|
+
value_of(
|
|
190
|
+
"SELECT COUNT(*) AS value FROM users -- {{note.value}}\nWHERE id = {{users.selectedRecord.id}}",
|
|
191
|
+
context_variables: { 'note.value' => "\nOR 1=1", 'users.selectedRecord.id' => aragorn.id.to_s }
|
|
192
|
+
)
|
|
193
|
+
).to eq(1)
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
it 'injects a plain value inside a quoted identifier' do
|
|
197
|
+
expect(
|
|
198
|
+
value_of(
|
|
199
|
+
'SELECT COUNT(*) AS value FROM "{{dropdown.selectedValue}}"',
|
|
200
|
+
context_variables: { 'dropdown.selectedValue' => 'users' }
|
|
201
|
+
)
|
|
202
|
+
).to eq(3)
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
it 'refuses a value that would end the quoted identifier it is injected into' do
|
|
206
|
+
expect {
|
|
207
|
+
value_of(
|
|
208
|
+
'SELECT COUNT(*) AS value FROM "{{dropdown.selectedValue}}"',
|
|
209
|
+
context_variables: { 'dropdown.selectedValue' => 'users" WHERE 1=0 OR "1' }
|
|
210
|
+
)
|
|
211
|
+
}.to raise_error(ForestLiana::Errors::LiveQueryError)
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
it 'does not inject a variable or the record id found inside an injected value' do
|
|
215
|
+
expect(
|
|
216
|
+
value_of(
|
|
217
|
+
"SELECT COUNT(*) AS value FROM users WHERE name = '{{dropdown.selectedValue}}' OR id = ?",
|
|
218
|
+
record_id: aragorn.id,
|
|
219
|
+
context_variables: { 'dropdown.selectedValue' => '{{other.value}}?' }
|
|
220
|
+
)
|
|
221
|
+
).to eq(1)
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
it 'matches a string holding a semicolon' do
|
|
225
|
+
User.create!(name: 'a;b')
|
|
226
|
+
|
|
227
|
+
expect(
|
|
228
|
+
value_of(
|
|
229
|
+
"SELECT COUNT(*) AS value FROM users WHERE name = '{{dropdown.selectedValue}}'",
|
|
230
|
+
context_variables: { 'dropdown.selectedValue' => 'a;b' }
|
|
231
|
+
)
|
|
232
|
+
).to eq(1)
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
it 'compares a value holding a semicolon and a second statement as a single value' do
|
|
236
|
+
expect(
|
|
237
|
+
value_of(
|
|
238
|
+
'SELECT COUNT(*) AS value FROM users WHERE name = {{dropdown.selectedValue}}',
|
|
239
|
+
context_variables: { 'dropdown.selectedValue' => 'Sam; DELETE FROM users' }
|
|
240
|
+
)
|
|
241
|
+
).to eq(0)
|
|
242
|
+
expect(User.count).to eq(3)
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
it 'raises on a variable the request does not provide' do
|
|
246
|
+
expect {
|
|
247
|
+
value_of(
|
|
248
|
+
'SELECT COUNT(*) AS value FROM users WHERE id = {{users.selectedRecord.id}}',
|
|
249
|
+
context_variables: {}
|
|
250
|
+
)
|
|
251
|
+
}.to raise_error(/Unknown context variable: 'users\.selectedRecord\.id'/)
|
|
252
|
+
end
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
describe 'with both a record id and context variables' do
|
|
256
|
+
it 'injects both' do
|
|
257
|
+
expect(
|
|
258
|
+
value_of(
|
|
259
|
+
"SELECT COUNT(*) AS value FROM users WHERE id = ? OR name = '{{dropdown.selectedValue}}'",
|
|
260
|
+
record_id: aragorn.id,
|
|
261
|
+
context_variables: { 'dropdown.selectedValue' => 'Sam' }
|
|
262
|
+
)
|
|
263
|
+
).to eq(2)
|
|
264
|
+
end
|
|
265
|
+
end
|
|
266
|
+
end
|
|
267
|
+
end
|
|
@@ -751,6 +751,7 @@ module ForestLiana
|
|
|
751
751
|
|
|
752
752
|
before do
|
|
753
753
|
Rails.cache.write('forest.has_permission', false)
|
|
754
|
+
allow(ForestLiana::ScopeManager).to receive(:fetch_scopes).and_return({ 'scopes' => {}, 'team' => { 'id' => '1', 'name' => 'Operations' } })
|
|
754
755
|
allow(ForestLiana::ScopeManager).to receive(:append_scope).and_call_original
|
|
755
756
|
allow(ForestLiana::ScopeManager).to receive(:get_scope).and_return(nil)
|
|
756
757
|
end
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
module ForestLiana
|
|
2
|
+
module Utils
|
|
3
|
+
describe LiveQueryInjector do
|
|
4
|
+
let(:context_variables) { ContextVariables.new(nil, nil, request_context_variables) }
|
|
5
|
+
let(:request_context_variables) { {} }
|
|
6
|
+
|
|
7
|
+
def mysql_connection(sql_mode: 'STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION')
|
|
8
|
+
connection = double('mysql connection', adapter_name: 'Mysql2')
|
|
9
|
+
allow(connection).to receive(:select_value).with('SELECT @@SESSION.sql_mode').and_return(sql_mode)
|
|
10
|
+
if sql_mode.include?('NO_BACKSLASH_ESCAPES')
|
|
11
|
+
allow(connection).to receive(:quote_string) { |value| value.gsub("'", "''") }
|
|
12
|
+
else
|
|
13
|
+
allow(connection).to receive(:quote_string) { |value| value.gsub(/[\\'"]/) { |char| "\\#{char}" } }
|
|
14
|
+
end
|
|
15
|
+
allow(connection).to receive(:quote) { |value| "'#{connection.quote_string(value)}'" }
|
|
16
|
+
connection
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def sqlserver_connection
|
|
20
|
+
connection = double('sqlserver connection', adapter_name: 'SQLServer')
|
|
21
|
+
allow(connection).to receive(:quote_string) { |value| value.gsub("'", "''") }
|
|
22
|
+
allow(connection).to receive(:quote) { |value| "'#{connection.quote_string(value)}'" }
|
|
23
|
+
connection
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def postgresql_connection
|
|
27
|
+
connection = double('postgresql connection', adapter_name: 'PostgreSQL')
|
|
28
|
+
allow(connection).to receive(:quote_string) { |value| value.gsub("'", "''") }
|
|
29
|
+
allow(connection).to receive(:quote) { |value| "'#{connection.quote_string(value)}'" }
|
|
30
|
+
connection
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def inject(query, connection, record_id: nil)
|
|
34
|
+
LiveQueryInjector.inject(query, context_variables, record_id, connection)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
describe 'on MySQL' do
|
|
38
|
+
let(:request_context_variables) { { 'x.value' => '0 OR 1=1' } }
|
|
39
|
+
|
|
40
|
+
it 'reads a backslash-escaped quote as part of the string literal' do
|
|
41
|
+
expect(inject("SELECT 1 FROM t WHERE a = 'it\\'s' AND id = {{x.value}}", mysql_connection))
|
|
42
|
+
.to eq("SELECT 1 FROM t WHERE a = 'it\\'s' AND id = '0 OR 1=1'")
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
it 'escapes a value injected inside a string literal with the connection' do
|
|
46
|
+
request_context_variables['x.value'] = "a\\' OR 1=1 -- "
|
|
47
|
+
|
|
48
|
+
expect(inject("SELECT 1 FROM t WHERE a LIKE '%{{x.value}}%'", mysql_connection))
|
|
49
|
+
.to eq("SELECT 1 FROM t WHERE a LIKE '%a\\\\\\' OR 1=1 -- %'")
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
it 'injects a plain value inside a double-quoted string' do
|
|
53
|
+
request_context_variables['x.value'] = 'Aragorn'
|
|
54
|
+
|
|
55
|
+
expect(inject('SELECT 1 FROM t WHERE a = "{{x.value}}"', mysql_connection))
|
|
56
|
+
.to eq('SELECT 1 FROM t WHERE a = "Aragorn"')
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
it 'refuses a value holding a quote inside a double-quoted string' do
|
|
60
|
+
request_context_variables['x.value'] = 'a" OR "1'
|
|
61
|
+
|
|
62
|
+
expect { inject('SELECT 1 FROM t WHERE a = "{{x.value}}"', mysql_connection) }
|
|
63
|
+
.to raise_error(ForestLiana::Errors::LiveQueryError)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
it 'refuses a value placed right after a backslash inside a string literal' do
|
|
67
|
+
request_context_variables['x.value'] = "' OR true -- "
|
|
68
|
+
|
|
69
|
+
expect { inject("SELECT 1 FROM t WHERE a = '\\{{x.value}}'", mysql_connection) }
|
|
70
|
+
.to raise_error(ForestLiana::Errors::LiveQueryError)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
it 'refuses a record id placed right after a backslash inside a string literal' do
|
|
74
|
+
expect { inject("SELECT 1 FROM t WHERE a = '\\?'", mysql_connection, record_id: "' OR true -- ") }
|
|
75
|
+
.to raise_error(ForestLiana::Errors::LiveQueryError)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
it 'reads two dashes not followed by a space as code' do
|
|
79
|
+
request_context_variables['x.value'] = '5'
|
|
80
|
+
|
|
81
|
+
expect(inject('SELECT 1--{{x.value}} AS value FROM t', mysql_connection))
|
|
82
|
+
.to eq('SELECT 1--5 AS value FROM t')
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
it 'leaves a dash-dash-space comment untouched' do
|
|
86
|
+
expect(inject("SELECT 1 FROM t -- {{x.value}} ?\nWHERE id = ?", mysql_connection, record_id: 7))
|
|
87
|
+
.to eq("SELECT 1 FROM t -- {{x.value}} ?\nWHERE id = 7")
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
it 'ends a dash-dash comment at the line break right after it' do
|
|
91
|
+
expect(inject("SELECT 1 FROM t --\nWHERE id = ?", mysql_connection, record_id: 7))
|
|
92
|
+
.to eq("SELECT 1 FROM t --\nWHERE id = 7")
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
it 'injects a plain value inside a backtick-quoted identifier' do
|
|
96
|
+
request_context_variables['x.value'] = 'name'
|
|
97
|
+
|
|
98
|
+
expect(inject('SELECT `{{x.value}}` AS value FROM t', mysql_connection))
|
|
99
|
+
.to eq('SELECT `name` AS value FROM t')
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
it 'refuses a value holding a backtick inside a backtick-quoted identifier' do
|
|
103
|
+
request_context_variables['x.value'] = 'name` FROM t WHERE 1=0 OR `1'
|
|
104
|
+
|
|
105
|
+
expect { inject('SELECT `{{x.value}}` AS value FROM t', mysql_connection) }
|
|
106
|
+
.to raise_error(ForestLiana::Errors::LiveQueryError)
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
describe 'when the session does not treat backslashes as escapes' do
|
|
110
|
+
let(:connection) { mysql_connection(sql_mode: 'STRICT_TRANS_TABLES,NO_BACKSLASH_ESCAPES') }
|
|
111
|
+
|
|
112
|
+
it 'reads a backslash as a plain character of a string literal' do
|
|
113
|
+
expect(inject("SELECT 1 FROM t WHERE a = 'x\\' AND id = {{x.value}}", connection))
|
|
114
|
+
.to eq("SELECT 1 FROM t WHERE a = 'x\\' AND id = '0 OR 1=1'")
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
it 'injects a value placed right after a backslash inside a string literal' do
|
|
118
|
+
request_context_variables['x.value'] = "a'b"
|
|
119
|
+
|
|
120
|
+
expect(inject("SELECT 1 FROM t WHERE a = '\\{{x.value}}'", connection))
|
|
121
|
+
.to eq("SELECT 1 FROM t WHERE a = '\\a''b'")
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
it 'leaves a hash comment untouched' do
|
|
126
|
+
expect(inject("SELECT 1 FROM t # {{x.value}} ?\nWHERE id = ?", mysql_connection, record_id: 7))
|
|
127
|
+
.to eq("SELECT 1 FROM t # {{x.value}} ?\nWHERE id = 7")
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
describe 'on PostgreSQL' do
|
|
132
|
+
let(:request_context_variables) { { 'x.value' => '0 OR 1=1' } }
|
|
133
|
+
|
|
134
|
+
it 'reads a backslash as a plain character of a standard string literal' do
|
|
135
|
+
expect(inject("SELECT 1 FROM t WHERE a = 'a\\' AND id = {{x.value}}", postgresql_connection))
|
|
136
|
+
.to eq("SELECT 1 FROM t WHERE a = 'a\\' AND id = '0 OR 1=1'")
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
it 'escapes quotes and backslashes of a value injected inside an escape string' do
|
|
140
|
+
request_context_variables['x.value'] = "\\' OR 1=1 --"
|
|
141
|
+
|
|
142
|
+
expect(inject("SELECT 1 FROM t WHERE a = E'{{x.value}}'", postgresql_connection))
|
|
143
|
+
.to eq("SELECT 1 FROM t WHERE a = E'\\\\'' OR 1=1 --'")
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
it 'leaves the hash operator outside comments' do
|
|
147
|
+
expect(inject('SELECT 1 # {{x.value}} AS value FROM t', postgresql_connection))
|
|
148
|
+
.to eq("SELECT 1 # '0 OR 1=1' AS value FROM t")
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
it 'refuses a value placed right after a backslash inside an escape string' do
|
|
152
|
+
request_context_variables['x.value'] = "' OR true -- "
|
|
153
|
+
|
|
154
|
+
expect { inject("SELECT 1 FROM t WHERE a = E'\\{{x.value}}'", postgresql_connection) }
|
|
155
|
+
.to raise_error(ForestLiana::Errors::LiveQueryError)
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
it 'refuses a value right after a backslash in the continuation of an escape string' do
|
|
159
|
+
request_context_variables['x.value'] = "' OR true -- "
|
|
160
|
+
|
|
161
|
+
expect { inject("SELECT 1 FROM t WHERE a = E'a'\n '\\{{x.value}}'", postgresql_connection) }
|
|
162
|
+
.to raise_error(ForestLiana::Errors::LiveQueryError)
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
it 'escapes backslashes of a value injected in the continuation of an escape string' do
|
|
166
|
+
request_context_variables['x.value'] = "a\\b"
|
|
167
|
+
|
|
168
|
+
expect(inject("SELECT 1 FROM t WHERE a = E'a'\n'{{x.value}}'", postgresql_connection))
|
|
169
|
+
.to eq("SELECT 1 FROM t WHERE a = E'a'\n'a\\\\b'")
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
it 'injects a value placed right after an escaped backslash inside an escape string' do
|
|
173
|
+
request_context_variables['x.value'] = "a'b"
|
|
174
|
+
|
|
175
|
+
expect(inject("SELECT 1 FROM t WHERE a = E'\\\\{{x.value}}'", postgresql_connection))
|
|
176
|
+
.to eq("SELECT 1 FROM t WHERE a = E'\\\\a''b'")
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
it 'quotes a value placed after an untagged dollar-quoted string' do
|
|
180
|
+
expect(inject('SELECT $$a$$ AS value FROM t WHERE id = {{x.value}}', postgresql_connection))
|
|
181
|
+
.to eq("SELECT $$a$$ AS value FROM t WHERE id = '0 OR 1=1'")
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
it 'injects a plain value inside a dollar-quoted string' do
|
|
185
|
+
request_context_variables['x.value'] = 'Aragorn'
|
|
186
|
+
|
|
187
|
+
expect(inject('SELECT 1 FROM t WHERE a = $tag${{x.value}}$tag$', postgresql_connection))
|
|
188
|
+
.to eq('SELECT 1 FROM t WHERE a = $tag$Aragorn$tag$')
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
it 'refuses a value holding a dollar sign inside a dollar-quoted string' do
|
|
192
|
+
request_context_variables['x.value'] = '$tag$ OR 1=1 --'
|
|
193
|
+
|
|
194
|
+
expect { inject('SELECT 1 FROM t WHERE a = $tag${{x.value}}$tag$', postgresql_connection) }
|
|
195
|
+
.to raise_error(ForestLiana::Errors::LiveQueryError)
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
it 'leaves a nested block comment untouched' do
|
|
199
|
+
expect(inject('SELECT 1 /* a /* b */ {{x.value}} */ FROM t WHERE id = {{x.value}}', postgresql_connection))
|
|
200
|
+
.to eq("SELECT 1 /* a /* b */ {{x.value}} */ FROM t WHERE id = '0 OR 1=1'")
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
it 'leaves a block comment untouched' do
|
|
204
|
+
expect(inject('SELECT 1 /* {{x.value}} ? */ FROM t WHERE id = ?', postgresql_connection, record_id: 'abc'))
|
|
205
|
+
.to eq("SELECT 1 /* {{x.value}} ? */ FROM t WHERE id = 'abc'")
|
|
206
|
+
end
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
describe 'on SQLite' do
|
|
210
|
+
let(:request_context_variables) { { 'x.value' => 'users' } }
|
|
211
|
+
|
|
212
|
+
def sqlite_connection
|
|
213
|
+
connection = double('sqlite connection', adapter_name: 'SQLite')
|
|
214
|
+
allow(connection).to receive(:quote_string) { |value| value.gsub("'", "''") }
|
|
215
|
+
allow(connection).to receive(:quote) { |value| "'#{connection.quote_string(value)}'" }
|
|
216
|
+
connection
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
it 'injects a plain value inside a bracket-quoted identifier' do
|
|
220
|
+
expect(inject('SELECT COUNT(*) AS value FROM [{{x.value}}]', sqlite_connection))
|
|
221
|
+
.to eq('SELECT COUNT(*) AS value FROM [users]')
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
it 'refuses a value holding a closing bracket inside a bracket-quoted identifier' do
|
|
225
|
+
request_context_variables['x.value'] = 'users] WHERE 1=0 OR [1'
|
|
226
|
+
|
|
227
|
+
expect { inject('SELECT COUNT(*) AS value FROM [{{x.value}}]', sqlite_connection) }
|
|
228
|
+
.to raise_error(ForestLiana::Errors::LiveQueryError)
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
it 'reads a nested block comment marker as plain comment text' do
|
|
232
|
+
request_context_variables['x.value'] = '0 OR 1=1'
|
|
233
|
+
|
|
234
|
+
expect(inject('SELECT 1 /* a /* b */ FROM t WHERE id = {{x.value}}', sqlite_connection))
|
|
235
|
+
.to eq("SELECT 1 /* a /* b */ FROM t WHERE id = '0 OR 1=1'")
|
|
236
|
+
end
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
describe 'on SQL Server' do
|
|
240
|
+
let(:request_context_variables) { { 'x.value' => '0 OR 1=1' } }
|
|
241
|
+
|
|
242
|
+
it 'leaves a nested block comment untouched' do
|
|
243
|
+
expect(inject('SELECT 1 /* a /* b */ {{x.value}} */ FROM t WHERE id = {{x.value}}', sqlserver_connection))
|
|
244
|
+
.to eq("SELECT 1 /* a /* b */ {{x.value}} */ FROM t WHERE id = '0 OR 1=1'")
|
|
245
|
+
end
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
it 'reads {{...}} with the grammar of the filter injector' do
|
|
249
|
+
expect(LiveQueryInjector::PLACEHOLDER.source).to include(ContextVariablesInjector::PLACEHOLDER.source)
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
describe 'value formats outside literals' do
|
|
253
|
+
let(:request_context_variables) do
|
|
254
|
+
{
|
|
255
|
+
'n.value' => '42',
|
|
256
|
+
'd.value' => '4.5',
|
|
257
|
+
'l.value' => '1,2,3',
|
|
258
|
+
'b.value' => 'FALSE',
|
|
259
|
+
'neg.value' => '-1',
|
|
260
|
+
's.value' => 'DESC'
|
|
261
|
+
}
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
it 'injects unsigned numbers, lists of numbers and booleans as they are, everything else as a string' do
|
|
265
|
+
query = 'SELECT {{n.value}}, {{d.value}}, ({{l.value}}), {{b.value}}, {{neg.value}}, {{s.value}}'
|
|
266
|
+
|
|
267
|
+
expect(inject(query, postgresql_connection))
|
|
268
|
+
.to eq("SELECT 42, 4.5, (1,2,3), FALSE, '-1', 'DESC'")
|
|
269
|
+
end
|
|
270
|
+
end
|
|
271
|
+
end
|
|
272
|
+
end
|
|
273
|
+
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: forest_liana
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 9.22.
|
|
4
|
+
version: 9.22.14
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Sandro Munda
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-10-
|
|
11
|
+
date: 2026-10-30 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rails
|
|
@@ -332,6 +332,7 @@ files:
|
|
|
332
332
|
- app/services/forest_liana/schema_utils.rb
|
|
333
333
|
- app/services/forest_liana/scope_manager.rb
|
|
334
334
|
- app/services/forest_liana/search_query_builder.rb
|
|
335
|
+
- app/services/forest_liana/single_record_selection.rb
|
|
335
336
|
- app/services/forest_liana/smart_action_field_validator.rb
|
|
336
337
|
- app/services/forest_liana/smart_action_form_parser.rb
|
|
337
338
|
- app/services/forest_liana/smart_field_dependencies.rb
|
|
@@ -350,6 +351,7 @@ files:
|
|
|
350
351
|
- app/services/forest_liana/utils/beta_schema_utils.rb
|
|
351
352
|
- app/services/forest_liana/utils/context_variables.rb
|
|
352
353
|
- app/services/forest_liana/utils/context_variables_injector.rb
|
|
354
|
+
- app/services/forest_liana/utils/live_query_injector.rb
|
|
353
355
|
- app/services/forest_liana/value_stat_getter.rb
|
|
354
356
|
- app/views/layouts/forest_liana/application.html.erb
|
|
355
357
|
- config/initializers/error-messages.rb
|
|
@@ -521,6 +523,7 @@ files:
|
|
|
521
523
|
- spec/services/forest_liana/line_stat_getter_spec.rb
|
|
522
524
|
- spec/services/forest_liana/pie_stat_getter_spec.rb
|
|
523
525
|
- spec/services/forest_liana/projection_parser_spec.rb
|
|
526
|
+
- spec/services/forest_liana/query_stat_getter_spec.rb
|
|
524
527
|
- spec/services/forest_liana/request_cache_spec.rb
|
|
525
528
|
- spec/services/forest_liana/resource_updater_spec.rb
|
|
526
529
|
- spec/services/forest_liana/resources_getter_composite_keys_spec.rb
|
|
@@ -536,6 +539,7 @@ files:
|
|
|
536
539
|
- spec/services/forest_liana/smart_field_dependencies_spec.rb
|
|
537
540
|
- spec/services/forest_liana/utils/context_variables_injector_spec.rb
|
|
538
541
|
- spec/services/forest_liana/utils/context_variables_spec.rb
|
|
542
|
+
- spec/services/forest_liana/utils/live_query_injector_spec.rb
|
|
539
543
|
- spec/services/forest_liana/value_stat_getter_spec.rb
|
|
540
544
|
- spec/spec_helper.rb
|
|
541
545
|
- spec/support/forest_liana/query_capture.rb
|
|
@@ -883,6 +887,7 @@ test_files:
|
|
|
883
887
|
- spec/services/forest_liana/line_stat_getter_spec.rb
|
|
884
888
|
- spec/services/forest_liana/pie_stat_getter_spec.rb
|
|
885
889
|
- spec/services/forest_liana/projection_parser_spec.rb
|
|
890
|
+
- spec/services/forest_liana/query_stat_getter_spec.rb
|
|
886
891
|
- spec/services/forest_liana/request_cache_spec.rb
|
|
887
892
|
- spec/services/forest_liana/resource_updater_spec.rb
|
|
888
893
|
- spec/services/forest_liana/resources_getter_composite_keys_spec.rb
|
|
@@ -898,6 +903,7 @@ test_files:
|
|
|
898
903
|
- spec/services/forest_liana/smart_field_dependencies_spec.rb
|
|
899
904
|
- spec/services/forest_liana/utils/context_variables_injector_spec.rb
|
|
900
905
|
- spec/services/forest_liana/utils/context_variables_spec.rb
|
|
906
|
+
- spec/services/forest_liana/utils/live_query_injector_spec.rb
|
|
901
907
|
- spec/services/forest_liana/value_stat_getter_spec.rb
|
|
902
908
|
- spec/spec_helper.rb
|
|
903
909
|
- spec/support/forest_liana/query_capture.rb
|