Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle URLs with invalid characters #2311

Merged
merged 4 commits into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/datadog/tracing/contrib/utils/quantization/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ module HTTP
def url(url, options = {})
url!(url, options)
rescue StandardError
options[:placeholder] || PLACEHOLDER
placeholder = options[:placeholder] || PLACEHOLDER

options[:base] == :exclude ? placeholder : "#{base_url(url)}/#{placeholder}"
end
lloeki marked this conversation as resolved.
Show resolved Hide resolved

def base_url(url, options = {})
lloeki marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -28,6 +30,12 @@ def base_url(url, options = {})
uri.query = nil
uri.fragment = nil
end.to_s
rescue StandardError
if (m = %r{([a-z]+://[^/]+)(?:/|$)}.match(url))
m[1]
else
''
end
end

def url!(url, options = {})
Expand Down
61 changes: 60 additions & 1 deletion spec/datadog/tracing/contrib/utils/quantization/http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,26 @@
# URLs do not permit unencoded non-ASCII characters in the URL.
let(:url) { 'http://example.com/path?繋がってて' }

it { is_expected.to eq(described_class::PLACEHOLDER) }
it { is_expected.to eq(format('http://example.com/%s', described_class::PLACEHOLDER)) }

context 'and base: :exclude' do
let(:options) { { base: :exclude } }

it { is_expected.to eq(described_class::PLACEHOLDER) }
end
end

context 'with unencoded ASCII characters' do
# URLs do not permit all ASCII characters to be unencoded in the URL.
let(:url) { 'http://example.com/|' }

it { is_expected.to eq(format('http://example.com/%s', described_class::PLACEHOLDER)) }

context 'and base: :exclude' do
let(:options) { { base: :exclude } }

it { is_expected.to eq(described_class::PLACEHOLDER) }
end
end

context 'with internal obfuscation and the default replacement' do
Expand All @@ -95,6 +114,46 @@
end
end

describe '#base_url' do
subject(:result) { described_class.base_url(url, options) }

let(:options) { {} }

context 'given a URL' do
let(:url) { 'http://example.com/path?category_id=1&sort_by=asc#featured' }

context 'default behavior' do
it { is_expected.to eq('http://example.com') }
end

context 'with Unicode characters' do
# URLs do not permit unencoded non-ASCII characters in the URL.
let(:url) { 'http://example.com/path?繋がってて' }

it { is_expected.to eq('http://example.com') }
end

context 'with unencoded ASCII characters' do
# URLs do not permit all ASCII characters to be unencoded in the URL.
let(:url) { 'http://example.com/|' }

it { is_expected.to eq('http://example.com') }
end

context 'without a base' do
let(:url) { '/foo' }

it { is_expected.to eq('') }
end

context 'that is entirely invalid' do
let(:url) { "\x00" }

it { is_expected.to eq('') }
end
end
end

describe '#query' do
subject(:result) { described_class.query(query, options) }

Expand Down