Skip to content

Add trusted dir check for loading project config files - #405

Open
t1m0thyj wants to merge 20 commits into
mainfrom
fix/trust-config-dirs
Open

t1m0thyj wants to merge 20 commits into
mainfrom
fix/trust-config-dirs

Conversation

@t1m0thyj

@t1m0thyj t1m0thyj commented Jul 29, 2026 •

Copy link
Copy Markdown
Member

What It Does

Updates the ConfigFile.autodiscover_config_dir method to skip config files in world-writable directories and in directories that are not trusted.

  • These checks can be bypassed with the ZOWE_CONFIG_ALLOW_WORLD_WRITABLE and ZOWE_CONFIG_ALLOW_UNTRUSTED_DIRS environment variables.
  • In CI environments, the trusted directory check is skipped.
  • The current directory and any location inside the user's home directory are automatically trusted. Other trusted directories can be stored in the trustedConfigDirs setting of Zowe CLI's ~/.zowe/settings/imperative.json.
  • Users can be prompted interactively to trust a config directory by passing trust_prompt_fn to the ProfileManager or ConfigFile classes.

How to Test

  1. Create a directory structure for testing:
test-trust-config-dirs/
 |- child-dir/
 |- fake-home/
     |- .zowe/
 |- world-writable/   # Run `chmod 777 world-writable` to set perms
 |- test.py           # Contains the test script below
 |- zowe.config.json  # Run `zowe config init` to generate 
 |- zowe.schema.json
import os
from zowe.core_for_zowe_sdk import ProfileManager, TrustedConfigManager, credential_manager

def trust_prompt(path):
    return input(f"Do you trust {path}? ").lower() == "y"

credential_manager.HAS_KEYRING = False
# TrustedConfigManager().add_trusted(os.path.dirname(__file__))
profile = ProfileManager().load(profile_type="zosmf", validate_schema=False)
print("\n".join(f"{k}: {v}" for k, v in profile.items()))
  1. Test loading config from current directory
    • Run test.py and it should load profile successfully
  2. (Unix only) Test loading config from world-writable directory
    • Move the config and schema files into world-writable
    • Change directory into world-writable and run ../test.py- should fail with untrusted warning
    • Set ZOWE_CONFIG_ALLOW_WORLD_WRITABLE=1 and run ../test.py - should succeed
    • Unset the environment variable for subsequent tests
    • Move the config and schema files back to top-level directory
  3. Test loading config from outside home directory
    • Set HOME to the absolute path of fake-home
    • Change directory into child-dir and run ../test.py- should fail with untrusted warning
    • Set ZOWE_CONFIG_ALLOW_UNTRUSTED_DIRS=1 and run ../test.py - should succeed
    • Set CI=true instead and run ../test.py - should succeed
    • Unset last 2 environment variables for subsequent tests
  4. Test loading config from outside home directory with trust callback
    • Edit test.py to pass trust_prompt_fn=trust_prompt on the load call
    • Ensure custom HOME variable is still set
    • Run ../test.py and answer n to prompt - should fail with untrusted warning
    • Run ../test.py and answer y to prompt - should succeed
  5. Test loading config from outside home directory with trusted config API
    • Ensure custom HOME variable is still set
    • Change directory into child-dir and run ../test.py - should fail with untrusted warning
    • Edit test.py to uncomment the call to TrustedConfigManager
    • Run ../test.py again - should succeed
    • Ensure fake-home/.zowe/settings/imperative.json was created and contains path

Review Checklist
I certify that I have:

  • updated the changelog
  • manually tested my changes
  • added/updated automated unit/integration tests
  • created/ran system tests (provide build number if applicable)
  • followed the contribution guidelines

Additional Comments

Signed-off-by: Timothy Johnson <timothy.johnson@broadcom.com>
@github-project-automation github-project-automation Bot moved this to New Issues in Zowe CLI Squad Jul 29, 2026
@zowe-robot zowe-robot moved this from New Issues to In Progress in Zowe CLI Squad Jul 29, 2026
@JTonda JTonda moved this from In Progress to Review/QA in Zowe CLI Squad Aug 10, 2026
@JTonda JTonda moved this from Review/QA to In Progress in Zowe CLI Squad Aug 10, 2026
@t1m0thyj t1m0thyj moved this from In Progress to Release Backlog in Zowe CLI Squad Aug 26, 2026
@t1m0thyj t1m0thyj moved this from Release Backlog to In Progress in Zowe CLI Squad Aug 26, 2026
@t1m0thyj t1m0thyj moved this from In Progress to On Hold in Zowe CLI Squad Aug 26, 2026
@t1m0thyj t1m0thyj removed the Blocked label Nov 5, 2026
@t1m0thyj t1m0thyj moved this from On Hold to In Progress in Zowe CLI Squad Nov 5, 2026
Signed-off-by: Timothy Johnson <timothy.johnson@broadcom.com>
Co-Authored-by: Claude @model: Sonnet-5.5
Modified AI generated code: Yes
Dev Name: Timothy Johnson

