Skip to content

Commit 9a94fa5

Browse files
ClearlyClairejesseplusplus
authored andcommitted
Fix error-prone SQL queries (mastodon#15828)
* Fix error-prone SQL queries in Account search While this code seems to not present an actual vulnerability, one could easily be introduced by mistake due to how the query is built. This PR parameterises the `to_tsquery` input to make the query more robust. * Harden code for Status#tagged_with_all and Status#tagged_with_none Those two scopes aren't used in a way that could be vulnerable to an SQL injection, but keeping them unchanged might be a hazard. * Remove unneeded spaces surrounding tsquery term * Please CodeClimate * Move advanced_search_for SQL template to its own function This avoids one level of indentation while making clearer that the SQL template isn't build from all the dynamic parameters of advanced_search_for. * Add tests covering tagged_with, tagged_with_all and tagged_with_none * Rewrite tagged_with_none to avoid multiple joins and make it more robust * Remove obsolete brakeman warnings * Revert "Remove unneeded spaces surrounding tsquery term" The two queries are not strictly equivalent. This reverts commit 86f16c5.
1 parent f4af8f2 commit 9a94fa5

2 files changed

Lines changed: 92 additions & 64 deletions

File tree

config/brakeman.ignore

Lines changed: 11 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -60,56 +60,6 @@
6060
"confidence": "High",
6161
"note": ""
6262
},
63-
{
64-
"warning_type": "Cross-Site Scripting",
65-
"warning_code": 2,
66-
"fingerprint": "71cf98c8235b5cfa9946b5db8fdc1a2f3a862566abb34e4542be6f3acae78233",
67-
"check_name": "CrossSiteScripting",
68-
"message": "Unescaped model attribute",
69-
"file": "app/views/admin/disputes/appeals/_appeal.html.haml",
70-
"line": 7,
71-
"link": "https://brakemanscanner.org/docs/warning_types/cross_site_scripting",
72-
"code": "t((Unresolved Model).new.strike.action, :scope => \"admin.strikes.actions\", :name => content_tag(:span, (Unresolved Model).new.strike.account.username, :class => \"username\"), :target => content_tag(:span, (Unresolved Model).new.account.username, :class => \"target\"))",
73-
"render_path": [
74-
{
75-
"type": "template",
76-
"name": "admin/disputes/appeals/index",
77-
"line": 20,
78-
"file": "app/views/admin/disputes/appeals/index.html.haml",
79-
"rendered": {
80-
"name": "admin/disputes/appeals/_appeal",
81-
"file": "app/views/admin/disputes/appeals/_appeal.html.haml"
82-
}
83-
}
84-
],
85-
"location": {
86-
"type": "template",
87-
"template": "admin/disputes/appeals/_appeal"
88-
},
89-
"user_input": "(Unresolved Model).new.strike",
90-
"confidence": "Weak",
91-
"note": ""
92-
},
93-
{
94-
"warning_type": "SQL Injection",
95-
"warning_code": 0,
96-
"fingerprint": "75fcd147b7611763ab6915faf8c5b0709e612b460f27c05c72d8b9bd0a6a77f8",
97-
"check_name": "SQL",
98-
"message": "Possible SQL injection",
99-
"file": "lib/mastodon/snowflake.rb",
100-
"line": 87,
101-
"link": "https://brakemanscanner.org/docs/warning_types/sql_injection/",
102-
"code": "connection.execute(\"CREATE OR REPLACE FUNCTION timestamp_id(table_name text)\\nRETURNS bigint AS\\n$$\\n DECLARE\\n time_part bigint;\\n sequence_base bigint;\\n tail bigint;\\n BEGIN\\n time_part := (\\n -- Get the time in milliseconds\\n ((date_part('epoch', now()) * 1000))::bigint\\n -- And shift it over two bytes\\n << 16);\\n\\n sequence_base := (\\n 'x' ||\\n -- Take the first two bytes (four hex characters)\\n substr(\\n -- Of the MD5 hash of the data we documented\\n md5(table_name || '#{SecureRandom.hex(16)}' || time_part::text),\\n 1, 4\\n )\\n -- And turn it into a bigint\\n )::bit(16)::bigint;\\n\\n -- Finally, add our sequence number to our base, and chop\\n -- it to the last two bytes\\n tail := (\\n (sequence_base + nextval(table_name || '_id_seq'))\\n & 65535);\\n\\n -- Return the time part and the sequence part. OR appears\\n -- faster here than addition, but they're equivalent:\\n -- time_part has no trailing two bytes, and tail is only\\n -- the last two bytes.\\n RETURN time_part | tail;\\n END\\n$$ LANGUAGE plpgsql VOLATILE;\\n\")",
103-
"render_path": null,
104-
"location": {
105-
"type": "method",
106-
"class": "Mastodon::Snowflake",
107-
"method": "define_timestamp_id"
108-
},
109-
"user_input": "SecureRandom.hex(16)",
110-
"confidence": "Medium",
111-
"note": ""
112-
},
11363
{
11464
"warning_type": "Mass Assignment",
11565
"warning_code": 105,
@@ -151,15 +101,15 @@
151101
"note": ""
152102
},
153103
{
154-
"warning_type": "Mass Assignment",
155-
"warning_code": 105,
156-
"fingerprint": "ab5035dd1a9f8c3a8d92fb2c37e8fe86fede4f87c91b71aa32e89c9eede602fc",
157-
"check_name": "PermitAttributes",
158-
"message": "Potentially dangerous key allowed for mass assignment",
159-
"file": "app/controllers/api/v1/notifications_controller.rb",
160-
"line": 81,
161-
"link": "https://brakemanscanner.org/docs/warning_types/mass_assignment/",
162-
"code": "params.permit(:account_id, :types => ([]), :exclude_types => ([]))",
104+
"warning_type": "SQL Injection",
105+
"warning_code": 0,
106+
"fingerprint": "9ccb9ba6a6947400e187d515e0bf719d22993d37cfc123c824d7fafa6caa9ac3",
107+
"check_name": "SQL",
108+
"message": "Possible SQL injection",
109+
"file": "lib/mastodon/snowflake.rb",
110+
"line": 87,
111+
"link": "https://brakemanscanner.org/docs/warning_types/sql_injection/",
112+
"code": "connection.execute(\" CREATE OR REPLACE FUNCTION timestamp_id(table_name text)\\n RETURNS bigint AS\\n $$\\n DECLARE\\n time_part bigint;\\n sequence_base bigint;\\n tail bigint;\\n BEGIN\\n time_part := (\\n -- Get the time in milliseconds\\n ((date_part('epoch', now()) * 1000))::bigint\\n -- And shift it over two bytes\\n << 16);\\n\\n sequence_base := (\\n 'x' ||\\n -- Take the first two bytes (four hex characters)\\n substr(\\n -- Of the MD5 hash of the data we documented\\n md5(table_name ||\\n '#{SecureRandom.hex(16)}' ||\\n time_part::text\\n ),\\n 1, 4\\n )\\n -- And turn it into a bigint\\n )::bit(16)::bigint;\\n\\n -- Finally, add our sequence number to our base, and chop\\n -- it to the last two bytes\\n tail := (\\n (sequence_base + nextval(table_name || '_id_seq'))\\n & 65535);\\n\\n -- Return the time part and the sequence part. OR appears\\n -- faster here than addition, but they're equivalent:\\n -- time_part has no trailing two bytes, and tail is only\\n -- the last two bytes.\\n RETURN time_part | tail;\\n END\\n $$ LANGUAGE plpgsql VOLATILE;\\n\")",
163113
"render_path": null,
164114
"location": {
165115
"type": "method",
@@ -216,8 +166,8 @@
216166
"type": "template",
217167
"template": "admin/trends/links/_preview_card"
218168
},
219-
"user_input": "(Unresolved Model).new.url",
220-
"confidence": "Weak",
169+
"user_input": "MediaAttachment.attached.find_by!(:shortcode => ((params[:id] or params[:medium_id]))).file.url(:original)",
170+
"confidence": "High",
221171
"note": ""
222172
},
223173
{

spec/models/status_spec.rb

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,87 @@
279279

280280
context 'when given one tag' do
281281
it 'returns the expected statuses' do
282-
expect(Status.tagged_with([tag1.id]).reorder(:id).pluck(:id).uniq).to match_array([status1.id, status5.id])
283-
expect(Status.tagged_with([tag2.id]).reorder(:id).pluck(:id).uniq).to match_array([status2.id, status5.id])
284-
expect(Status.tagged_with([tag3.id]).reorder(:id).pluck(:id).uniq).to match_array([status3.id, status5.id])
282+
expect(Status.tagged_with([tag1.id]).reorder(:id).pluck(:id).uniq).to eq [status1.id, status5.id]
283+
expect(Status.tagged_with([tag2.id]).reorder(:id).pluck(:id).uniq).to eq [status2.id, status5.id]
284+
expect(Status.tagged_with([tag3.id]).reorder(:id).pluck(:id).uniq).to eq [status3.id, status5.id]
285+
end
286+
end
287+
288+
context 'when given multiple tags' do
289+
it 'returns the expected statuses' do
290+
expect(Status.tagged_with([tag1.id, tag2.id]).reorder(:id).pluck(:id).uniq).to eq [status1.id, status2.id, status5.id]
291+
expect(Status.tagged_with([tag1.id, tag3.id]).reorder(:id).pluck(:id).uniq).to eq [status1.id, status3.id, status5.id]
292+
expect(Status.tagged_with([tag2.id, tag3.id]).reorder(:id).pluck(:id).uniq).to eq [status2.id, status3.id, status5.id]
293+
end
294+
end
295+
end
296+
297+
describe '.tagged_with_all' do
298+
let(:tag1) { Fabricate(:tag) }
299+
let(:tag2) { Fabricate(:tag) }
300+
let(:tag3) { Fabricate(:tag) }
301+
let!(:status1) { Fabricate(:status, tags: [tag1]) }
302+
let!(:status2) { Fabricate(:status, tags: [tag2]) }
303+
let!(:status3) { Fabricate(:status, tags: [tag3]) }
304+
let!(:status4) { Fabricate(:status, tags: []) }
305+
let!(:status5) { Fabricate(:status, tags: [tag1, tag2]) }
306+
307+
context 'when given one tag' do
308+
it 'returns the expected statuses' do
309+
expect(Status.tagged_with_all([tag1.id]).reorder(:id).pluck(:id).uniq).to eq [status1.id, status5.id]
310+
expect(Status.tagged_with_all([tag2.id]).reorder(:id).pluck(:id).uniq).to eq [status2.id, status5.id]
311+
expect(Status.tagged_with_all([tag3.id]).reorder(:id).pluck(:id).uniq).to eq [status3.id]
312+
end
313+
end
314+
315+
context 'when given multiple tags' do
316+
it 'returns the expected statuses' do
317+
expect(Status.tagged_with_all([tag1.id, tag2.id]).reorder(:id).pluck(:id).uniq).to eq [status5.id]
318+
expect(Status.tagged_with_all([tag1.id, tag3.id]).reorder(:id).pluck(:id).uniq).to eq []
319+
expect(Status.tagged_with_all([tag2.id, tag3.id]).reorder(:id).pluck(:id).uniq).to eq []
320+
end
321+
end
322+
end
323+
324+
describe '.tagged_with_none' do
325+
let(:tag1) { Fabricate(:tag) }
326+
let(:tag2) { Fabricate(:tag) }
327+
let(:tag3) { Fabricate(:tag) }
328+
let!(:status1) { Fabricate(:status, tags: [tag1]) }
329+
let!(:status2) { Fabricate(:status, tags: [tag2]) }
330+
let!(:status3) { Fabricate(:status, tags: [tag3]) }
331+
let!(:status4) { Fabricate(:status, tags: []) }
332+
let!(:status5) { Fabricate(:status, tags: [tag1, tag2, tag3]) }
333+
334+
context 'when given one tag' do
335+
it 'returns the expected statuses' do
336+
expect(Status.tagged_with_none([tag1.id]).reorder(:id).pluck(:id).uniq).to eq [status2.id, status3.id, status4.id]
337+
expect(Status.tagged_with_none([tag2.id]).reorder(:id).pluck(:id).uniq).to eq [status1.id, status3.id, status4.id]
338+
expect(Status.tagged_with_none([tag3.id]).reorder(:id).pluck(:id).uniq).to eq [status1.id, status2.id, status4.id]
339+
end
340+
end
341+
342+
context 'when given multiple tags' do
343+
it 'returns the expected statuses' do
344+
expect(Status.tagged_with_none([tag1.id, tag2.id]).reorder(:id).pluck(:id).uniq).to eq [status3.id, status4.id]
345+
expect(Status.tagged_with_none([tag1.id, tag3.id]).reorder(:id).pluck(:id).uniq).to eq [status2.id, status4.id]
346+
expect(Status.tagged_with_none([tag2.id, tag3.id]).reorder(:id).pluck(:id).uniq).to eq [status1.id, status4.id]
347+
end
348+
end
349+
end
350+
351+
describe '.permitted_for' do
352+
subject { described_class.permitted_for(target_account, account).pluck(:visibility) }
353+
354+
let(:target_account) { alice }
355+
let(:account) { bob }
356+
let!(:public_status) { Fabricate(:status, account: target_account, visibility: 'public') }
357+
let!(:unlisted_status) { Fabricate(:status, account: target_account, visibility: 'unlisted') }
358+
let!(:private_status) { Fabricate(:status, account: target_account, visibility: 'private') }
359+
360+
let!(:direct_status) do
361+
Fabricate(:status, account: target_account, visibility: 'direct').tap do |status|
362+
Fabricate(:mention, status: status, account: account)
285363
end
286364
end
287365

0 commit comments

Comments
 (0)