Skip to content

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

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

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

Conversation

@Phil-Browne

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.
  • Added tests covering rejection, normalization, and the fail-closed switch default.

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.29%. Comparing base (dee5b8d) to head (145a89e).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #524   +/-   ##
=======================================
  Coverage   79.29%   79.29%           
=======================================
  Files         193      193           
  Lines       18683    18683           
=======================================
  Hits        14814    14814           
  Misses       2820     2820           
  Partials     1049     1049           
🚀 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

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