Skip to content

fix(config): a config write or audit shows text of a config file that PyYAML or ruamel.yaml cannot load #2451

Closed

Description

@dan-petty

Problem Description

Three config paths that do not load the settings still show text of a config file that PyYAML or ruamel.yaml cannot load. #1468 fixes this for the settings load (config/settings.py::_read_config_file). These paths stay:

  • config/write_history.py::_mapping runs yaml.safe_load on the old and the new bytes of each config write (record_write). remove_config_key and config set parse the file with ruamel.yaml, which accepts a local tag such as !vault. Then _mapping raises a PyYAML ConstructorError, and its message quotes the line with the value.
  • config/round_trip.py::find and the save of the round-trip catch only ruamel.yaml.YAMLError. ruamel.yaml raises ValueError for !!int <text> and KeyError for !!bool <text>, and the message quotes the value. The from None of these handlers also keeps the first error in __context__.
  • commands/config.py::_scan_yaml_for_secret_keys (devops config audit-keys) catches (yaml.YAMLError, OSError, ValueError). PyYAML raises KeyError for !!bool <text>, and the message quotes the value in lowercase.

Reproduction Steps

  1. Write ai:, provider: ollama and api_key: !vault FAKE-value-tag to a temporary file, and set DEVOPS_CLI_CONFIG to that file.
  2. Run devops config unset ai.provider.
  3. Or, in Python: from devops_cli.config.write_history import _mapping; _mapping(b"ai:\n api_key: !vault FAKE-value-tag\n").
  4. Or: from devops_cli.config import round_trip; round_trip.find("ai:\n api_key: !!int FAKE-value-int\n", "ai.provider", name="f").

Expected vs Actual Behavior

Expected: each path stops with a ConfigurationError that names the file and the type of the error, with the line and the column when the error has a mark. The error has no cause and no context, and no output holds text of the file. One shared helper can do this for all config reads.

Actual: devops config unset ai.provider exits 1, and its output holds api_key: !vault FAKE-value-tag three times: in the log line Command 'config' failed, in the plain traceback and in the Rich traceback. The command already wrote the file and made the .bak copy, so the audit record of the write is also lost. round_trip.find raises ValueError: invalid literal for int() with base 10: 'FAKE-value-int'.

Environment Diagnostics

devops-cli at branch fix/1468-config-parse-error-hides-line (on origin/main 4d5f789), PyYAML 6.0.3, ruamel.yaml from uv.lock, Python 3.14.

Logs, Traces, or Terminal Output

ConstructorError | could not determine a constructor for the tag '!vault'
  in "<byte string>", line 3, column 12:
      api_key: !vault FAKE-value-tag
               ^
ValueError | invalid literal for int() with base 10: 'FAKE-value-int'
KeyError | 'fake-value-b'

Pre-Submission Checklist

  • I have verified that no sensitive credentials, private keys, or internal tokens are contained in this issue report.
  • I have checked existing issues and pull requests to ensure this is not a duplicate.

Source: #1468

Proposed design

Problem statement

Three config code paths leak the text of a config file in error output when PyYAML or ruamel.yaml cannot parse a YAML tag. The exception messages quote the offending line, which exposes potentially secret values in tracebacks and logs. A shared helper must convert any YAML parse error into a ConfigurationError that names the file and error type without quoting file content.

Acceptance criteria

Requirement: shared-helper-raises-configuration-error

The shared helper SHALL raise a ConfigurationError that names the file path and the exception type, and SHALL include the line and column when the original exception carries a mark.

Scenario: helper-catches-constructor-error

  • WHEN yaml.safe_load raises a ConstructorError with a mark on line 3 column 12 for a file at /tmp/test.yaml
  • THEN the helper raises ConfigurationError with a message containing /tmp/test.yaml, ConstructorError, line 3, and column 12, and the message does not contain the value text from the file

Requirement: shared-helper-suppresses-cause-and-context

The shared helper SHALL raise the ConfigurationError with from None so that the exception has no cause and no context.

Scenario: helper-suppresses-context

  • WHEN the helper catches a ValueError that was raised inside a try block that already had an active exception
  • THEN the raised ConfigurationError has cause set to None and context set to None

Requirement: write-history-uses-helper

config/write_history.py::_mapping SHALL call the shared helper instead of calling yaml.safe_load directly, so that a ConstructorError or KeyError does not propagate with file content.

Scenario: write-history-vault-tag

  • WHEN _mapping receives bytes containing api_key: !vault FAKE-value-tag
  • THEN a ConfigurationError is raised and its message does not contain FAKE-value-tag

Requirement: round-trip-uses-helper

config/round_trip.py::find and the round-trip save SHALL call the shared helper instead of catching only ruamel.yaml.YAMLError, so that ValueError and KeyError from ruamel.yaml are converted to ConfigurationError.

Scenario: round-trip-int-tag

  • WHEN round_trip.find receives text containing api_key: !!int FAKE-value-int
  • THEN a ConfigurationError is raised and its message does not contain FAKE-value-int

Requirement: audit-keys-uses-helper

commands/config.py::_scan_yaml_for_secret_keys SHALL call the shared helper instead of catching (yaml.YAMLError, OSError, ValueError), so that a KeyError from PyYAML is converted to ConfigurationError.

