Skip to content

environment_opts_out() fails open for a falsy non-string value (0, False) in an explicit mapping #46

Closed

Description

@dmccoystephenson

environment_opts_out() documents that "a value that is not a string -- opts out too", and the 0.5.0 release made every opt-out fail closed. A falsy non-string value in an explicit mapping still fails open, because (env.get(name) or "") turns it into a blank (unset) variable:

>>> from trace_client import environment_opts_out
>>> environment_opts_out({"TRACE_USAGE_REPORTING": "0"})
True
>>> environment_opts_out({"TRACE_USAGE_REPORTING": 0})
False
>>> environment_opts_out({"TRACE_USAGE_REPORTING": False})
False
>>> environment_opts_out({"TRACE_USAGE_REPORTING": []})
False

A truthy non-string (1, True) does opt out, since .strip() raises and the except returns True. That is the only case test_an_environment_that_cannot_be_read_disables_and_does_not_raise covers ({"DO_NOT_TRACK": 1}).

os.environ only ever holds strings, so the constructor's own check is not affected. The bug only shows up for a program that passes its own mapping (for example, settings parsed from YAML or TOML, where TRACE_USAGE_REPORTING: 0 becomes an int).

Suggested fix: treat only a missing (None) value as unset, and make any other non-string value opt out, as the docstring already says. String handling is unchanged, so no wire format, accepted value or disabled_reason changes, and trace-client-java (whose environment map is Map<String, String>) is unaffected.

This issue was drafted during a Gardener session (https://github.com/Stephenson-Software/gardener).


drafted by Claude on behalf of Daniel Stephenson

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions