Skip to content

Commit bad5a93

Browse files
ampagentDanilo Radovic
andcommitted
Keep the API key file out of a Vercel CLI deploy
`go-live` on Vercel tells the person to run `npx vercel --prod`. The Vercel CLI uploads the project folder, reads `.vercelignore` rather than `.gitignore`, and its default ignore list does not cover `.patchstackrc.local.json`, so the API key was uploaded with the deploy. `go-live` now adds `.patchstackrc.local.json` to `.vercelignore` when the host is Vercel (creating the file if needed, never adding the line twice) and says so. If it cannot, it says why and asks the person to add it. `next` only reads, so its Vercel steps ask for the line instead. The printed `vercel env add` command now uses `--environment production`, the syntax the Vercel CLI documents. The CLI help, README, AGENT-INSTALL.md and the Claude plugin's `next` skill now say that `go-live` writes this one entry on Vercel. Amp-Thread-ID: https://ampcode.com/threads/T-2e659ca3-7bd0-4d70-b3d3-09638251060b Co-authored-by: Danilo Radovic <danilo@patchstack.com>
1 parent 6680ba6 commit bad5a93

9 files changed

Lines changed: 145 additions & 22 deletions

File tree

‎AGENT-INSTALL.md‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ Every command at a glance — what it does, whether it reads your source, what i
8989
| `next` | Say where the project stands and the one thing to do now: connect the account (with the link), or go live (with the same steps `go-live` prints, or asking where the app is hosted when the project does not say). Answers the "done" the summary ends on. | Reads the project files `guide` and `go-live` read | The connected note in `.patchstackrc.json`, when it changed | The site UUID, to the public claim page that says whether it is connected (skipped once `.patchstackrc.json` records that it is) |
9090
| `doctor [--url <live app>]` | Check that everything Patchstack needs works, and print each problem with its fix: the install, the site, the account, the guard's wiring, its rules file, the read-only rules credential, the map link, the build steps, and whether the published app fetches its rules. A problem only the app's owner can fix (connecting the account, publishing) is marked as theirs. Exits 1 when it finds a problem. | Reads the project files `guide` and `protect --check` read | Nothing | The site UUID, to the public claim page that says whether it is connected (skipped once `.patchstackrc.json` records that it is); the read-only rules credential from the guard's rules file, and the site key when this machine holds one, each once to `monitor/pulse/token`, to check Patchstack accepts them; one GET with that token to `monitor/pulse/sync-status/<your site uuid>`, which answers only when the published app (read-only credential) and a key-holding copy last fetched rules, and how many (doctor never fetches rules itself, which would be recorded as a fetch); with `--url`, two GET requests to that address: its page, and the same page with a test attack in a `file` parameter, which another firewall in front of the app may also flag |
9191
| `vulns` | Show what Patchstack found in this project's packages: vulnerabilities by severity and outdated packages, as counts, with the link that shows the details. Package names are shown in the dashboard once the site is connected. | No | Nothing | The site UUID, to the public claim page that returns the counts |
92-
| `go-live [--host <name>]` | Show how to add the project's API key to its host and deploy, in three steps. The host is read from project files (`vercel.json`, `netlify.toml`, `wrangler.toml`, `fly.toml`, `render.yaml`, `railway.json`, `amplify.yml`, `Procfile`, `.replit`, a Lovable project's packages) or named with `--host`. The commands it prints read the key from `.patchstackrc.local.json` as they run; it never prints the key. | Reads those project files only | Nothing | Nothing |
92+
| `go-live [--host <name>]` | Show how to add the project's API key to its host and deploy, in three steps. The host is read from project files (`vercel.json`, `netlify.toml`, `wrangler.toml`, `fly.toml`, `render.yaml`, `railway.json`, `amplify.yml`, `Procfile`, `.replit`, a Lovable project's packages) or named with `--host`. The commands it prints read the key from `.patchstackrc.local.json` as they run; it never prints the key. | Reads those project files only | On Vercel only, a `.vercelignore` entry for `.patchstackrc.local.json` (the file is created if missing): the Vercel CLI uploads the project folder and does not read `.gitignore` | Nothing |
9393
| `widget [--remove]` | Add the Patchstack Connector widget tag to the root page (in a workspace repo, the app package's root page), or take the tag Connect added out with `--remove` (a tag added by hand is left alone). Saves the choice as `"widget"` in `.patchstackrc.json`, which `scan` and `mark-build` follow; `setup --widget` saves the same choice. | Reads the root page to find where the tag goes | `.patchstackrc.json`; the widget `<script>` tag in the root HTML shell or framework root | Nothing |
9494
| `init <site-uuid>` | Optional: pre-seed `.patchstackrc.json` with an existing UUID. | No | `.patchstackrc.json` only | Nothing |
9595
| `mark-build` | Ensure the widget tag in built pages, and — **on a production build only** — stamp the live-site flag + build fingerprint. A local or preview build is stamped with neither, and has a stale marker removed; `--production` forces it for a build published by hand. Run as a `postbuild` step. | No | Build output only (`dist/ build/ out/ .output/public/ _site/`) — never source | The same manifest `scan` sent, plus one word for what it did with the marker — and only for a site already registered |
@@ -218,13 +218,15 @@ Only when the person asks you to publish the site. Start with `npx @patchstack/c
218218
```
219219

220220
A preview deploy runs without the prefix.
221-
- **A deploy that builds on the host** (`vercel --prod`, or any git-connected Netlify or Vercel site) labels production by itself, but the host never receives `.patchstackrc.local.json`. Before the first production deploy, put the key in the host's production settings, read straight from the file so it is never printed:
221+
- **A deploy that builds on the host** (`vercel --prod`, or any git-connected Netlify or Vercel site) labels production by itself, but the host must not receive `.patchstackrc.local.json`. Before the first production deploy, put the key in the host's production settings, read straight from the file so it is never printed:
222222

223223
```
224-
node -p "require('./.patchstackrc.local.json').apiKey" | npx vercel env add PATCHSTACK_API_KEY production --sensitive
224+
node -p "require('./.patchstackrc.local.json').apiKey" | npx vercel env add PATCHSTACK_API_KEY --environment production --sensitive
225225
npx netlify env:set PATCHSTACK_API_KEY "$(node -p "require('./.patchstackrc.local.json').apiKey")" --context production --secret
226226
```
227227

228+
`vercel --prod` uploads the project folder, and the Vercel CLI reads `.vercelignore`, not `.gitignore`. `go-live` on a Vercel project adds `.patchstackrc.local.json` to `.vercelignore`; if it says it could not, add the line yourself before deploying.
229+
228230
An app with a server needs this for runtime protection: the guard fetches its rules with the key. Without it the deploy is still recognised as long as the packages have not changed since the last scan, but nothing is protected. Never put the key in a committed file, in a public variable (`NEXT_PUBLIC_*`, `VITE_*`), or in your reply.
229231
- **A host that builds the site itself but is not named under "How each environment is labelled"** — ChatGPT Sites (`*.chatgpt.site`, published from Codex) is one — gives Patchstack nothing that says "production", so its builds report `local` and the live site is never recognised as deployed. In that host's environment settings for the published site, set two variables: `PATCHSTACK_API_KEY`, read from `.patchstackrc.local.json` as above, and `PATCHSTACK_ENVIRONMENT=production`. Set the label only where the published build reads it, never in a committed file, and not in settings a preview or the local dev server also reads. Then publish again: the deployed site keeps serving the build from before setup until it is rebuilt.
230232
- **Give Patchstack the live address when the host does not.** Patchstack confirms a deploy by reading the build marker off the live page, either when a visitor's browser loads the widget or through its own scheduled check of the site's address. A new site may get no visitors for days, so the scheduled check is often the only confirmation, and it can only read an address Patchstack has. Vercel, Netlify, Render and Railway production builds report their address themselves (see "Package and command behavior"). Anywhere else, including ChatGPT Sites, Lovable or Bolt, a custom domain, GitHub Pages, Cloudflare Pages or a folder uploaded by hand, take the public address from the deploy output or ask the person for it. Then:
@@ -1139,7 +1141,7 @@ Remove only the pieces that are actually present — check for each first. If no
11391141
4. **Remove the hooks from `package.json` scripts.** If a hook was chained (e.g. `"postbuild": "existing-command && patchstack-connect mark-build"`), remove only the `patchstack-connect …` part and keep the rest; if removal leaves a script empty, delete the key.
11401142
5. **Signal Patchstack that the package is being removed**: run `npx @patchstack/connect uninstall` (while the package is still installed and `.patchstackrc.json` still exists). If the site was never claimed, this deletes its anonymous record on Patchstack; if the site is claimed, it is only flagged — the record stays until its owner removes it in the dashboard. A failed signal must not stop the uninstall; continue with the remaining steps.
11411143
6. **Uninstall the package** with the manager matching the lockfile: `npm uninstall` / `pnpm remove` / `yarn remove` / `bun remove` `@patchstack/connect`. Don't hand-edit `node_modules` or the lockfile.
1142-
7. **Delete `.patchstackrc.json` and `.patchstackrc.local.json`** (the second holds the API key and is git-ignored, so it is present locally even when the repo shows nothing), remove the `.gitignore` entry setup added for it, and remove `PATCHSTACK_SITE_UUID`, `PATCHSTACK_API_KEY` (and public-prefixed variants like `NEXT_PUBLIC_PATCHSTACK_SITE_UUID`) from env files and CI variables.
1144+
7. **Delete `.patchstackrc.json` and `.patchstackrc.local.json`** (the second holds the API key and is git-ignored, so it is present locally even when the repo shows nothing), remove the `.gitignore` entry setup added for it (and the `.vercelignore` entry `go-live` added on Vercel), and remove `PATCHSTACK_SITE_UUID`, `PATCHSTACK_API_KEY` (and public-prefixed variants like `NEXT_PUBLIC_PATCHSTACK_SITE_UUID`) from env files and CI variables.
11431145
8. **Commit** the changes. Reporting stops immediately. On HTML shells the `window.__PATCHSTACK_PROD__` flag that `mark-build` stamped on production builds lives only in build output — the next build simply won't contain it (rebuild if build output is committed). On JSX roots and Astro layouts `scan` wrote the same marker into source; remove that managed `#region patchstack` block (or the hand-pasted equivalent) with the widget tag in step 2.
11441146

11451147
The `uninstall` signal is the only account-side effect local removal can have: it deletes an *unclaimed* (anonymous) record and merely flags a *claimed* one. A claimed site keeps using a site slot until its owner removes it in the dashboard at https://app.patchstack.com — end your report by telling the user this, alongside the site UUID from step 1. Never attempt to authenticate or remove a claimed site on the user's behalf.

‎README.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,9 @@ patchstack-connect vulns Show vulnerabilities by sever
195195
packages, with the link to the details
196196
patchstack-connect go-live [--host <name>] Show how to add your API key to your host
197197
(Vercel, Netlify, Cloudflare, Render, Railway,
198-
Fly.io, Heroku, Amplify, Replit, Lovable) and deploy
198+
Fly.io, Heroku, Amplify, Replit, Lovable) and deploy.
199+
On Vercel it adds .patchstackrc.local.json to
200+
.vercelignore
199201
patchstack-connect widget [--remove] Add the Patchstack widget to your page, or
200202
remove the tag Connect added; saved for builds
201203
patchstack-connect mark-build [--production] Stamp built HTML with a production flag +

‎claude-plugin/skills/next/SKILL.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ Run the command that matches what the person said, in the project root, and show
2929
| "What did Patchstack find?", "show me the vulnerabilities" | `npx @patchstack/connect vulns` |
3030
| "Show my Patchstack summary" | `npx @patchstack/connect summary` |
3131

32-
`next` checks with Patchstack whether the account is connected, so it sends the project's site ID; the others read the project only. None of them starts, builds or deploys the app, and you should not either: deploying is the person's step, and `go-live` prints how to do it on their host.
32+
`next` checks with Patchstack whether the account is connected, so it sends the project's site ID; the others read the project only, except that `go-live` on a Vercel project adds `.patchstackrc.local.json` to `.vercelignore` so a `vercel` deploy does not upload the key. None of them starts, builds or deploys the app, and you should not either: deploying is the person's step, and `go-live` prints how to do it on their host.
3333

3434
If the project has no `.patchstackrc.json`, Patchstack is not set up here yet: use the `setup` skill.

‎src/cli.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import {
4242
} from './demo.js';
4343
import {
4444
SECRET_CONFIG_FILENAME,
45+
ensureVercelIgnored,
4546
credentialInCommittedConfig,
4647
persistApiKey,
4748
secretFileIgnored,
@@ -227,7 +228,8 @@ Usage:
227228
host and deploy: the host is found from project
228229
files (vercel.json, netlify.toml, fly.toml, ...)
229230
or named with --host. Never prints the key.
230-
Sends nothing; writes nothing
231+
Sends nothing. On Vercel it adds
232+
.patchstackrc.local.json to .vercelignore
231233
patchstack-connect widget [--remove] Add the Patchstack widget to the project's page,
232234
or take the Connect-added tag out with --remove.
233235
Saves the choice in .patchstackrc.json so the
@@ -1953,7 +1955,10 @@ async function runWidget(args: ParsedArgs): Promise<number> {
19531955
return 0;
19541956
}
19551957

1956-
/** How to add the key to this project's host and deploy. Reads project files only; sends nothing. */
1958+
/**
1959+
* How to add the key to this project's host and deploy. Sends nothing; on Vercel it adds the credential file
1960+
* to `.vercelignore`, because the Vercel CLI uploads whatever that file does not exclude.
1961+
*/
19571962
async function runGoLive(args: ParsedArgs): Promise<number> {
19581963
const named = getStringFlag(args.flags, 'host');
19591964
let host = null;
@@ -1970,7 +1975,8 @@ async function runGoLive(args: ParsedArgs): Promise<number> {
19701975
console.error(`No ${SECRET_CONFIG_FILENAME} here yet. Run setup first: npx @patchstack/connect setup`);
19711976
return 1;
19721977
}
1973-
for (const line of renderGoLive(host, { useColor: useColor() })) console.log(line);
1978+
const vercelIgnore = host === 'vercel' ? await ensureVercelIgnored(process.cwd()) : undefined;
1979+
for (const line of renderGoLive(host, { useColor: useColor(), vercelIgnore })) console.log(line);
19741980
return 0;
19751981
}
19761982

‎src/config.ts‎

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -350,25 +350,31 @@ async function writeAtomicFile(target: string, content: string, mode: number): P
350350
}
351351

