forest_liana 9.22.13 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fcbf97d59c4b454c53e5f068f969e8e503e2a722c1a011e2d03b7f9e73a0c152
4
- data.tar.gz: cd6608d3c224d6daaedd240928a34cec77d49c35bfdf52fdb0d5b5187a23cf02
3
+ metadata.gz: 0ea33256d7cec4221e9c94bbe46f8bc4f7caaef29854b237cff1961aed4809dd
4
+ data.tar.gz: c80925513502ae243fabd0d0c6840483634940882226ccb1e6c686c0cbc52553
5
5
  SHA512:
6
- metadata.gz: 78dd9047c6f8647076342d0a99f223ad66c7c33590b6b8be4726901d4aa6b057eb97eb4137cd6de3852fcf06d505621b77e6d6b653bb699443e753b57955a782
7
- data.tar.gz: c0dc2372c158fb2f86929279b3594ae6aec5b2270ae6fd74a19871c7dc26af89b0b20d8d5f8a822ddd6147ab11d89413f80df582168ea31e41e7fac847c2e430
6
+ metadata.gz: f21a026ae94a6061ba804462b9e681dc4e43fa57ace54b627ca06fcec199d1ddbeebdbc701c94e4820d3e00de8bab105dc6acd3614db4f855318592b6957ab81
7
+ data.tar.gz: 0f8579fab129cd9ba90747c9c37a22de38bf4aa0bf89a210e4cee5a806129a9aee1b1adb5da22c8ede9925047478cf18fbceb79b394f5d5323b27f13a2e1ce08
@@ -13,14 +13,11 @@ module ForestLiana
13
13
  end
14
14
 
15
15
  def perform
16
- context_variables = Utils::ContextVariables.new(nil, nil, @params['contextVariables'])
17
- raw_query = Utils::ContextVariablesInjector.inject_context_in_value(@params['query'].strip, context_variables)
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
- if @params['record_id']
22
- raw_query.gsub!('?', @params['record_id'].to_s)
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']
@@ -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
- value = context_variables.get_value(context_variable_key)
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 = regex.match(value_with_context_variables_injected))
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
@@ -1,3 +1,3 @@
1
1
  module ForestLiana
2
- VERSION = "9.22.13"
2
+ VERSION = "9.22.14"
3
3
  end
@@ -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
@@ -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.13
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-23 00:00:00.000000000 Z
11
+ date: 2026-10-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -351,6 +351,7 @@ files:
351
351
  - app/services/forest_liana/utils/beta_schema_utils.rb
352
352
  - app/services/forest_liana/utils/context_variables.rb
353
353
  - app/services/forest_liana/utils/context_variables_injector.rb
354
+ - app/services/forest_liana/utils/live_query_injector.rb
354
355
  - app/services/forest_liana/value_stat_getter.rb
355
356
  - app/views/layouts/forest_liana/application.html.erb
356
357
  - config/initializers/error-messages.rb
@@ -522,6 +523,7 @@ files:
522
523
  - spec/services/forest_liana/line_stat_getter_spec.rb
523
524
  - spec/services/forest_liana/pie_stat_getter_spec.rb
524
525
  - spec/services/forest_liana/projection_parser_spec.rb
526
+ - spec/services/forest_liana/query_stat_getter_spec.rb
525
527
  - spec/services/forest_liana/request_cache_spec.rb
526
528
  - spec/services/forest_liana/resource_updater_spec.rb
527
529
  - spec/services/forest_liana/resources_getter_composite_keys_spec.rb
@@ -537,6 +539,7 @@ files:
537
539
  - spec/services/forest_liana/smart_field_dependencies_spec.rb
538
540
  - spec/services/forest_liana/utils/context_variables_injector_spec.rb
539
541
  - spec/services/forest_liana/utils/context_variables_spec.rb
542
+ - spec/services/forest_liana/utils/live_query_injector_spec.rb
540
543
  - spec/services/forest_liana/value_stat_getter_spec.rb
541
544
  - spec/spec_helper.rb
542
545
  - spec/support/forest_liana/query_capture.rb
@@ -884,6 +887,7 @@ test_files:
884
887
  - spec/services/forest_liana/line_stat_getter_spec.rb
885
888
  - spec/services/forest_liana/pie_stat_getter_spec.rb
886
889
  - spec/services/forest_liana/projection_parser_spec.rb
890
+ - spec/services/forest_liana/query_stat_getter_spec.rb
887
891
  - spec/services/forest_liana/request_cache_spec.rb
888
892
  - spec/services/forest_liana/resource_updater_spec.rb
889
893
  - spec/services/forest_liana/resources_getter_composite_keys_spec.rb
@@ -899,6 +903,7 @@ test_files:
899
903
  - spec/services/forest_liana/smart_field_dependencies_spec.rb
900
904
  - spec/services/forest_liana/utils/context_variables_injector_spec.rb
901
905
  - spec/services/forest_liana/utils/context_variables_spec.rb
906
+ - spec/services/forest_liana/utils/live_query_injector_spec.rb
902
907
  - spec/services/forest_liana/value_stat_getter_spec.rb
903
908
  - spec/spec_helper.rb
904
909
  - spec/support/forest_liana/query_capture.rb