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

Guard injection against old rubies #4140

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

lloeki
Copy link
Contributor

@lloeki lloeki commented Nov 21, 2024

What does this PR do?

Add a known-processable frontend script to the main injection script.

Motivation:

Main injection script may be attempted to be loaded via RUBYOPT=-r'/path/to/host_inject.rb' but be unprocessable.

Change log entry

Add safe NOOP injection script for very old rubies

Additional Notes:

How to test the change?

These versions may be unable to execute - or even parse - the main host
injection script.
@lloeki lloeki requested a review from a team as a code owner November 21, 2024 15:17
@github-actions github-actions bot added the single-step Single Step APM Instrumentation label Nov 21, 2024
gemfile = Bundler::SharedHelpers.default_gemfile
lockfile = Bundler::SharedHelpers.default_lockfile

datadog_gemfile = gemfile.dirname + '.datadog-Gemfile'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Quality Violation

Consider using string interpolation or formatting instead of concatenation. (...read more)

The rule "Avoid string concatenation" is an important coding practice in Ruby for ensuring efficient and clean code. String concatenation in Ruby using the '+' operator creates a new string object, which can lead to excessive memory usage and slower performance when dealing with large strings or performing the operation multiple times.

Instead, Ruby provides alternatives that are more efficient. The string interpolation syntax #{} allows you to insert variables directly into strings without creating new string objects. This is not only more memory efficient, but also provides cleaner and more readable code.

Another alternative is the format method, which allows you to create a formatted string with placeholders for variables. This method is particularly useful when dealing with more complex strings, as it provides a clear and concise way to format your strings.

By following this rule, you can write more efficient and cleaner Ruby code, leading to better performance and readability.

View in Datadog  Leave us feedback  Documentation

lockfile = Bundler::SharedHelpers.default_lockfile

datadog_gemfile = gemfile.dirname + '.datadog-Gemfile'
datadog_lockfile = lockfile.dirname + '.datadog-Gemfile.lock'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Quality Violation

Consider using string interpolation or formatting instead of concatenation. (...read more)

The rule "Avoid string concatenation" is an important coding practice in Ruby for ensuring efficient and clean code. String concatenation in Ruby using the '+' operator creates a new string object, which can lead to excessive memory usage and slower performance when dealing with large strings or performing the operation multiple times.

Instead, Ruby provides alternatives that are more efficient. The string interpolation syntax #{} allows you to insert variables directly into strings without creating new string objects. This is not only more memory efficient, but also provides cleaner and more readable code.

Another alternative is the format method, which allows you to create a formatted string with placeholders for variables. This method is particularly useful when dealing with more complex strings, as it provides a clear and concise way to format your strings.

By following this rule, you can write more efficient and cleaner Ruby code, leading to better performance and readability.

View in Datadog  Leave us feedback  Documentation

env = { 'BUNDLE_GEMFILE' => datadog_gemfile.to_s,
'DD_TRACE_SKIP_LIB_INJECTION' => 'true',
'GEM_PATH' => utils.path }
add_output, add_status = Open3.capture2e(env, bundle_add_cmd)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 Code Vulnerability

Potential shell injection, check inputs are not coming from untrusted data (...read more)

This rule checks for potential shell injection vulnerabilities in your Ruby code. Shell injection is a serious security risk, as it allows an attacker to execute arbitrary commands on your system, potentially leading to data theft, corruption, or other malicious actions. When user input is used to form shell commands, it is essential to ensure that it cannot be manipulated to alter the intended command execution.

The importance of this rule cannot be overstated. Preventing shell injection attacks is a critical aspect of maintaining the security and integrity of your application and its data. Shell injections can lead to severe consequences, including unauthorized access, data breaches, and system compromise.

To avoid shell injection vulnerabilities, always use the array form of system commands in Ruby, such as system("echo", "Hello, World!") instead of system("echo Hello, World!"). The array form ensures that the arguments are passed directly to the command and not interpreted by the shell. Also, avoid using user input directly in shell commands. If it's unavoidable, make sure to sanitize the input thoroughly before using it. Use libraries such as Shellwords.escape to escape any potentially dangerous characters in the user input.

View in Datadog  Leave us feedback  Documentation

require 'open3'
require 'json'

Open3.capture2e(fowarder, 'library_entrypoint', stdin_data: payload.to_json)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 Code Vulnerability

Potential shell injection, check inputs are not coming from untrusted data (...read more)

This rule checks for potential shell injection vulnerabilities in your Ruby code. Shell injection is a serious security risk, as it allows an attacker to execute arbitrary commands on your system, potentially leading to data theft, corruption, or other malicious actions. When user input is used to form shell commands, it is essential to ensure that it cannot be manipulated to alter the intended command execution.

The importance of this rule cannot be overstated. Preventing shell injection attacks is a critical aspect of maintaining the security and integrity of your application and its data. Shell injections can lead to severe consequences, including unauthorized access, data breaches, and system compromise.

To avoid shell injection vulnerabilities, always use the array form of system commands in Ruby, such as system("echo", "Hello, World!") instead of system("echo Hello, World!"). The array form ensures that the arguments are passed directly to the command and not interpreted by the shell. Also, avoid using user input directly in shell commands. If it's unavoidable, make sure to sanitize the input thoroughly before using it. Use libraries such as Shellwords.escape to escape any potentially dangerous characters in the user input.

View in Datadog  Leave us feedback  Documentation

@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.77%. Comparing base (b0b8015) to head (9f0a245).
Report is 6 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4140      +/-   ##
==========================================
- Coverage   97.78%   97.77%   -0.02%     
==========================================
  Files        1350     1350              
  Lines       81322    81322              
  Branches     4107     4107              
==========================================
- Hits        79522    79512      -10     
- Misses       1800     1810      +10     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.


🚨 Try these New Features:

@pr-commenter
Copy link

pr-commenter bot commented Nov 21, 2024

Benchmarks

Benchmark execution time: 2024-11-21 15:57:59

Comparing candidate commit 9f0a245 in PR branch lloeki/guard-injection-against-old-rubies with baseline commit 0dec342 in branch master.

Found 0 performance improvements and 0 performance regressions! Performance is the same for 31 metrics, 2 unstable metrics.

utils.debug 'Fork not supported... skipping injection'
telemetry.emit(pid, utils.version, [{ name: 'library_entrypoint.abort', tags: ['reason:fork_not_supported'] }])
end
if RUBY_VERSION.start_with?('2.5.')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be >= ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
single-step Single Step APM Instrumentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants