Mitigation Strategy: Strict Environment Variable Management (Next.js Specific)
-
Description:
NEXT_PUBLIC_
andSERVER_ONLY_
Prefixes: Enforce a strict naming convention. All client-side environment variables must use theNEXT_PUBLIC_
prefix. All server-side-only variables must use a custom prefix likeSERVER_ONLY_
.- Centralized Access (Next.js Context): Create a module (e.g.,
config.js
) that importsprocess.env
. This module selectively exports only the intended client-side variables (those withNEXT_PUBLIC_
) through clearly named functions or constants. Client components never directly accessprocess.env
. - Build-Time Validation (Next.js Build Process): Integrate a script into the Next.js build process (using
prebuild
orbuild
inpackage.json
). This script:- Runs after the Next.js build completes.
- Analyzes the generated client-side bundles (in the
.next
directory). - Uses a regular expression (or AST parsing) to search for any usage of
SERVER_ONLY_
prefixed variables within these bundles. - If found, the build fails with a descriptive error.
- Runtime Validation (
_app.js
): Within_app.js
(or a custom server), before any rendering, include a script that:- On the server (
typeof window === 'undefined'
), checks for the presence of requiredSERVER_ONLY_
variables. - On the client (
typeof window !== 'undefined'
), checks for the absence ofSERVER_ONLY_
variables. - Throws an error or prevents rendering if validation fails.
- On the server (
-
Threats Mitigated:
- Client-Side Exposure of Secrets (High Severity): Prevents accidental leakage of server-side secrets (API keys, etc.) to the client due to incorrect usage of Next.js's environment variable handling.
- Next.js Configuration Errors (Medium Severity): Reduces the risk of deploying a Next.js application with misconfigured environment variables, leading to unexpected behavior.
-
Impact:
- Client-Side Exposure of Secrets: Risk significantly reduced (near zero with full implementation). The multi-layered approach leverages Next.js's build process and runtime environment.
- Next.js Configuration Errors: Risk significantly reduced. Early detection during build and runtime prevents deployment of misconfigured applications.
-
Currently Implemented:
- Prefixes: Partially (inconsistent use of
SERVER_ONLY_
). - Centralized Access: Not implemented.
- Build-Time Validation: Not implemented.
- Runtime Validation: Not implemented.
- Prefixes: Partially (inconsistent use of
-
Missing Implementation:
- Consistent
SERVER_ONLY_
prefixing. config.js
module creation.- Build-time validation script.
- Runtime validation in
_app.js
.
- Consistent
Mitigation Strategy: SSRF Prevention in Next.js Data Fetching Functions
-
Description:
- URL Allowlist (Next.js Config): Maintain a strict allowlist of permitted domains and URL prefixes within the Next.js configuration (or carefully managed environment variables). This list is used exclusively by
getStaticProps
,getStaticPaths
, andgetServerSideProps
. - URL Validation (within Data Fetching): Inside
getStaticProps
,getStaticPaths
, andgetServerSideProps
, use the built-inURL
object (or a similar library) to parse all URLs before making any external requests. - Allowlist Enforcement: Before fetching data, compare the parsed URL's hostname and path against the allowlist. Reject the request if it doesn't match.
- Avoid Direct User Input (in URL Construction): Within these data fetching functions, never directly construct URLs from user-supplied input. Instead, use user input as keys to look up pre-defined, safe URLs from a configuration or database. If user input must be used, sanitize it rigorously before using it in any URL-related logic.
- URL Allowlist (Next.js Config): Maintain a strict allowlist of permitted domains and URL prefixes within the Next.js configuration (or carefully managed environment variables). This list is used exclusively by
-
Threats Mitigated:
- Server-Side Request Forgery (SSRF) in
getStaticProps
,getStaticPaths
,getServerSideProps
(High Severity): Prevents attackers from exploiting these Next.js server-side functions to access internal resources or make unauthorized requests.
- Server-Side Request Forgery (SSRF) in
-
Impact:
- SSRF: Risk significantly reduced. By strictly controlling the URLs accessed by these Next.js functions, the attack surface is minimized.
-
Currently Implemented:
- URL Allowlist: Not implemented.
- URL Validation: Partially (inconsistent).
- Allowlist Enforcement: Not implemented.
- Avoid Direct User Input: Partially.
-
Missing Implementation:
- Allowlist creation.
- Consistent URL parsing.
- Allowlist check implementation.
- Refactoring to avoid direct user input in URLs.
Mitigation Strategy: Safe Redirects with Next.js
-
Description:
- Prefer Relative Redirects (Next.js
redirect
): When using theredirect
object ingetStaticProps
,getServerSideProps
, ornext.config.js
, always prefer relative paths (e.g.,/login
) over absolute URLs. - URL Allowlist (for External Redirects - Next.js Config): If external redirects are unavoidable, maintain an allowlist of permitted domains within the Next.js configuration.
- Validation (within Redirect Logic): Before performing any redirect (using the
redirect
object), validate the destination URL:- If it's a relative path, no further validation is needed.
- If it's an absolute URL, use the
URL
object to parse it and check if the hostname matches the allowlist.
- Avoid User Input (in Redirect Destinations): Do not use user-supplied data directly to construct the
destination
property of theredirect
object. Use server-side logic or a lookup table.
- Prefer Relative Redirects (Next.js
-
Threats Mitigated:
- Open Redirects using Next.js
redirect
(Medium Severity): Prevents attackers from using the Next.js redirect functionality to redirect users to malicious sites.
- Open Redirects using Next.js
-
Impact:
- Open Redirects: Risk significantly reduced. Prioritizing relative redirects and validating absolute URLs against an allowlist effectively mitigates this vulnerability.
-
Currently Implemented:
- Relative Redirects: Partially.
- URL Allowlist: Not implemented.
- Validation: Not implemented.
- Avoid User Input: Partially.
-
Missing Implementation:
- Consistent use of relative redirects.
- Allowlist for external redirects.
- Validation logic for all redirects.
- Refactoring to avoid user input.
Mitigation Strategy: Migrate from getInitialProps
(Next.js Deprecation)
-
Description:
- Identify: Search the entire codebase for any usage of
getInitialProps
. - Refactor (to Next.js Recommended Methods): Replace every instance of
getInitialProps
with eithergetServerSideProps
(for server-side rendering on each request) orgetStaticProps
(for static generation at build time). Choose the appropriate method based on the component's data requirements. - Testing (Post-Migration): After refactoring, thoroughly test all affected components to ensure they function correctly and that no data is unintentionally exposed to the client. This is crucial because
getInitialProps
runs on both the server and client, while the replacements have clear server/client separation.
- Identify: Search the entire codebase for any usage of
-
Threats Mitigated:
- Data Exposure due to
getInitialProps
Misuse (High Severity): Eliminates the risk of accidentally exposing sensitive data to the client by using the deprecatedgetInitialProps
method, which has ambiguous execution context.
- Data Exposure due to
-
Impact:
- Data Exposure: Risk eliminated by migrating to
getServerSideProps
andgetStaticProps
, which have clearly defined server-side execution.
- Data Exposure: Risk eliminated by migrating to
-
Currently Implemented:
- Identify: Not performed.
- Refactor: Not performed.
- Testing: Not performed.
-
Missing Implementation:
- Codebase search for
getInitialProps
. - Complete refactoring to
getServerSideProps
orgetStaticProps
. - Thorough testing after migration.
- Codebase search for