Skip to content
Code & CI · Secret guide

Leaked Sourcegraph Access Token: what to do in the first hour

High severityDetected onlyLast verified · 4 min read

A Sourcegraph access token authenticates calls to the Sourcegraph GraphQL API and the command-line tool as the user who created it. It can search the code of every repository that user can see, which for many companies means the whole private codebase, including old branches and files that are hard to find by hand. That makes a leaked token a quiet way to read source code at scale and hunt for other secrets inside it. Delete the token first, then review what the account could reach.

Check your GitHub account for leaked secrets — free

Provider
Sourcegraph
Severity
High
Impact
Source code and build access
Checked live by LeakWatch
No

What a Sourcegraph Access Token looks like

Since 2022 Sourcegraph tokens start with sgp_ followed by hexadecimal characters. Two shapes exist: a prefix, then an optional 16-character instance identifier or the word local, then a 40-character hexadecimal body; or the prefix directly followed by 40 hexadecimal characters:

sgp_XXXXXXXXXXXXXXXX_XXXXXXXX…   (with instance identifier, masked)
sgp_XXXXXXXX…                    (prefix + 40 hex characters, masked)

You will find it in the SRC_ACCESS_TOKEN environment variable, in the src command-line tool configuration, in an Authorization: token ... header sent to /.api/graphql, and in editor extensions or scripts that call the search API. Older tokens had no prefix and are not covered by the rule, because a bare 40-character hexadecimal string is indistinguishable from a Git commit hash.

How Sourcegraph Access Tokens get leaked

  • Shell history and scripts. SRC_ACCESS_TOKEN exported in a script or committed in a helper that runs src search.
  • CI pipelines. A job that queries code search to check for a pattern, with the token written in the workflow file.
  • Editor and tool configuration. An extension or a dotfile repository that carries the instance address and the token.
  • Documentation and issue threads. A curl example for the GraphQL API pasted with a working token.
  • Internal tools. A bot or dashboard that calls Sourcegraph, with its token left in the source.

What to do in the first hour

  1. Delete the token. Sign in to your Sourcegraph instance as the token owner and delete it from your user settings (see below). The Sourcegraph documentation suggests testing the old token afterwards: a query with it should return an invalid access token error.
  2. Create a new token only if needed, and store it in a secret manager rather than in a file.
  3. Update scripts and pipelines that used the old value.
  4. Check what the account can see. List the repositories the user has access to. Everything in that list was readable with the token.
  5. Look for activity. Site administrators can look at the audit and usage information that their edition provides. Look for searches or API calls from unknown addresses, and for unusual volume.
  6. Scan for secrets in your code. If an attacker searched your code, they may have found other credentials. Rotate any secret that appears in a repository the token could read.
  7. Clean up the code and the history. See I accidentally pushed an API key to GitHub.

Not sure what else leaked from the same repository? Run a free scan.

Not sure what else leaked? Run a free scan.

Revoke it at Sourcegraph

Sourcegraph is available as a cloud service and as a self-hosted instance, so the address depends on where you work. According to the Sourcegraph documentation, you open the avatar menu at the top right, choose Settings, pick Access tokens in the sidebar, find the token and use the Delete button next to it. Use the Sourcegraph access token settings of your own instance. A person has not confirmed the exact menu wording for this guide, and the interface can change, so look for the section where your access tokens are listed and follow the Sourcegraph documentation on revoking an access token. A site administrator can also remove tokens on behalf of other users.

How LeakWatch detects it

The rule is called Sourcegraph Access Token. It accepts the prefix sgp_ followed either by a 16-character hexadecimal identifier or the word local, an underscore and 40 hexadecimal characters, or directly by 40 hexadecimal characters. The match is case-insensitive and must end with a quote, whitespace, a semicolon or the end of the line. It refuses bare 40-character hexadecimal values, because those are usually Git commit hashes, and anything shorter such as sgp_ plus a few characters. The severity is high. This type is not checked live: LeakWatch does not send any request to Sourcegraph with the token it found.

LeakWatch detects this format but does not check it live: it can tell you the key is exposed, not whether it still works. Assume it does until you have revoked it.

FAQ

Can the token read my private repositories?

It can search and read code in any repository that the owner of the token can access in Sourcegraph. That is why the user’s repository list tells you the exposure.

Does deleting the token stop access immediately?

The documentation describes deleting as removing the token, after which API calls return an invalid token error. Test it with a harmless query to be sure.

Should I worry about old tokens without the prefix?

If you created tokens before the prefix existed, delete them too. They are not detected by the rule, but they work the same way.

Get alerted next time a secret leaks — create a free account

LeakWatch is not affiliated with Sourcegraph.