-
Notifications
You must be signed in to change notification settings - Fork 228
fix: Ensure encoding errors handled during SQL obfuscation for Trilogy #345
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
Merged
arielvalentin
merged 4 commits into
open-telemetry:main
from
adrianna-chang-shopify:ac-trilogy-encoding-errors
Feb 27, 2023
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
cb81feb
Add test coverage for db_statement being configured via ENV var.
adrianna-chang-shopify d2abda5
Add test for invalid byte sequences when obfuscating SQL for Mysql2 i…
adrianna-chang-shopify ed9dec6
Ensure invalid byte sequences are encoded when obsfucating SQL for Tr…
adrianna-chang-shopify d4a33e0
Handle errors with SQL obfuscation
adrianna-chang-shopify File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -172,6 +172,19 @@ | |
| _(span.attributes['net.peer.name']).must_equal host.to_s | ||
| _(span.attributes['net.peer.port']).must_equal port.to_s | ||
| end | ||
|
|
||
| it 'encodes invalid byte sequences for db.statement' do | ||
| # \255 is off-limits https://en.wikipedia.org/wiki/UTF-8#Codepage_layout | ||
| sql = "SELECT * from users where users.id = 1 and users.email = '[email protected]\255'" | ||
| obfuscated_sql = 'SELECT * from users where users.id = ? and users.email = ?' | ||
|
|
||
| expect do | ||
| client.query(sql) | ||
| end.must_raise Mysql2::Error | ||
|
|
||
| _(span.name).must_equal 'mysql' | ||
| _(span.attributes['db.statement']).must_equal obfuscated_sql | ||
| end | ||
| end | ||
|
|
||
| describe 'when db_statement set as omit' do | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |
| port: port, | ||
| username: username, | ||
| password: password, | ||
| database: database, | ||
| ssl: false | ||
| } | ||
| end | ||
|
|
@@ -230,6 +231,19 @@ | |
| _(span.name).must_equal 'select' | ||
| _(span.attributes[OpenTelemetry::SemanticConventions::Trace::DB_STATEMENT]).must_equal obfuscated_sql | ||
| end | ||
|
|
||
| it 'encodes invalid byte sequences for db.statement' do | ||
| # \255 is off-limits https://en.wikipedia.org/wiki/UTF-8#Codepage_layout | ||
| sql = "SELECT * from users where users.id = 1 and users.email = '[email protected]\255'" | ||
| obfuscated_sql = 'SELECT * from users where users.id = ? and users.email = ?' | ||
|
|
||
| expect do | ||
| client.query(sql) | ||
| end.must_raise Trilogy::Error | ||
|
|
||
| _(span.name).must_equal 'mysql' | ||
| _(span.attributes[OpenTelemetry::SemanticConventions::Trace::DB_STATEMENT]).must_equal obfuscated_sql | ||
| end | ||
| end | ||
|
|
||
| describe 'when db_statement is set to omit' do | ||
|
|
@@ -245,5 +259,64 @@ | |
| _(span.attributes[OpenTelemetry::SemanticConventions::Trace::DB_STATEMENT]).must_be_nil | ||
| end | ||
| end | ||
|
|
||
| describe 'when db_statement is configured via environment variable' do | ||
| describe 'when db_statement set as omit' do | ||
| it 'omits db.statement attribute' do | ||
| OpenTelemetry::TestHelpers.with_env('OTEL_RUBY_INSTRUMENTATION_TRILOGY_CONFIG_OPTS' => 'db_statement=omit;') do | ||
| instrumentation.instance_variable_set(:@installed, false) | ||
| instrumentation.install | ||
| sql = "SELECT * from users where users.id = 1 and users.email = '[email protected]'" | ||
| expect do | ||
| client.query(sql) | ||
| end.must_raise Trilogy::Error | ||
|
|
||
| _(span.attributes['db.system']).must_equal 'mysql' | ||
| _(span.name).must_equal 'select' | ||
| _(span.attributes[OpenTelemetry::SemanticConventions::Trace::DB_STATEMENT]).must_be_nil | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe 'when db_statement set as obfuscate' do | ||
| it 'obfuscates SQL parameters in db.statement' do | ||
| OpenTelemetry::TestHelpers.with_env('OTEL_RUBY_INSTRUMENTATION_TRILOGY_CONFIG_OPTS' => 'db_statement=obfuscate;') do | ||
| instrumentation.instance_variable_set(:@installed, false) | ||
| instrumentation.install | ||
|
|
||
| sql = "SELECT * from users where users.id = 1 and users.email = '[email protected]'" | ||
| obfuscated_sql = 'SELECT * from users where users.id = ? and users.email = ?' | ||
| expect do | ||
| client.query(sql) | ||
| end.must_raise Trilogy::Error | ||
|
|
||
| _(span.attributes['db.system']).must_equal 'mysql' | ||
| _(span.name).must_equal 'select' | ||
| _(span.attributes['db.statement']).must_equal obfuscated_sql | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe 'when db_statement is set differently than local config' do | ||
| let(:config) { { db_statement: :omit } } | ||
|
|
||
| it 'overrides local config and obfuscates SQL parameters in db.statement' do | ||
| OpenTelemetry::TestHelpers.with_env('OTEL_RUBY_INSTRUMENTATION_TRILOGY_CONFIG_OPTS' => 'db_statement=obfuscate') do | ||
| instrumentation.instance_variable_set(:@installed, false) | ||
| instrumentation.install | ||
|
|
||
| sql = "SELECT * from users where users.id = 1 and users.email = '[email protected]'" | ||
| obfuscated_sql = 'SELECT * from users where users.id = ? and users.email = ?' | ||
| expect do | ||
| client.query(sql) | ||
| end.must_raise Trilogy::Error | ||
|
|
||
| _(span.attributes['db.system']).must_equal 'mysql' | ||
| _(span.name).must_equal 'select' | ||
| _(span.attributes['db.statement']).must_equal obfuscated_sql | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.