Skip to content

NH-155201: restrict the config file permission - #322

Open
xuan-cao-swi wants to merge 12 commits into
mainfrom
NH-155201
Open

xuan-cao-swi wants to merge 12 commits into
mainfrom
NH-155201

Conversation

@xuan-cao-swi

@xuan-cao-swi xuan-cao-swi commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Description

  1. Removed Dir.pwd auto-discovery: Config files are no longer searched for relative to the current working directory. Instead, at most one config file is loaded per process.
  2. Strict SW_APM_CONFIG_RUBY rules & precedence: When set, SW_APM_CONFIG_RUBY takes precedence over Rails initializers and must point to an absolute path to a file. Relative paths, directories, and missing files log a warning without falling back to other paths.
  3. Rails & non-Rails behavior: When SW_APM_CONFIG_RUBY is unset, only Rails apps load <Rails root>/config/initializers/solarwinds_apm.rb. Non-Rails apps no longer load a config file automatically and must explicitly set SW_APM_CONFIG_RUBY=/absolute/path/to/solarwinds_apm_config.rb (a warning is logged if a legacy solarwinds_apm_config.rb is found in the working directory).

Test (if applicable)

@xuan-cao-swi
xuan-cao-swi marked this pull request as ready for review November 13, 2026 18:05
@xuan-cao-swi
xuan-cao-swi requested review from a team as code owners November 13, 2026 18:05
Copilot AI balanced review requested due to automatic review settings November 13, 2026 18:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The permission check misses replaceable symlink targets in world-writable directories, while documentation overstates enforcement and tests do not assert the required warnings.

3 open issues
What changed in this PR

This PR updates how the SolarWinds APM Ruby agent selects configuration files and introduces permission warnings ahead of planned enforcement in the next release.

Changes:

  • Gives SW_APM_CONFIG_RUBY exclusive precedence and resolves default paths relative to the application root.
  • Warns about world-writable configuration files and non-sticky world-writable directories while continuing to load them.
  • Adds selection and permission tests, plus configuration documentation.
File Description
test/​solarwinds_apm/​config_test.rb Adds configuration precedence, root resolution, and permission scenarios.
lib/​solarwinds_apm/​config.rb Refactors file selection and adds permission warnings.
CONFIGURATION.md Documents selection precedence and configuration permission policy.

Review effort: Balanced

Comment thread lib/solarwinds_apm/config.rb Resolved
Comment thread CONFIGURATION.md Resolved
Comment thread test/solarwinds_apm/config_test.rb Resolved
Update configuration handling to warn instead of refuse loading world-writable files and directories. Adjust tests to reflect the new behavior.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Comment thread test/solarwinds_apm/config_test.rb Fixed
Comment thread test/solarwinds_apm/config_test.rb Fixed
Comment thread test/solarwinds_apm/config_test.rb Fixed
Comment thread test/solarwinds_apm/config_test.rb Fixed
Comment thread test/solarwinds_apm/config_test.rb Fixed
Comment thread test/solarwinds_apm/config_test.rb Fixed
@tammy-baylis-swi

Copy link
Copy Markdown
Contributor

I suggest we stop access to world-writable config files immediately and make it a breaking release, but maybe there is a key use Ruby case for step-wise? cc @cheempz

@cheempz

cheempz commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

I suggest we stop access to world-writable config files immediately and make it a breaking release, but maybe there is a key use Ruby case for step-wise? cc @cheempz

Agree. Also, similar to Python the permission check will not work on Windows (and would involve implementing a native extension to check the ACL). AI output:

Ruby’s world_writable? does not inspect Windows ACLs, and sticky-bit semantics are unsupported. Configuration loading should still work, but the checks cannot reliably identify insecure permissions.

I think it would be fair enough to highlight the permission check will not apply on Windows.

@xuan-cao-swi

Copy link
Copy Markdown
Contributor Author

Updated the comment for highlighting the permission check will not apply on Windows.
Stop access to world-writable config files now.

@cheempz cheempz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

More changes please. Also, do we know what happens when world_writable? is called for a file on Windows, meaning could it cause the trusted config check to return false? IMHO safer to check if system is posix and explicitly exclude (or ensure returns true) on Windows.

Comment thread test/solarwinds_apm/config_test.rb Resolved
Comment thread CONFIGURATION.md Resolved
@xuan-cao-swi

Copy link
Copy Markdown
Contributor Author

ruby world_writable? will return nil for all the file it checks, which make the function trusted_config_file? always return true.

Is python trying to implement native check like checking from cmd output like out, status = Open3.capture2e('icacls', config_file)

@cheempz

cheempz commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

ruby world_writable? will return nil for all the file it checks, which make the function trusted_config_file? always return true.

Is python trying to implement native check like checking from cmd output like out, status = Open3.capture2e('icacls', config_file)

@tammy-baylis-swi made a POC PR that that checked for effective user id of the file, it is not supported on Windows so we decided to not make the effective user id check at all, since her other changes addressed the security concerns. I think this check for permissions is a good idea--what i'm trying to point out is we don't know the behaviour of world_writeable? on all versions of Windows, so why not make this more deterministic--detect if on posix system, if so, go ahead with check. else skip check.

