Skip to content

Commit

Permalink
fix: encode URL user-inputted Ids to prevent injection
Browse files Browse the repository at this point in the history
  • Loading branch information
phiHero committed Sep 4, 2024
1 parent bf829be commit 5dfcb5c
Show file tree
Hide file tree
Showing 15 changed files with 30 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/typesense/alias.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def delete
private

def endpoint_path
"#{Aliases::RESOURCE_PATH}/#{@name}"
"#{Aliases::RESOURCE_PATH}/#{ERB::Util.url_encode(@name)}"
end
end
end
2 changes: 1 addition & 1 deletion lib/typesense/aliases.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def [](alias_name)
private

def endpoint_path(alias_name)
"#{Aliases::RESOURCE_PATH}/#{alias_name}"
"#{Aliases::RESOURCE_PATH}/#{ERB::Util.url_encode(alias_name)}"
end
end
end
2 changes: 1 addition & 1 deletion lib/typesense/analytics_rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def delete
private

def endpoint_path
"#{AnalyticsRules::RESOURCE_PATH}/#{@rule_name}"
"#{AnalyticsRules::RESOURCE_PATH}/#{ERB::Util.url_encode(@rule_name)}"
end
end
end
2 changes: 1 addition & 1 deletion lib/typesense/analytics_rules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def [](rule_name)
private

def endpoint_path(operation = nil)
"#{AnalyticsRules::RESOURCE_PATH}#{operation.nil? ? '' : "/#{operation}"}"
"#{AnalyticsRules::RESOURCE_PATH}#{operation.nil? ? '' : "/#{ERB::Util.url_encode(operation)}"}"
end
end
end
2 changes: 1 addition & 1 deletion lib/typesense/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def delete
private

def endpoint_path
"#{Collections::RESOURCE_PATH}/#{@name}"
"#{Collections::RESOURCE_PATH}/#{ERB::Util.url_encode(@name)}"
end
end
end
2 changes: 1 addition & 1 deletion lib/typesense/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def update(partial_document, options = {})
private

def endpoint_path
"#{Collections::RESOURCE_PATH}/#{@collection_name}#{Documents::RESOURCE_PATH}/#{@document_id}"
"#{Collections::RESOURCE_PATH}/#{ERB::Util.url_encode(@collection_name)}#{Documents::RESOURCE_PATH}/#{ERB::Util.url_encode(@document_id)}"
end
end
end
2 changes: 1 addition & 1 deletion lib/typesense/documents.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def delete(query_parameters = {})
private

def endpoint_path(operation = nil)
"#{Collections::RESOURCE_PATH}/#{@collection_name}#{Documents::RESOURCE_PATH}#{operation.nil? ? '' : "/#{operation}"}"
"#{Collections::RESOURCE_PATH}/#{ERB::Util.url_encode(@collection_name)}#{Documents::RESOURCE_PATH}#{operation.nil? ? '' : "/#{operation}"}"
end
end
end
2 changes: 1 addition & 1 deletion lib/typesense/key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def delete
private

def endpoint_path
"#{Keys::RESOURCE_PATH}/#{@id}"
"#{Keys::RESOURCE_PATH}/#{ERB::Util.url_encode(@id)}"
end
end
end
2 changes: 1 addition & 1 deletion lib/typesense/override.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def delete
private

def endpoint_path
"#{Collections::RESOURCE_PATH}/#{@collection_name}#{Overrides::RESOURCE_PATH}/#{@override_id}"
"#{Collections::RESOURCE_PATH}/#{ERB::Util.url_encode(@collection_name)}#{Overrides::RESOURCE_PATH}/#{ERB::Util.url_encode(@override_id)}"
end
end
end
2 changes: 1 addition & 1 deletion lib/typesense/overrides.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def [](override_id)
private

def endpoint_path(operation = nil)
"#{Collections::RESOURCE_PATH}/#{@collection_name}#{Overrides::RESOURCE_PATH}#{operation.nil? ? '' : "/#{operation}"}"
"#{Collections::RESOURCE_PATH}/#{ERB::Util.url_encode(@collection_name)}#{Overrides::RESOURCE_PATH}#{operation.nil? ? '' : "/#{ERB::Util.url_encode(operation)}"}"
end
end
end
2 changes: 1 addition & 1 deletion lib/typesense/preset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def delete
private

def endpoint_path
"#{Presets::RESOURCE_PATH}/#{@preset_name}"
"#{Presets::RESOURCE_PATH}/#{ERB::Util.url_encode(@preset_name)}"
end
end
end
2 changes: 1 addition & 1 deletion lib/typesense/presets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def [](preset_name)
private

def endpoint_path(operation = nil)
"#{Presets::RESOURCE_PATH}#{operation.nil? ? '' : "/#{operation}"}"
"#{Presets::RESOURCE_PATH}#{operation.nil? ? '' : "/#{ERB::Util.url_encode(operation)}"}"
end
end
end
2 changes: 1 addition & 1 deletion lib/typesense/synonym.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def delete
private

def endpoint_path
"#{Collections::RESOURCE_PATH}/#{@collection_name}#{Synonyms::RESOURCE_PATH}/#{@synonym_id}"
"#{Collections::RESOURCE_PATH}/#{ERB::Util.url_encode(@collection_name)}#{Synonyms::RESOURCE_PATH}/#{ERB::Util.url_encode(@synonym_id)}"
end
end
end
2 changes: 1 addition & 1 deletion lib/typesense/synonyms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def [](synonym_id)
private

def endpoint_path(operation = nil)
"#{Collections::RESOURCE_PATH}/#{@collection_name}#{Synonyms::RESOURCE_PATH}#{operation.nil? ? '' : "/#{operation}"}"
"#{Collections::RESOURCE_PATH}/#{ERB::Util.url_encode(@collection_name)}#{Synonyms::RESOURCE_PATH}#{operation.nil? ? '' : "/#{ERB::Util.url_encode(operation)}"}"
end
end
end
17 changes: 16 additions & 1 deletion spec/typesense/alias_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
require_relative 'shared_configuration_context'

describe Typesense::Alias do
subject(:books_alias) { typesense.aliases['books'] }
subject(:client) { typesense }

let(:books_alias) { typesense.aliases['books'] }

include_context 'with Typesense configuration'

Expand All @@ -21,6 +23,19 @@

expect(result).to eq('collection_name' => 'books_january')
end

it 'returns the specified alias with URI encoded name' do
stub_request(:get, Typesense::ApiCall.new(typesense.configuration).send(:uri_for, '/aliases/abc123%3F%3D%2B-_!%40%23%24%25%5E%26*()~%20%2F', typesense.configuration.nodes[0]))
.with(headers: {
'X-Typesense-Api-Key' => typesense.configuration.api_key,
'Content-Type' => 'application/json'
})
.to_return(status: 200, body: JSON.dump('collection_name' => 'books_january'), headers: { 'Content-Type': 'application/json' })

result = client.aliases["abc123?=+-_!@\#$%^&*()~ /"].retrieve

expect(result).to eq('collection_name' => 'books_january')
end
end

describe '#delete' do
Expand Down

0 comments on commit 5dfcb5c

Please sign in to comment.