Skip to content

Implement robust stage validation#215

Merged
GrahamCampbell merged 3 commits intomainfrom
robust-stage-validation
Apr 30, 2026
Merged

Implement robust stage validation#215
GrahamCampbell merged 3 commits intomainfrom
robust-stage-validation

Conversation

@GrahamCampbell
Copy link
Copy Markdown
Contributor

No description provided.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR introduces centralized, stricter stage name validation to prevent unsafe/invalid stage values (e.g., path traversal strings) from being accepted across CLI, configuration, dotenv loading, AWS provider behavior, and variable resolution.

Changes:

  • Added a shared stage regex (lib/utils/stage-pattern.js) and a single validation helper (lib/utils/validate-stage.js) that throws INVALID_STAGE on invalid input.
  • Wired stage validation into core stage resolution paths (Serverless constructor, Service, AWS provider, dotenv loading, config schema).
  • Expanded and updated unit tests to cover invalid stages across CLI, schema, dotenv, variables, and provider behavior.

Reviewed changes

Copilot reviewed 25 out of 25 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
lib/utils/stage-pattern.js Introduces the shared stage-name regex string.
lib/utils/validate-stage.js Adds centralized stage validation with a consistent error message/code.
lib/utils/resolve-stage.js Resolves stage using own-properties and validates stage values.
lib/serverless.js Validates CLI stage early in the constructor when provided.
lib/classes/service.js Validates stage passed via --stage/-s and validates stage used in function naming.
lib/plugins/aws/provider.js Ensures getStage() always returns a validated stage (or throws).
lib/cli/load-dotenv.js Validates stage before resolving .env.<stage> filenames.
lib/cli/conditionally-load-dotenv.js Uses shared resolveStage() for dotenv loading.
lib/config-schema.js Switches schema stage pattern to the shared stage-pattern module.
lib/classes/config-schema-handler/index.js Uses schema #/definitions/stage for provider stage validation.
lib/configuration/variables/sources/instance-dependent/param.js Validates resolved stage before resolving params.
lib/configuration/variables/sources/instance-dependent/get-sls.js Validates resolved stage before returning ${sls:stage}.
test/unit/lib/utils/validate-stage.test.js New unit tests for validateStage behavior and exports.
test/unit/lib/utils/resolve-stage.test.js Adds tests for invalid/empty/inherited stage handling in resolveStage.
test/unit/scripts/serverless.test.js Adds CLI-level test ensuring invalid --stage is rejected.
test/unit/lib/serverless.test.js Adds constructor-level stage validation tests.
test/unit/lib/plugins/aws/provider.test.js Adds AWS provider stage validation tests.
test/unit/lib/plugins/aws/package/compile/events/api-gateway/lib/validate.test.js Updates expected error message for invalid stage when http events exist.
test/unit/lib/cli/load-dotenv.test.js Adds tests ensuring invalid stages fail before reading dotenv files.
test/unit/lib/cli/conditionally-load-dotenv.test.js Adds tests for invalid stages when dotenv loading is enabled.
test/unit/lib/classes/service.test.js Adds tests for stage validation via short alias and provider stage usage.
test/unit/lib/config-schema.test.js Verifies schema uses the shared stage pattern (definitions + params keys).
test/unit/lib/classes/config-schema-handler/index.test.js Verifies provider stage schema uses shared stage pattern.
test/unit/lib/configuration/variables/sources/instance-dependent/param.test.js Adds variable-resolution error coverage when stage is invalid.
test/unit/lib/configuration/variables/sources/instance-dependent/get-sls.test.js Adds variable-resolution error coverage for invalid stage sources.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/configuration/variables/sources/instance-dependent/param.js Outdated
Comment thread lib/configuration/variables/sources/instance-dependent/get-sls.js Outdated
Comment thread test/unit/lib/serverless.test.js
@GrahamCampbell GrahamCampbell merged commit 5649ec1 into main Apr 30, 2026
4 checks passed
@GrahamCampbell GrahamCampbell deleted the robust-stage-validation branch April 30, 2026 23:03
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