352352
/**
353-
* Add a line to the project's `.gitignore` if it is not already covered, and report whether it now is.
353+
* Add a line to the project's `.gitignore` (or another ignore file in the same syntax) if it is not
354+
* already covered, and report whether it now is.
354355
*
355356
* Deliberately conservative about the file: it appends one entry and never rewrites or reorders what is
356-
* there. A missing `.gitignore` is created, because a project without one is exactly the project that
357-
* would commit this.
357+
* there. A missing file is created, because a project without one is exactly the project that would
358+
* commit (or upload) this.
358359
*
359360
* Not conservative about the ANSWER. Every failure — unreadable file, unwritable file, an entry that is
360361
* there but negated further down — comes back as `ignored: false` with a reason, because the caller's next
361362
* line is either an assurance or a warning and it has to be the right one.
362363
*/
363-
export async function ensureIgnored(cwd: string, entry: string, label = 'Patchstack credential — never commit this'): Promise<{ ignored: boolean; reason?: string }> {
364-
const target = path.join(cwd, '.gitignore');
364+
export async function ensureIgnored(
365+
cwd: string,
366+
entry: string,
367+
label = 'Patchstack credential — never commit this',
368+
file = '.gitignore',
369+
): Promise<{ ignored: boolean; reason?: string }> {
370+
const target = path.join(cwd, file);
365371
let existing = '';
366372
try {
367373
existing = await readFile(target, 'utf8');
368374
} catch (err) {
369375
const code = (err as NodeJS.ErrnoException).code;
370376
if (code !== 'ENOENT') {
371-
return { ignored: false, reason: `.gitignore could not be read (${code ?? 'unknown error'})` };
377+
return { ignored: false, reason: `${file} could not be read (${code ?? 'unknown error'})` };
372378
}
373379
}
374380

@@ -381,7 +387,7 @@ export async function ensureIgnored(cwd: string, entry: string, label = 'Patchst
381387
} catch (err) {
382388
const code = (err as NodeJS.ErrnoException).code;
383389

384-
return { ignored: false, reason: `.gitignore could not be written (${code ?? 'unknown error'})` };
390+
return { ignored: false, reason: `${file} could not be written (${code ?? 'unknown error'})` };
385391
}
386392

387393
// Read back rather than assume. The write can succeed and still not produce coverage — a symlink into a
@@ -390,14 +396,25 @@ export async function ensureIgnored(cwd: string, entry: string, label = 'Patchst
390396
const written = await readFile(target, 'utf8');
391397
if (ignoresEntry(written, entry)) return { ignored: true };
392398

393-
return { ignored: false, reason: '.gitignore does not cover the file after writing it' };
399+
return { ignored: false, reason: `${file} does not cover the file after writing it` };
394400
} catch (err) {
395401
const code = (err as NodeJS.ErrnoException).code;
396402

397-
return { ignored: false, reason: `.gitignore could not be read back (${code ?? 'unknown error'})` };
403+
return { ignored: false, reason: `${file} could not be read back (${code ?? 'unknown error'})` };
398404
}
399405
}
400406

407+
/**
408+
* Keep the credential file out of a `vercel` CLI deploy.
409+
*
410+
* The Vercel CLI uploads the project folder and reads `.vercelignore`, not `.gitignore`, and its built-in
411+
* ignore list does not cover this file — so without the entry, `vercel --prod` ships the API key with the
412+
* deployment's source.
413+
*/
414+
export async function ensureVercelIgnored(cwd: string): Promise<{ ignored: boolean; reason?: string }> {
415+
return ensureIgnored(cwd, SECRET_FILENAME, 'Patchstack credential — never upload this', '.vercelignore');
416+
}
417+
401418
/**
402419
* Whether the project currently ignores the credential file.
403420
*

0 commit comments

Comments
 (0)