Skip to content

fix(macos): use literal key matching and safe awk replacement in upsert_env_value - #5871

Merged
gabsprogrammer merged 1 commit into
Osmantic:public-betafrom
vaibhavsrv:fix/macos-upsert-env-value-safe
Sep 19, 2026
Merged

gabsprogrammer merged 1 commit into
Osmantic:public-betafrom
vaibhavsrv:fix/macos-upsert-env-value-safe

Conversation

@vaibhavsrv

Copy link
Copy Markdown
Contributor

Why this matters

In ods/installers/macos/lib/env-generator.sh, upsert_env_value() checked for key existence using a regex pattern grep -qE "^${key}=" and updated values via sed -i '' "s|^${key}=.*|${key}=${value}|". As highlighted in #5819, regex key matching causes keys containing metacharacters (such as .) to clobber sibling variables (e.g. setting FOO.BAR matches and overwrites FOO_BAR). Additionally, using delimiter | in sed breaks if values contain pipe characters or ampersands (&), resulting in syntax errors or value corruption.

This patch replaces the regex probe and sed substitution with POSIX awk matching on literal key prefixes (index($0, k "=") == 1) and literal value reconstruction, preventing sibling key collisions and delimiter injection.

Validation

  • Repro baseline: Invoked upsert_env_value with key FOO.BAR against an environment file containing FOO_BAR=existing_value; confirmed baseline overwrote FOO_BAR with FOO.BAR.
  • Post-fix: Verified test_macos_upsert_env_value.py passes with exit code 0; FOO_BAR is preserved when FOO.BAR is added, and values containing delimiters (|, &) update cleanly without corruption.
  • Environment suites: 1 passed. New-test Ruff, ShellCheck, and diff checks pass; new regressions wired into Linux CI.

Overlap check

Risk / AI disclosure

AI-assisted investigation, implementation and CLI regressions. This strengthens key-matching and delimiter escaping in macOS installer environment generation, not runtime process admission. Independent human review and platform/runtime qualification remain gates. No running configuration, deployment or upstream merge changed.

Follow-up integration evidence

Composed with #5863 at 1de196a without conflicts. Production and test diffs passed together; macOS bridge and environment generation contracts remain intact.
Backlog composition was local-only (production/test diffs, excluding workflow/Makefile wiring); it is not an upstream merge or independent human approval. Declared live-review gates remain open.

@gabsprogrammer
gabsprogrammer merged commit 641e73f into Osmantic:public-beta Sep 19, 2026
43 checks passed
roshangupta00750 added a commit to roshangupta00750/ODS that referenced this pull request Sep 29, 2026
…ter-safe

Osmantic#5871 fixed upsert_env_value in installers/macos/lib/env-generator.sh to probe
and rewrite .env keys with a literal awk match, but installers/macos/ods-macos.sh
carries an identical copy that the fix missed. It still probes with a regex and
updates with sed:

    if grep -qE "^${key}=" "$env_file"; then
        sed -i '' "s|^${key}=.*|${key}=${value}|" "$env_file"

Two failures result on macOS, where this copy runs (HERMES session token,
WEBUI_AUTH, CPU budgets, SHIELD_API_KEY):

- A key containing regex metacharacters rewrites the wrong line. Setting
  FOO.BAR against an .env that has FOO_BAR=... matches it (`.` is a wildcard),
  and the sed rewrites FOO_BAR into FOO.BAR=..., destroying the original key.
- A value containing the sed delimiter `|` (a URL or token) aborts the update
  with `sed: bad flag in substitute command` and drops the write, while `&`
  and `\` are interpreted as sed replacement syntax.

Apply the same literal-prefix awk probe and awk rewrite Osmantic#5871 used, so the two
macOS copies behave identically.

tests/test_macos_upsert_env_value.py now parametrizes over both
env-generator.sh and ods-macos.sh; the ods-macos case fails on the base branch
(FOO_BAR clobbered by FOO.BAR) and passes with this change, on macOS and Linux.
shellcheck clean.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants