# Environment audit

`auditEnv(text, { environment? })`, from the root export or
`dev-error-explainers/next-env-checker`, audits one pasted Next.js `.env` file.
It reads raw values locally and never returns input keys, values or original
lines. There is no network, filesystem access, environment mutation or command execution.

## Result and selection

```js
import { auditEnv } from 'dev-error-explainers';
auditEnv('NEXT_PUBLIC_API_KEY=synthetic', { environment: 'production' });
// { version: '0.1', status: 'audited', assignments: 1,
//   matched: true, redactions: 1, results: [...] }
```

- `version`: audit result version, currently `0.1`, not the package version.
- `status`: `audited` or `invalid-input`. Invalid input produces no findings.
- `assignments`: number of parsed assignments, including repeated keys.
- `matched`: whether any configured check produced a finding; not proof of a leak.
- `redactions`: number of recognised redaction operations on the input according
  to the existing `redact()` function. Not a count of all secrets, and not a count
  of audited values: no input value is returned, recognised or otherwise.
- `results`: findings in severity/line order. Each is a contract `Diagnosis`
  with family `next-env`, module `next-env-checker`, and an additional 1-based `line`.

Non-string input, text exceeding 204800 JavaScript characters or an invalid
environment produces `invalid-input`. Environments are exactly `unknown`
(default), `development`, `production`. Empty and comment-only files are valid.

`diagnose(text, { only: 'next-env', environment })` explicitly selects the same
checks and returns the original error contract envelope (without audit status,
assignment count or the extra line field). Automatic `diagnose(text)` and the
GitHub Action do not audit environment files. `detect(text)` can additionally
name `next-env-checker` when a NEXT_PUBLIC_ assignment yields a finding; it does
not assert that an entire file is safe or valid.

CLI: `dev-error-explainers --only env --environment production --json .env.local`.
Aliases: `env`, `next-env`, `next-env-checker`. JSON is the native audit result.
Completed audits exit 0, even with findings or no findings; usage/invalid input
exits 64. Other error-diagnosis modes keep their existing exit semantics.
The MCP tool is `audit_env`; see [MCP.md](MCP.md).

## Rules

| Rule | Signal | Interpretation and official source |
|---|---|---|
| `next-env.public-secret-name` | Non-empty NEXT_PUBLIC_ assignment with a secret-like name, except recognised public-name suffixes | A heuristic warning, not proof the value is secret. [Next.js](https://nextjs.org/docs/app/guides/environment-variables) |
| `next-env.public-supabase-secret` | NEXT_PUBLIC_ value with the sb_secret_ prefix | Privileged key-format exposure indicator. Does not verify key validity. [Supabase API keys](https://supabase.com/docs/guides/getting-started/api-keys) |
| `next-env.public-service-role` | NEXT_PUBLIC_ JWT payload claims service_role | Decoded claim, not signature verification or proof the key works. [Supabase API keys](https://supabase.com/docs/guides/getting-started/api-keys) |
| `next-env.duplicate-key` | Repeated key in one file | Last assignment wins within dotenv's parsed file. No conclusion about effective process.env. [dotenv parser](https://github.com/motdotla/dotenv/blob/v17.3.1/lib/main.js) |
| `next-env.invalid-line` | Non-comment line not consumed by the assignment grammar | Review the syntax; no claim that the framework necessarily throws. [dotenv parser](https://github.com/motdotla/dotenv/blob/v17.3.1/lib/main.js) |
| `next-env.production-localhost` | Explicit production context, URL/HOST/ENDPOINT/ORIGIN name and HTTP(S) URL with localhost, 127.0.0.1 or ::1 hostname | Check whether a loopback service is intentional. [URL Standard](https://url.spec.whatwg.org/#dom-url-hostname) |

## Parsing and limits

The assignment grammar is adapted from dotenv 17.3.1, with its BSD-2-Clause
notice included in THIRD_PARTY_NOTICES.md. Tests compare valid parsing examples
against that version of dotenv, installed only as a development dependency.

`export`, unquoted spaces, quoted multiline strings, comments, colon assignments
and dotted/hyphenated names are accepted by the targeted grammar. They are not
reported as errors merely for using those forms. Like dotenv, the parser can
accept some unmatched quotes as an unquoted value; this is not a complete syntax
linter. The package does not load files or implement dotenv-expand interpolation.

No expansion of `$VARIABLE` references, no cross-file precedence, no inspection
of source code or browser bundles, no inspection of a deployed environment and
no verification of credentials or JWT signatures. Each assignment is inspected,
including a value later overwritten by a duplicate; a warning is not proof the
value reached a deployed client. Server-only service-role keys are not labelled
leaks just for existing. An anon key without NEXT_PUBLIC_ is not automatically
an error: server-side use is valid.

The production loopback check requires an explicit context and checks only the
listed HTTP(S) hostnames. It does not recognise every possible local address or
resolve DNS. A zero-finding result is not a security certification.
