# VNX-SEC-006: Stripe secret key

Reports a hard-coded Stripe secret key (sk_live_ or sk_test_ plus 24 or more characters) in any file, excluding Stripe's published documentation keys.

| Field | Value |
|---|---|
| Rule ID | `VNX-SEC-006` |
| Severity | Critical |
| SARIF level | `error` |
| Kind | `secrets` |
| Languages | Any file |
| CWE | [CWE-798](https://cwe.mitre.org/data/definitions/798.html) |
| CAPEC | [CAPEC-191](https://capec.mitre.org/data/definitions/191.html) |
| ATT&CK | [T1552.001](https://attack.mitre.org/techniques/T1552/001/) |
| CWSS vector | `CWSS:1.0/TI:C/AP:A/AL:A/IC:N/FC:LT/RP:N/RL:A/AV:I/AS:N/IN:A/SC:A/BI:C/DI:H/EX:H/EC:N/P:W` |
| Tags | `secrets` `stripe` `payment` `credentials` |
| Source | [`internal/sast/rules/vnx-sec-006.rego`](https://github.com/Vulnesis/cli/blob/main/internal/sast/rules/vnx-sec-006.rego) |

## Overview

This rule detects Stripe secret API keys matching the patterns `sk_live_[0-9a-zA-Z]{24,}` and `sk_test_[0-9a-zA-Z]{24,}` in source files. Stripe secret keys are used to make authenticated requests to the Stripe API and provide complete control over a Stripe account: creating charges, issuing refunds, accessing customer payment methods, creating subscriptions, and downloading financial reports. Live keys (`sk_live_`) operate against real payment data; test keys (`sk_test_`) affect only the Stripe test environment but still represent a credential management failure.

## Why this matters

A leaked Stripe live secret key gives an attacker the ability to issue refunds draining your balance, create fraudulent charges to your customers' saved payment methods, download your entire customer and transaction database, create new API keys and webhooks for persistent access, and exfiltrate payout account details. Stripe tracks API usage and can flag unusual activity, but by the time a finding is raised significant damage may already be done.

Test keys (`sk_test_`) are less immediately dangerous but their presence indicates poor secrets hygiene that is likely to extend to live keys as well. Additionally, test mode can be used to enumerate customers and understand your integration before pivoting to a live key obtained elsewhere.

Note that Stripe also issues restricted keys and publishable keys, this rule specifically targets secret keys because they provide write access to the account.

## What gets flagged

A standalone `sk_live_` (live mode) or `sk_test_` (test mode) token of 24 or more alphanumeric characters: assigned to `stripe.api_key`, `Stripe.api_key` or `stripe.Key`, passed to `require('stripe')(...)` or `\Stripe\Stripe::setApiKey(...)`, or set in a `.env` file.

```python
# FLAGGED: secret key set on the SDK
stripe.api_key = "sk_live_<24+ characters>"
```

```javascript
// FLAGGED: key passed to the Node SDK
const stripe = require('stripe')('sk_live_<24+ characters>');
```

Not flagged:

- Stripe's published documentation test keys and placeholder runs (`sk_live_xxxxxxxx...`).
- Publishable keys (`pk_live_`, `pk_test_`), which are meant for browsers, and restricted keys (`rk_`, reported by VNX-SEC-321/322).
- Prefix checks and environment references (`os.environ["STRIPE_SECRET_KEY"]`).
- Test, fixture, vendored and generated files.

## Remediation

1. **Rotate the key immediately.** Log into the Stripe Dashboard → Developers → API keys → Roll key. Stripe lets you roll a key without downtime by generating a new key and providing a transition period.

2. **Check Stripe logs for unauthorized use.** In the Dashboard go to Developers → Logs. Filter by the date range when the key was exposed and look for unexpected charges, refund requests, or customer data access.

3. **Remove from source code.** Load from environment variables:

```python
# SAFE: load Stripe key from environment
import stripe
import os

stripe.api_key = os.environ['STRIPE_SECRET_KEY']

charge = stripe.Charge.create(
    amount=2000,
    currency='usd',
    source='tok_visa',
)
```

```javascript
// SAFE: load from environment in Node.js
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);
```

4. **Use Stripe restricted keys for limited-scope operations.** Rather than the full secret key, create a restricted key in the Dashboard that is scoped to only the API resources your code needs. For example, a webhook handler that only needs to read charge objects should not use a key with write access.

5. **Implement webhook signature verification** to ensure incoming webhook events are genuinely from Stripe rather than forged requests:

```python
# SAFE: verify webhook signatures
import stripe
import os

endpoint_secret = os.environ['STRIPE_WEBHOOK_SECRET']

event = stripe.Webhook.construct_event(
    payload, sig_header, endpoint_secret
)
```

6. **Scan git history** for the exposed key and rewrite history if found:

```bash
gitleaks detect --source . --verbose
git filter-repo --replace-text <(echo 'sk_live_xxxx==>REDACTED_STRIPE_KEY')
```

7. **Understand the difference between test and live keys.** Never use live keys in development or staging environments. Set up environment-specific configuration so `sk_test_` keys are used for non-production and `sk_live_` keys are used only in production, loaded exclusively from secure secrets storage.

## References

- [CWE-798: Use of Hard-coded Credentials](https://cwe.mitre.org/data/definitions/798.html)
- [Stripe: API keys documentation](https://stripe.com/docs/keys)
- [Stripe: Restricted API keys](https://stripe.com/docs/keys#limit-access)
- [Stripe: Webhook signature verification](https://stripe.com/docs/webhooks/signatures)
- [OWASP: Credentials Management Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Credentials_Management_Cheat_Sheet.html)
- [MITRE ATT&CK T1552.001 – Credentials In Files](https://attack.mitre.org/techniques/T1552/001/)

## Suppressing this rule

Put a `nosec` comment on the line, or on line 1 to cover the whole file. Use the comment marker of the file's language (`#`, `//`, `--` or `;`):

```text
# nosec vnx-sec-006
// nosec vnx-sec-006
```

Or record an ignore rule: `vulnetix ignore add --rule vnx-sec-006 --reason "..."`. See [Suppressing findings](/sast/guides/suppressing-findings.md).