Signed-off-by: Timothy Johnson <timothy.johnson@broadcom.com>
Co-Authored-by: Claude @model: Sonnet-5.5
Modified AI generated code: Yes
Dev Name: Timothy Johnson

Signed-off-by: Timothy Johnson <timothy.johnson@broadcom.com>
Co-Authored-by: Claude @model: Sonnet-5.5
Modified AI generated code: Yes
Dev Name: Timothy Johnson

Signed-off-by: Timothy Johnson <timothy.johnson@broadcom.com>
@codecov

codecov Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.82759% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.03%. Comparing base (51aa8a7) to head (af8dc11).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/zos_files/zowe/zos_files_for_zowe_sdk/uss.py 50.00% 3 Missing ⚠️
.../zos_files/zowe/zos_files_for_zowe_sdk/datasets.py 33.33% 2 Missing ⚠️
src/core/zowe/core_for_zowe_sdk/profile_manager.py 91.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #405      +/-   ##
==========================================
+ Coverage   85.53%   86.03%   +0.50%     
==========================================
  Files          49       50       +1     
  Lines        3001     3087      +86     
==========================================
+ Hits         2567     2656      +89     
+ Misses        434      431       -3     
Flag Coverage Δ
unittests 86.03% <94.82%> (+0.50%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Co-Authored-by: Claude @model: Sonnet-5.5
Modified AI generated code: Yes
Dev Name: Timothy Johnson

Signed-off-by: Timothy Johnson <timothy.johnson@broadcom.com>
@zFernand0
zFernand0 self-requested a review November 9, 2026 13:37
Signed-off-by: Timothy Johnson <timothy.johnson@broadcom.com>
@t1m0thyj
t1m0thyj force-pushed the fix/trust-config-dirs branch from a51f01c to 7ae229e Compare November 9, 2026 22:01
Signed-off-by: Timothy Johnson <timothy.johnson@broadcom.com>
Signed-off-by: Timothy Johnson <timothy.johnson@broadcom.com>
pujal0909 and others added 2 commits November 11, 2026 09:52
Co-Authored-by: Claude @model: Sonnet-5.5
Modified AI generated code: Yes
Dev Name: Timothy Johnson

Signed-off-by: Timothy Johnson <timothy.johnson@broadcom.com>
@t1m0thyj
t1m0thyj marked this pull request as ready for review November 12, 2026 20:46
@zowe-robot zowe-robot moved this from In Progress to Review/QA in Zowe CLI Squad Nov 12, 2026
Signed-off-by: Timothy Johnson <timothy.johnson@broadcom.com>
@CBforZ
CBforZ self-requested a review November 12, 2026 21:01

@CBforZ CBforZ left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Ran through the test steps and everything worked as expected

settings_json = self.__load_settings_json()
return set(settings_json[self.__settings_key])

def add_trusted(self, dir_path: str) -> None:

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

TODO: Require an absolute path to be provided

@zFernand0 zFernand0 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM! 😋

Left two small Qs about the intended behavior 🙏

path = os.path.join(current_dir, self.filename)
is_trusted = True

if os.path.isfile(path):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

isfile() seems to follow symlinks. I know we recently tried to prevent following them... should we add an extra check here for symbolic links?
os.path.islink(path) should do the trick

This is unrelated to the PR so it can be handled separately.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We could add an additional check and not os.path.islink(path), but I agree this is probably best handled in a follow-up PR - likely a breaking change and different from how Zowe CLI behaves.

Co-Authored-by: Claude @model: Sonnet-5.5
Modified AI generated code: Yes
Dev Name: Timothy Johnson

Signed-off-by: Timothy Johnson <timothy.johnson@broadcom.com>
Co-Authored-by: Claude @model: Sonnet-5.5
Modified AI generated code: Yes
Dev Name: Timothy Johnson

Signed-off-by: Timothy Johnson <timothy.johnson@broadcom.com>
@t1m0thyj
t1m0thyj force-pushed the fix/trust-config-dirs branch from bf6e626 to e66139f Compare November 16, 2026 21:49
Co-Authored-by: Claude @model: Sonnet-5.5
Modified AI generated code: Yes
Dev Name: Timothy Johnson

Signed-off-by: Timothy Johnson <timothy.johnson@broadcom.com>
@t1m0thyj
t1m0thyj requested review from anaxceron, awharn, jace-roell, pujal0909 and traeok and a lite review from Copilot November 16, 2026 21:51
@t1m0thyj t1m0thyj changed the title Add ownership check for loading project config files Add trusted dir check for loading project config files Nov 16, 2026

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

Active moderate and critical findings remain in trusted path validation, including inconsistent normalization and symlink-based containment bypasses.

2 open issues
What changed in this PR

Adds trusted-directory and world-writable-directory checks to project configuration autodiscovery, with environment overrides, CI support, interactive trust prompts, and persistent trusted-directory settings.

Changes:

  • Introduces TrustedConfigManager for trusted-directory persistence and security checks.
  • Updates ConfigFile and ProfileManager to enforce trust validation and support trust prompts.
  • Adds unit and integration coverage and documents the behavior in the changelog.
File Description
tests/​unit/​core/​test_trusted_config.py Adds unit tests for trusted-directory persistence and security-check behavior.
tests/​unit/​core/​test_profile_manager.py Adds tests for secure config autodiscovery, overrides, and trust prompt integration.
tests/​integration/​test_config_trust.py Adds integration coverage for trust behavior with real temporary directories.
src/​core/​zowe/​core_for_zowe_sdk/​trusted_config.py Implements trusted-directory management, persistence, environment overrides, and filesystem security checks.
src/​core/​zowe/​core_for_zowe_sdk/​profile_manager.py Propagates and caches optional trust prompt callbacks during profile loading.
src/​core/​zowe/​core_for_zowe_sdk/​config_file.py Applies trusted-directory and world-writable-directory checks during configuration autodiscovery.
src/​core/​zowe/​core_for_zowe_sdk/​__init__.py Exports TrustedConfigManager through the core package.
CHANGELOG.md Documents the new configuration-loading security behavior and related APIs.

Review effort: Lite

Comment on lines +103 to +104
if Path(dir_path).is_relative_to(Path.home()):
return True
Comment on lines +143 to +144
if not self.__is_valid_path(dir_path):
raise ValueError("Trusted directory path cannot be relative or a symbolic link")
Co-Authored-by: Claude @model: Sonnet-5.5
Modified AI generated code: Yes
Dev Name: Timothy Johnson

Signed-off-by: Timothy Johnson <timothy.johnson@broadcom.com>
Co-Authored-by: Claude @model: Sonnet-5.5
Modified AI generated code: Yes
Dev Name: Timothy Johnson

Signed-off-by: Timothy Johnson <timothy.johnson@broadcom.com>
Signed-off-by: Timothy Johnson <timothy.johnson@broadcom.com>

@traeok traeok left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The changes make sense to me, but the lint stage is failing. I plan to approve the PR once its resolved. I left a couple suggestions as well. Thanks Timothy

Comment on lines +169 to +173
settings_json = self.__load_settings_json()
if dir_path in settings_json[self.__settings_key]:
settings_json[self.__settings_key].remove(dir_path)
with open(self.local_storage_path, "w", encoding="utf-8") as fp:
fp.write(json.dumps(settings_json, indent=2) + "\n")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

(nitpick) This only removes the first instance of the directory path. Should we remove all instances to cover the edge case of users manually editing this file?

Comment on lines +109 to +111
def __is_valid_path(self, path) -> bool:
"""Return True if the path is absolute, normalized, and not a symlink."""
return os.path.isabs(path) and os.path.normpath(path) == path and not os.path.islink(path)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The symlink validation only checks the final component of the path, consider refactoring this to also reject symlinked parents.

Co-Authored-by: Claude @model: Sonnet-5.5
Modified AI generated code: Yes
Dev Name: Timothy Johnson

Signed-off-by: Timothy Johnson <timothy.johnson@broadcom.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Review/QA

Development

Successfully merging this pull request may close these issues.

7 participants