@tammy-baylis-swi

Copy link
Copy Markdown
Contributor

FYI this was the PoC PR: solarwinds/apm-python#938

@xuan-cao-swi

Copy link
Copy Markdown
Contributor Author

detect if on posix system, if so, go ahead with check. else skip check.

Do we allow this risk on windows? Does python have an approach to remove this kind of risk completely?

@cheempz

cheempz commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

detect if on posix system, if so, go ahead with check. else skip check.

Do we allow this risk on windows? Does python have an approach to remove this kind of risk completely?

For Python, the risk of maliciously crafted config that allowed writing a privileged file was removed via tighter sanitation of input/output so we decided there was no need to check the config file ownership agains effective user id of the application. Implementing it for Python on Windows would require either a subprocess that runs Powershell/CMD or dragging in native extension code dependency, neither seem worth it for the added security.

For Ruby, if you find that implementing the check on Windows is rather lightweight (doesn't require native extension or dependencies in the runtime), then it's worth doing. Otherwise i'm fine with allowing this risk on Windows if the changes in this and other PRs address the original issue posted in the PSIRT report.

@xuan-cao-swi

Copy link
Copy Markdown
Contributor Author

I see, I think to sanitize the config file makes more sense. For ruby, I was also thinking the subprocess (e.g. Open3.capture2e), but yeah, it seems not ideal

@xuan-cao-swi

Copy link
Copy Markdown
Contributor Author

Ruby uses .rb files to load configuration, which is very hard to sanitize. Unlike other languages that use JSON or YAML (such as OTel config files) which are easier to sanitize, loading an .rb file requires executing the code inside it.

I am thinking of changing the apm-ruby config file from an .rb file to a YAML or JSON file, aligning it with other languages and complying with the OTel specification.

For Rails, even though an .rb file is more favored because it loads from config/initializers/*.rb, I am thinking we could keep that .rb file without any default values and comment out all the options—while changing the gem defaults (solarwinds_apm_config.rb and ENV['SW_APM_CONFIG_RUBY']) to JSON or YAML.

Alternatively, we could completely drop the Rails initializer generator (for example, installing config via rails solarwinds_apm:install).

@cheempz

cheempz commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

I suggest the following:

  • no autodiscovery of the config file--for Rails it's already prescribed path. for non-Rails, require the absolute path to be explicitly given (not whatever is in instrumented process's current dir)
  • on POSIX system, verify not world writable and (bonus points) that the file is owned by root or by same effective user of the instrumented process
    • on non-POSIX system log an info message about skipping permission checks
  • add tests
  • make corresponding documentation changes

And agree we should switch the non-Rails SWO config to YAML or JSON to align with upstream esp. future declarative config--but let's do that in a separate PR.

@xuan-cao-swi

Copy link
Copy Markdown
Contributor Author

no autodiscovery of the config file--for Rails it's already prescribed path.

Just to confirm, we drop the discovery of the file solarwinds_apm_config.rb and only keep config/initializers/solarwinds_apm.rb for rails config file?

@cheempz

cheempz commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

no autodiscovery of the config file--for Rails it's already prescribed path.

Just to confirm, we drop the discovery of the file solarwinds_apm_config.rb and only keep config/initializers/solarwinds_apm.rb for rails config file?

Yep. For non-rails let's require the end user to specify SW_APM_CONFIG_RUBY https://github.com/solarwinds/apm-ruby/blob/main/CONFIGURATION.md#non-rails-applications

@xuan-cao-swi

xuan-cao-swi commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor Author

Updated the PR description as well

@xuan-cao-swi
xuan-cao-swi requested review from cheempz and a balanced review from Copilot November 23, 2026 19:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Critical path-validation gaps allow attackers to replace unchecked ancestor directories or retarget symlinks between validation and loading, enabling execution of untrusted Ruby code.

2 open issues
3 resolved since last review

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Permission checks allow untrusted group members to modify root-owned configuration files or replace entries in group-writable ancestor directories, enabling Ruby code execution with the application's privileges.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

High severity Reject group-writable configs and non-sticky ancestor directories

lib/​solarwinds_apm/​support/​secure_path.rb:75

The permission checks accept root-owned 0664 files and 0775 directories even when their groups include untrusted users. Those users can modify the config in place or replace entries in a non-sticky ancestor directory before loading, causing Ruby code to execute with the application's privileges. Reject group-writable files and group-writable non-sticky directories throughout the ancestor walk (also at line 75), unless an explicit trusted-group policy is enforced.

2 resolved since last review

Review effort: Balanced

##
# SecurePath
#
# Verifies that a file is safe to execute (e.g. load as Ruby code) with the application's privileges.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@xuan-cao-swi this seems very strongly worded and potentially misleading for what we're trying to achieve. We can only minimize risk by checking obviously bad permissions, not guarantee that a file is safe to execute. Can this module be somehing like PosixPermissionCheck instead?

end
end

def self.owner_untrusted_reason(subject, stat)

@cheempz cheempz Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@xuan-cao-swi sorry, i suggested the euid check, but from more research this could be an issue in containers if the config file is mounted from the host. Depending on the OS and container runtime, the mounted file's UID may or may not be remapped to a UID in the container (and even if remapped, it could very well be different from EUID of the application process). The remapping typically is enabled in Docker Desktop (which would mask this problem) but not in Linux. How about let's remove this check?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

i see a lot of digital ink was spilled in the CONFIGURATION section to document these cases ;)... can get rid of all that too.

@cheempz cheempz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@xuan-cao-swi i only looked at the secure_path.rb and CONFIGURATION, but wanted to get you this feedback first. Happy to go over together too if you'd like.

Comment thread CONFIGURATION.md
export SW_APM_CONFIG_RUBY=/etc/solarwinds_apm/config.rb
```

Configuration files are not auto-discovered for non-Rails applications. A `solarwinds_apm_config.rb` in the current working directory isn't loaded; the agent logs a warning instead.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
Configuration files are not auto-discovered for non-Rails applications. A `solarwinds_apm_config.rb` in the current working directory isn't loaded; the agent logs a warning instead.
> [!NOTE]
> Starting with version 8.0.0, the configuration file is no longer auto-discovered; a warning is logged for the deprecated behavior where `solarwinds_apm_config.rb` was loaded from the current working directory.

Comment thread CONFIGURATION.md
- **MySQL2**: `query` operations
- **PostgreSQL**: the "[exec-ish](https://github.com/solarwinds/apm-ruby/blob/main/lib/solarwinds_apm/patch/tag_sql/sw_pg_patch.rb#L15)" operations like `exec` and `query` are supported.

> **⚠️ Limitation:** Currently does not support prepared statements.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
> [!NOTE]
> Currently does not support prepared statements.

Comment thread CONFIGURATION.md
http_proxy=http://<username>:<password>@<proxyHost>:<proxyPort> ruby my.app
```

> **Note:** Starting with version 7.0.0, `SW_APM_PROXY` is deprecated in favor of standard HTTP proxy environment variables.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
> [!NOTE]
> Starting with version 7.0.0, `SW_APM_PROXY` is deprecated in favor of standard HTTP proxy environment variables.

Comment thread CONFIGURATION.md

These checks don't apply to the Rails initializer `config/initializers/solarwinds_apm.rb`, because Rails loads every file in `config/initializers` regardless. Protect it like the rest of your application code.

> **Note:** On non-POSIX systems (Windows), file permission bits aren't meaningful, so the agent skips these checks. Secure the configuration file with Windows ACLs instead.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
> **Note:** On non-POSIX systems (Windows), file permission bits aren't meaningful, so the agent skips these checks. Secure the configuration file with Windows ACLs instead.
> [!NOTE]
> File permissions is only checked on POSIX systems.

#
# Only the SW_APM_CONFIG_RUBY file is checked with SolarWindsAPM::SecurePath.trusted_path, and its
# validated real path is what gets loaded. The Rails initializer isn't checked, because Rails loads
# every file in config/initializers regardless.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This part is now making me think:

Rails loads every file in config/initializers regardless.

in line 54 we're calling load(config_file) if config_file. Does that mean if APM Ruby config is given as a Rails initializer, it ends up being loaded twice?

Comment thread CONFIGURATION.md

### Prerequisites

> **⚠️ Important:** Programmatic configuration requires disabling auto-configuration:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you add a rule in the AGENTS file to standardize docs on github markdown alerts and get it to update all instances? i've made some comment suggestions based on spot checking but there's probably more.

Comment thread CONFIGURATION.md

Configuration files are not auto-discovered for non-Rails applications. A `solarwinds_apm_config.rb` in the current working directory isn't loaded; the agent logs a warning instead.

#### Custom Location

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

let's remove this Custom Location section, it's redudant with the Config File Selection section added below.

Comment thread CONFIGURATION.md

At most one configuration file is loaded:

1. If `SW_APM_CONFIG_RUBY` is set, only that file is used. It must be an absolute path to a file. A relative path, a directory, or a missing file means no configuration file is loaded, and the agent doesn't fall back to the Rails initializer.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Minor but i am OCD and bothered by it, can we say APM Ruby instead of the agent everywhere in this doc?

Comment thread CONFIGURATION.md
1. If `SW_APM_CONFIG_RUBY` is set, only that file is used. It must be an absolute path to a file. A relative path, a directory, or a missing file means no configuration file is loaded, and the agent doesn't fall back to the Rails initializer.
2. Otherwise, for Rails applications only, the agent loads `config/initializers/solarwinds_apm.rb` from the Rails application root. The root is `Rails.root` when it's available, or the directory of the absolute `BUNDLE_GEMFILE` set by the Rails `config/boot.rb`. The current working directory is never searched.

### Config File Security

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this section can be a lot shorter now, i think it's enough to outline the permission check logic, no need to show how to use chmod.

@cheempz

cheempz commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Also, regarding the last copilot review:

Permission checks allow untrusted group members to modify root-owned configuration files or replace entries in group-writable ancestor directories, enabling Ruby code execution with the application's privileges.

This seems too restrictive, there could be valid use case (like CI/CD or infra-as-code) where the config file should be group writable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

5 participants