Implement robust stage validation#215
Merged
GrahamCampbell merged 3 commits intomainfrom Apr 30, 2026
Merged
Conversation
There was a problem hiding this comment.
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 throwsINVALID_STAGEon 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.