Scenario: audit-keys-bool-tag

  • WHEN _scan_yaml_for_secret_keys reads a file containing a value with a !!bool tag that PyYAML cannot resolve
  • THEN a ConfigurationError is raised and its message does not contain the value text

Existing Tool Considered

PyYAML 6.0.3 and ruamel.yaml (from uv.lock) are the YAML parsers. Neither provides a safe error wrapper that suppresses file content in exception messages. The change builds a shared helper in the project's config package. Written cost: approximately 80 lines of Python for the helper and its unit tests, plus edits to three call sites in write_history.py, round_trip.py, and commands/config.py.

Key questions

  • Which exception types must the shared helper catch?: The helper SHALL catch YAMLError (covers PyYAML ConstructorError, ScannerError, ParserError), ValueError (ruamel.yaml !!int), KeyError (PyYAML and ruamel.yaml !!bool), and TypeError (defensive). It SHALL NOT catch OSError, which the caller handles separately for missing files. (proposed)
    • Rejected: Catch only YAMLError. Rejected. PyYAML raises KeyError for !!bool and ruamel.yaml raises ValueError for !!int. These are not subclasses of YAMLError.
    • Rejected: Catch YAMLError, ValueError, KeyError, TypeError. Chosen. Covers all observed exception types from both PyYAML and ruamel.yaml for unresolvable tags. TypeError is included defensively for edge cases in value conversion.
    • Rejected: Catch Exception broadly. Rejected. Too broad. Would mask programming errors such as AttributeError or RecursionError that indicate a real bug.
  • Where SHALL the shared helper live?: The helper SHALL be a function in the config package, importable by all three call sites. A module such as config/yaml_safe.py or a function in an existing config utility module is appropriate. (proposed)
    • Rejected: New module config/yaml_safe.py. Chosen. Keeps the helper in the config package where all three call sites already import from. A single function with a clear name is sufficient.
    • Rejected: Add to an existing utility module outside config/. Rejected. The helper is specific to config file parsing. Placing it outside the config package adds an unnecessary cross-package dependency.
    • Rejected: Duplicate the try/except in each call site. Rejected. The body states one shared helper. Duplication would make future exception-type additions a three-file change.
  • What SHALL the ConfigurationError message contain?: The message SHALL contain the file path, the exception class name, and the line and column from the mark when present. It SHALL NOT contain any text from the file. It SHALL use from None to suppress cause and context. (proposed)
    • Rejected: Include file path, exception type, line, column; exclude file text; use from None. Chosen. Matches the expected behavior stated in the body. The file path and error type give the user enough to diagnose the problem without exposing values.
    • Rejected: Include the full original exception message. Rejected. PyYAML and ruamel.yaml messages quote the offending line, which is the leak this issue fixes.
  • Does the helper need to handle both PyYAML and ruamel.yaml, or one parser per call site?: The helper SHALL accept the raw bytes or text and the file path, and SHALL call the parser that the caller specifies. write_history uses PyYAML safe_load; round_trip uses ruamel.yaml; _scan_yaml_for_secret_keys uses PyYAML. The helper wraps whichever parser the caller passes in. (proposed)
    • Rejected: Helper takes a parser callable and wraps it. Chosen. Each call site already uses a different parser. A generic wrapper that takes the callable keeps the helper simple and avoids duplicating parser-specific logic.
    • Rejected: Two helpers, one per parser. Rejected. The exception handling logic is identical. Two helpers would duplicate the message-building and from None logic.

Open questions

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

#1468

Activity

  1. added
    source/agent
    type/bug
    status/new
    value/medium
    effort/medium
    on Oct 28, 2026
  2. dan-petty commented on Oct 28, 2026

    @dan-petty
    OwnerAuthor

    Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones.

    • Type: type/bug. Three config paths leak file content in error output and miss exception types, which is a defect in existing behaviour.
    • Priority: P2-Medium. The leak can expose secret values in tracebacks, but it requires specific YAML tags such as !vault or !!int to trigger.
    • Value: Medium. Preventing secret values from appearing in error output protects users who use local YAML tags for sensitive config.
    • Effort: Medium. Three code paths need the correct exception handling and a shared helper must be created to replace the ad-hoc catches.
    • Related: fix(config): the secrets audit never prints a config line it cannot parse, and an unparsable file is not reported clean #1022, which asks for a different change. Both target _scan_yaml_for_secret_keys leaking config text on parse errors, but #1022 fixed the YAMLError case while the candidate fixes an uncaught KeyError in that path and two additional paths.
  3. added
    priority/p2-medium
    on Oct 28, 2026
  4. devops-cli-service commented on Oct 29, 2026

    @devops-cli-service
    Contributor

    Refine cannot set this item to Ready at 4a3242e until a person answers the questions below. Reply in a comment or edit the body, and the next refine round looks at the item again.

  5. added
    scope/config
    owner/agent
    status/in-progress
    status/in-review
    and removed
    status/new
    status/in-progress
    on Oct 30, 2026
  6. removed
    status/in-review
    owner/agent
    on Nov 5, 2026
  7. devops-cli-service commented on Nov 5, 2026

    @devops-cli-service
    Contributor
  8. devops-cli-service commented on Nov 5, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-05 00:33 UTC

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/mediumpriority/p2-mediumscope/configsource/agenttype/bugvalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions