CCyberwiki

Privileged key in client-side code

Privileged key in client-side code
ClassSecrets
Also known asService role key in the browser, secret key in the frontend
CWECWE-798: Use of Hard-coded Credentials
Typical severityCritical
AffectsWeb pages and mobile apps that call Supabase, Stripe, AWS and other services directly, and build tools that copy prefixed variables into the bundle

A privileged key in client-side code is a server credential built into JavaScript sent to the browser or into a mobile app: the Supabase secret key or service role key, a Stripe secret key, a cloud access key. Anyone who loads the page or installs the app can read the key and send requests with every permission it carries. It is the outbound form of hard-coded credentials, the one where a front end holds the credential it uses to sign in to a back-end service.1

Public and privileged keys

Supabase and Stripe each issue two kinds of key. A public key identifies the project and is meant to ship in client code; the service limits what it can do. A privileged key is meant for servers the developer controls, and the Supabase and Stripe secret keys have full access to the project or account. Supabase states the rule as "Pick the key by asking where the code runs": code that is shipped (a browser, a mobile app, a CLI) uses the publishable key, and code on a server, an Edge Function or a cron job uses the secret key.2

Table 1. Public and privileged keys at two providers
ServicePublic key (may ship)Privileged key (server only)
SupabasePublishable key sb_publishable_...; legacy anon keySecret key sb_secret_...; legacy service_role key
StripePublishable key pk_live_...Secret key sk_live_...; restricted key rk_live_...

The legacy Supabase keys are long tokens that begin with eyJ; the newer keys are short strings. Supabase is deprecating the legacy pair by the end of 2026, and creating new keys does not revoke the old ones: they stay valid until they are turned off under Settings > API Keys.2 Stripe treats only the publishable key as safe outside the back end, so its restricted keys stay on the server as well.3

Mechanism

The usual path is an environment variable with a public prefix. Build tools read .env files and copy the values of prefixed variables into the bundle at build time. Next.js replaces each reference to process.env.NEXT_PUBLIC_... "with a hard-coded value" in the JavaScript sent to the browser.4 Vite does the same for VITE_ variables, and Expo for EXPO_PUBLIC_ variables, which end up "visible in plain-text in your compiled application".56

A project in which a client component reads its data with the admin key:

// .env
NEXT_PUBLIC_SUPABASE_SERVICE_ROLE_KEY=eyJhbGciOi...

// app/orders/list.tsx, a Client Component
"use client";
const supabase = createClient(url, process.env.NEXT_PUBLIC_SUPABASE_SERVICE_ROLE_KEY!);

After next build, the key is a string literal in a file that every visitor downloads. Supabase blocks its newer secret keys when a request carries a browser User-Agent and answers with HTTP 401, but the key still works from any other tool, such as a script or curl.2 A mobile app hides its keys no better: pulling a credential out of a client binary "is usually very simple".1

Impact

Each Supabase key maps to a database role. The secret key runs as service_role, which has full access to the project's data and the BYPASSRLS attribute, so no row level security policy applies to it.2 A visitor holding that key can read, change and delete every row in every table the role is granted, whatever policies the project has. A Stripe secret key has "unrestricted permissions on all Stripe APIs", including charges and account data.3

Notable cases

On 1 September 2022, Symantec's Threat Hunter Team reported 1,859 publicly available Android and iOS apps that contained hard-coded AWS credentials, 98% of them on iOS. In 77% of the apps the tokens were valid for private AWS services. One business intranet provider had placed its AWS keys in a mobile SDK to reach a translation service; the keys opened all of its AWS services and the files of more than 15,000 customer companies. A digital identity SDK used by five banking apps exposed over 300,000 fingerprint records, and a library in 16 gambling apps carried AWS root credentials with read and write access.7

Detection

List the variables in every .env file and in the hosting dashboard whose names start with NEXT_PUBLIC_, VITE_ or EXPO_PUBLIC_, or with a custom prefix set by Vite's envPrefix option.5 Any of them holding a secret key or a service role key is exposed. Then search the source and the build output for the key formats in Table 1:

grep -rnE "sb_secret_|sk_live_|rk_live_|service_role" src/ <build-folder>/

A long token beginning with eyJ is a legacy Supabase key; compare it with the two legacy keys shown in the dashboard to learn which one it is.

Prevention

Requests that need the privileged key move to code that runs only on a server: a server route, an Edge Function or a worker. The browser calls that route, and the route checks who the user is before it uses the key. Supabase's own example keeps the two clients apart by name:2

// Browser: publishable key, limited by row level security
createClient(process.env.NEXT_PUBLIC_SUPABASE_URL!, process.env.NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY!);

// Server only: secret key, no public prefix
createClient(process.env.SUPABASE_URL!, process.env.SUPABASE_SECRET_KEY!);

In Next.js, a variable without the prefix is replaced with an empty string in client code.8 Adding import 'server-only' at the top of the file that creates the admin client turns any import of it from a Client Component into a build-time error.8 In a Vite project the secret belongs on a backend server or in a serverless function.5

A key that the server holds can also carry fewer rights, "limited at the back end to only performing actions valid for the front end".1 At Stripe that means a restricted key with chosen permissions in place of the unrestricted secret key.3 See Principle of least privilege.

A key that has shipped in a build is public. Rotate it once the cause is fixed;2 the steps per provider are in Responding to a leaked API key.

Prompt for a coding agent

Find every place in this project where a privileged key can reach the browser or a mobile build: environment variables starting with NEXT_PUBLIC_, VITE_, EXPO_PUBLIC_ or a custom envPrefix that hold a Supabase secret or service_role key, a Stripe secret or restricted key, or a cloud access key; and any such key written into a file that runs on the client. Move each request that needs the key into a server route or Edge Function that checks the signed-in user first, read the key there from a variable without a public prefix, and add import 'server-only' to the file that creates the admin client. Use the publishable key in client code. Do not print key values. List each file you changed and each key that must now be rotated.

In SecFlash

On the Exposure page, SecFlash reports SF-EXP-301 for a secret key in public JavaScript, SF-EXP-302 for a token whose decoded payload shows a Supabase service_role key or a login token, SF-EXP-304 for a secret key written into the page, and SF-EXP-612 for a secret key in a mobile build. On the Security page, it reports SF-SEC-724 when the source code puts the service_role key in browser code or in a NEXT_PUBLIC_, VITE_ or EXPO_PUBLIC_ variable, and SF-SEC-728 for a secret in a value built into a mobile app.

See also

References

  1. a b c "CWE-798: Use of Hard-coded Credentials". Common Weakness Enumeration. MITRE.
  2. a b c d e f "Understanding API keys". Supabase Docs.
  3. a b c "API keys". Stripe Documentation. Stripe.
  4. back "How to use environment variables in Next.js". Next.js Docs. Vercel.
  5. a b c "Env Variables and Modes". Vite documentation.
  6. back "Environment variables in Expo". Expo Documentation. Expo.
  7. back Symantec Threat Hunter Team (1 September 2022). "Mobile App Supply Chain Vulnerabilities Could Endanger Sensitive Business Information". Symantec Enterprise Blogs. Broadcom.
  8. a b Vercel. "Server and Client Components". Next.js Docs.