Leaked keys and key expiry
A virtual key that ends up in a public repository, a chat log or a shared screenshot keeps working until somebody turns it off. This page covers the three controls that close that gap: a public report endpoint that blocks a leaked key the moment a scanner or a person reports it, a maximum key lifetime that gives every key an end date, and expiry emails that reach the key owner 30 days and 1 day before a key stops working. The lifetime cap and the expiry emails are part of key hygiene and need a license granting key_hygiene. The report endpoint is in every edition.
A real-world scenario
A developer pastes a working script into a public repository with the team's virtual key still in it. A secret scanner that watches the organization's repositories finds the key a few minutes later and posts it to POST /public/compromised_key on your gateway. The gateway blocks the key at once, records who reported it and from which address, posts a high alert to your alerting channel and emails the key's owner. The developer creates a replacement key in the Admin UI, and the leaked one never serves another request.
Because every key of that team also has a maximum lifetime of one year, even a key that nobody reports stops working on its own, and the owner has had an email a month before and a day before telling them so.
Report a leaked key
POST /public/compromised_key takes the leaked key as it was found. It needs no authentication because the key itself proves the caller has it. When the key matches an active virtual key, that key is blocked at once. Right after the answer is sent, the gateway logs a WARNING line naming the key alias, the team, the source and the reporter's address, the operators get a compromised_key_reported alert, and the key's owner gets a Virtual Key Compromised email when email is configured. When audit logging is on, through store_audit_logs: true or an Enterprise license, an audit row records the block with spanway_changed_by set to compromised_key_report and the source and reporter_ip in its after value. On a Starter gateway without audit logging, the WARNING line is the record of the block.
The answer is always 202 {"status": "accepted"}, whether or not a key matched, so the body never tells a caller which keys exist. The log line, the audit row, the alert and the owner email are written after the answer is sent. Answering for a matching key takes one more database read than for an unknown key, so the gateway does not promise identical response times. That does not help an attacker, because a caller who holds a key can already test it by calling any model route with it. Only raw keys starting with sk- are looked up. A hashed key is accepted and ignored, because hashes are visible to admins and must not be enough to block a key. Each client address may send 10 reports a minute by default, and the route is on by default.
-
In the Admin UI, open Virtual Keys and note the alias of the key you are about to report, so you can check the result afterwards.
-
Send the report. Any HTTP client works, this is the curl form:
curl -X POST http://localhost:4000/public/compromised_key \-H "Content-Type: application/json" \-d '{"key": "sk-9f3a2c7e41b8d6059012ijkl3456mnop", "source": "secret scanner, finding 8841"}'Expected result:
{"status": "accepted"} -
Back in Virtual Keys, the key now shows as blocked. With audit logging on, open Logs, then Audit Logs, and filter on the key to see the
blockedrow with thesourceyou sent and thereporter_ipthe report came from. Without audit logging, look for thecompromised key report: blocked virtual keyline in the gateway log. -
Create a replacement key for the owner with + Create New Key, and remove the leaked key from wherever it was found.
To change these settings in the Admin UI, open Status Page and find the Leaked key reports card under Public surfaces. The Serve POST /public/compromised_key switch turns the route on or off at once, and Reports per client address per minute with Save leaked key reports settings changes the limit. A proxy admin can change them, other admins see them read only, and values set in the config file are locked. The same change with the API:
curl -X POST http://localhost:4000/config/field/update \
-H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \
-d '{"config_type": "general_settings", "field_name": "compromised_key_reports_per_minute", "field_value": 30}'
Settings, under general_settings:
| Key | Default | What it does |
|---|---|---|
compromised_key_reports | true | Lets a scanner or a person get a leaked key blocked without signing in. When on, a matching active key is blocked at once. When off, the route answers 404 and a leaked key keeps working until someone blocks it by hand. Turn it off only on a gateway that is never reachable by outside scanners. |
compromised_key_reports_per_minute | 10 | Reports one client address may send per minute. Over that the route answers 429 with Retry-After: 60. |
general_settings:
compromised_key_reports: true
compromised_key_reports_per_minute: 10
Point your secret scanner at this route. Any tool that can send an HTTP request on a finding can use it, and the source field is a single line of free text that lands in the log line and the audit row so you can trace the report back to the finding. It is shown as plain text in the alert and the owner email, never as a link or markup.
Maximum key lifetime
max_lifetime_days under general_settings.key_hygiene gives every key an expiry date no later than its creation date plus that many days. The hourly key hygiene job applies it to existing keys as well as new ones, so turning it on with 365 means a key created two years ago expires at its next run. A key that already expires sooner is left alone, keys of the teams in exempt_team_ids are left alone, and blocked or already expired keys are not touched. Every change writes an audit row with spanway_changed_by set to key_hygiene_policy and the reason key_hygiene maximum key lifetime.
An organization can set its own limit in its metadata under max_key_lifetime_days, a whole number of days from 1 to 3650. The stricter of the gateway value and the organization value wins for that organization's keys, and an organization value applies even when the gateway sets no cap. Setting it needs a license granting key_hygiene, the same as the job that applies it, so a gateway without that license refuses the value instead of storing one that would never take effect.
-
Set the gateway cap. In the Admin UI, open Virtual Keys, click Key hygiene, type
365into Maximum key lifetime (days) on the Policy card and click Save policy. Or set it in the config and restart or reload the gateway:general_settings:key_hygiene:max_lifetime_days: 365exempt_team_ids: ["break-glass"] -
For a stricter organization, open Organizations in the Admin UI, pick the organization, open the Settings tab, click Edit Settings, type
90into Maximum key lifetime (days) and click Save Changes. The value is stored in the organization metadata asmax_key_lifetime_days. Or use the API:curl -X PATCH http://localhost:4000/organization/update \-H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \-d '{"organization_id": "org-finance", "metadata": {"max_key_lifetime_days": 90}}' -
Within an hour, open Key hygiene from the Virtual Keys page. The No expiry tile drops to the exempt keys, and each capped key shows its new expiry date in the table.
Expected result: a key created on 1 March with the gateway cap of 365 days expires on 1 March the following year, and a key of org-finance created on 1 March expires on 30 May. A new key created with a longer duration keeps that longer date for at most an hour, until the next run shortens it.
Expiry emails
The same job emails the owner of every key that is about to expire, 30 days and 1 day before by default. Change the thresholds with expiry_notice_days, or set it to [] to turn the emails off. In the Admin UI the thresholds are the Expiry emails (days before) field on the Policy card of the Key hygiene page, where an empty field turns the emails off. Each threshold is sent once for each expiry date. When a key is given a later expiry, for example with /key/update, the thresholds it was already warned at count again, so the owner is warned before the new date too. A key whose lifetime is shorter than the first threshold gets one email that covers every threshold already passed. Keys of exempt teams are left out without a log line. A key without an owner is skipped with a WARNING line, and a key whose owner has no email address is logged as a failed notice with User email not found. Each notice that went out writes an audit row with spanway_changed_by set to key_hygiene_policy, the reason key_hygiene expiry notice and the thresholds sent so far. When the Virtual Key Expiring email event is switched off, nothing is sent and nothing is recorded, so switching it back on sends every notice that is still due. Days left are rounded up, so a key with 4 days and 20 hours left is announced as expiring in 5 days, and the final notice says 1 day.
The email is the Virtual Key Expiring event in email notifications. It names the key by alias, or by its last four hash characters when it has none, and gives the expiry time and the days left. It never contains the key itself. Email needs an email provider configured, see email.
general_settings:
key_hygiene:
expiry_notice_days: [30, 7, 1]
-
Configure an email provider and
PROXY_BASE_URL, then open Logging & Alerts in the Admin UI and check that Virtual Key Expiring is ticked under Email Notifications. -
Give a key a short lifetime to see the effect, for example with the API:
curl -X POST http://localhost:4000/key/update \-H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \-d '{"key": "sk-9f3a2c7e41b8d6059012ijkl3456mnop", "duration": "5d"}' -
Within an hour the owner receives "Spanway: Your API key expires in 5 days", the key's metadata records
expiry_notices_sentwith the thresholds already sent, and the audit log shows anupdatedrow for the key bykey_hygiene_policy.
Errors
| Status | Route | Meaning | Fix |
|---|---|---|---|
404 Leaked key reporting is not enabled | POST /public/compromised_key | compromised_key_reports is false. | Set it to true, or send the report to the gateway that has it on. |
| 422 | POST /public/compromised_key | The body is not a report, for example key is missing, shorter than 20 characters, or source is longer than 200 characters or contains line breaks or other control characters. | Send {"key": "sk-..."} with an optional single-line source. |
429 Too many leaked key reports from this address | POST /public/compromised_key | The client sent more than compromised_key_reports_per_minute reports in the last minute. | Wait for the Retry-After seconds, or raise the limit for a scanner that batches findings. |
503 No database connected | POST /public/compromised_key | The gateway runs without a database, so there are no stored keys to block. | Set DATABASE_URL. |
400 max_key_lifetime_days must be a whole number of days from 1 to 3650. Received: <value> | POST /organization/new, PATCH /organization/update | The organization value is not a whole number from 1 to 3650, for example "90 days" or 0. | Send a number such as 90, or null to remove the limit. |
403 Your Spanway license does not include the 'key_hygiene' feature. ... | POST /organization/new, PATCH /organization/update | The organization sets max_key_lifetime_days and the license does not grant key_hygiene. | Add the feature to the license, or leave the field out. |
400 Invalid key_hygiene: max_lifetime_days: Input should be greater than or equal to 1 | POST /config/field/update, Save policy on the Key hygiene page | The saved policy has a value out of range. The message names the field and the rule. | Fix the value and save again. It applies at once, no restart needed. |
500 general_settings.key_hygiene is invalid | /key/hygiene routes | The policy in config.yaml has max_lifetime_days outside 1 to 3650, or expiry_notice_days repeats a value or has one outside 1 to 365. | Fix the value in the config file and restart, or save a valid policy from the Policy card, which applies at once. The job logs the same message and skips its run until it is fixed. |
Logs
| Level | Line | Meaning |
|---|---|---|
| WARNING | compromised key report: blocked virtual key alias=... team=... source=... reporter_ip=... | A report matched and the key is now blocked. Normal. Check the audit row when audit logging is on, and tell the owner if email is not configured. Without audit logging this line is the record of the block. |
| WARNING | compromised key report: owner of key alias ... not emailed (no email callback configured) or (the key has no owner) | The key was blocked but no owner email went out, because no email callback is configured or the key belongs to no user. Tell the owner yourself or set up email alerts. |
| ERROR | compromised key report: alert failed or compromised key report: owner email for key alias ... failed | The key was blocked but the alert or the email could not be sent. Check the alerting or email provider settings. |
| INFO | key hygiene: capped the lifetime of N keys | The hourly job shortened N expiry dates to the lifetime cap. Normal. |
| INFO | key hygiene: sent expiry notices for N keys | The hourly job sent N expiry emails. A threshold is recorded against a key only once an email provider accepted the notice. Normal. |
| INFO | key hygiene: expiry notices skipped, no email callback is configured | Expiry notices are turned on (the default) but no email provider is set up, so the hourly job sends nothing and records nothing. Once you configure email, the next run sends every notice that is still due, including those for keys already inside a threshold. |
| WARNING | key hygiene: expiry notice for key alias ... not emailed (the key has no owner) | The key has no user_id, so there is nobody to email and the threshold is not recorded. Give service keys an owner, or put them in an exempt team. |
| ERROR | key hygiene: expiry notice for key alias ... failed: ... | The notice could not be sent. User email not found means the key's owner has no email address, anything else means the email provider rejected it. The threshold is not recorded, so the next hourly run tries again. Give the user an email address, or check the email provider settings. |
| ERROR | key hygiene job skipped: general_settings.key_hygiene is invalid | The policy does not validate. Fix the value named in the message. |
Related
- Key hygiene for the report, unused key auto-expiry and the
exempt_team_idslist. - Email notifications for providers and the per-event switches.
- Alerting for the
compromised_key_reportedalert type. - Audit logs for the rows these controls write.
- Virtual keys for creating, blocking and rotating keys.