Skip to content

ESD-1635: Validate the environment in WASM setAuthCredentials - #524

Open
Phil-Browne wants to merge 7 commits into
mainfrom
esd-1635-wasm-setauthcredentials-env
Open

ESD-1635: Validate the environment in WASM setAuthCredentials#524
Phil-Browne wants to merge 7 commits into
mainfrom
esd-1635-wasm-setauthcredentials-env

Conversation

@Phil-Browne

@Phil-Browne Phil-Browne commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

setAuthCredentials wrote its environment argument straight into MEGAPORT_ENVIRONMENT with no validation. Downstream, login_wasm.go's credential login path defaulted any unrecognized environment to the production API endpoint, so a typo, wrong case, or stray whitespace would silently authenticate real credentials against production instead of the intended environment.

  • setAuthCredentials now normalizes (lowercase, trim) and validates the environment the same way setAuthToken does, rejecting anything outside production/staging/development instead of storing it.
  • login_wasm.go's credential path now fails closed on an unrecognized environment instead of defaulting to production.
  • login_wasm.go's bearer-token login path had the same fail-open default; it now fails closed too instead of silently routing an unrecognized token environment to production.
  • The credential path also let the page-writable megaportCredentials JS global override an already-validated MEGAPORT_ENVIRONMENT, so a script sharing the page origin could redirect valid credentials to a different environment. It now only falls back to the global when the env var is empty, matching the token path's existing precedence.
  • Added tests covering rejection, normalization, the fail-closed switch default on both login paths, and the global-override precedence fix.

Ref: ESD-1635

setAuthCredentials wrote its environment argument straight into
MEGAPORT_ENVIRONMENT with no validation, unlike setAuthToken. An
unrecognized value (wrong case, typo, trailing whitespace) fell
through login_wasm.go's default case to the production API endpoint,
silently authenticating credentials against the wrong backend.

Normalize and validate the environment the same way setAuthToken
does, rejecting anything outside production/staging/development
instead of storing it. Also make login_wasm.go's credential path fail
closed on an unrecognized environment rather than defaulting to
production.
Add a follow-up comment on the pre-existing empty-env-defaults-to-
production path in login_wasm.go explaining why it's still safe,
use consistent raw-string error literals, update setAuthCredentials'
doc comment and TS type declaration to reflect the new validation,
and add missing test coverage for the too-few-args branch.
Copilot AI review requested due to automatic review settings July 10, 2026 16:16
@Phil-Browne
Phil-Browne requested review from a team and penzeliz-megaport as code owners July 10, 2026 16:16
@Phil-Browne
Phil-Browne requested a review from mega-alex July 10, 2026 16:16
@codecov

codecov Bot commented Jul 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.48%. Comparing base (6737d3b) to head (278b81c).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #524      +/-   ##
==========================================
- Coverage   79.48%   79.48%   -0.01%     
==========================================
  Files         193      193              
  Lines       18859    18859              
==========================================
- Hits        14991    14990       -1     
  Misses       2820     2820              
- Partials     1048     1049       +1     

see 1 file with indirect coverage changes

🚀 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.

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.

Pull request overview

Hardens WASM credential authentication by validating and canonicalizing the environment name so typos/case/whitespace can’t silently route API-key logins to production.

Changes:

  • Added restrictEnvironmentNameStrict to bucket only recognized environments and report whether the input matched.
  • Updated setAuthCredentials to normalize + strictly validate the environment before storing it, and to return the normalized bucket on success.
  • Updated login_wasm.go’s credential login path to fail closed on unknown environments, plus added/updated WASM tests and TS typings.

Reviewed changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
internal/wasm/wasm.go Adds strict environment bucketing and enforces normalized/validated environments in setAuthCredentials.
internal/wasm/wasm_test.go Adds tests for strict bucketing and setAuthCredentials normalization/rejection behavior.
internal/commands/config/login_wasm.go Makes credential login fail closed on unknown MEGAPORT_ENVIRONMENT instead of defaulting to production.
internal/commands/config/login_wasm_test.go Adds a WASM test ensuring unknown environments are rejected in the credential login path.
frontend-integration/types/megaport-wasm.d.ts Updates TypeScript docs/types to reflect normalization, rejection, and success payload shape for setAuthCredentials.

@penzeliz-megaport penzeliz-megaport left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reviewed for correctness; no findings. LGTM.

…credentials-env

# Conflicts:
#	internal/commands/config/login_wasm_test.go
#	internal/wasm/wasm_test.go
@Phil-Browne

Copy link
Copy Markdown
Contributor Author

Merge conflict resolved in 7e8a74c

Phil-Browne and others added 4 commits July 15, 2026 13:31
…credentials-env

# Conflicts:
#	internal/commands/config/login_wasm_test.go
Two fail-open gaps in loginFunc: the bearer-token path silently
routed an unrecognized environment to production instead of
rejecting it, and the credential path let the page-writable
megaportCredentials JS global override an already-validated
MEGAPORT_ENVIRONMENT env var. Both now fail closed, matching the
env-var-wins precedence already used elsewhere in this function.
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.

3 participants