Leaked server secrets are a security compile error in Elements
In Elements, three ways of leaking a server secret to the browser are compile errors: calling server code from browser code without an @rpc function, reading an environment variable in browser code, and reading a config property that has no @browser build tag. The video shows each error, how the build follows the call graph from an event handler to server code, and the browser's version of an @rpc function, which has the function's name and nothing from its body.
How a Secret in Browser Code Reaches an Attacker
Your database and your app server are locked down. Your app's pages are not: anybody can open them in a browser, and the browser downloads the JavaScript of each page. Anybody can read that JavaScript. In Chrome, you open the developer tools, click Sources and click the file.
The video shows a shop page that is not an Elements app. Its JavaScript file, app.js, has two leaks:
app.jsasync function loadCustomers() { const rows = await db.from("customers").select("email, plan, card_last4"); return rows; } const API_KEY = "sk_live_EXAMPLE_9f2c7a41d8b3";
The first leak is information about how the system is designed. The query names a database table and three of its columns, and those names give an attacker clues about how to attack the database. The second leak is the API key, which an attacker can copy. With an API key, an attacker can use a service that you are billed for. With a database password, an attacker can connect to the database.
These leaks usually happen by accident, whether you wrote the code or your coding agent did. One developer wrote that people had used up the usage limits on his API keys (opens in a new tab). Another reported about $2,200 in unauthorized API charges (opens in a new tab) from a key that was in client-side code.
In Elements, each of these leaks is a compile error.
A Database Query Called From a Button Is a Compile Error
The demo app has one page, app/pages/home/template.ehtml, a reactive HTML file. In a reactive HTML file, the TypeScript of a page is written above its <html> tag. We write a function that queries the database, and we call the function from the button's onclick event handler:
app/pages/home/template.ehtmlimport { db } from "@elements/app"; function getProducts() { return db.query`select * from products`; } <html> <button onclick={() => getProducts()}>Get Products</button> </html>
An event handler runs in the browser, so a click on the button would call getProducts, and the query, in the browser. We save the file. The Elements build server runs a build, and elements start prints State: Error in the terminal:
TerminalState: Error ERRORS: You have 1 error: 1. app/pages/home/template.ehtml:4:10 Security error: cannot call server code from the browser without going through an rpc function. 3: function getProducts() { 4: return db.query`select * from products`; 5: } Call stack: 2. app/pages/home/template.ehtml:4:10 db.query 1. app/pages/home/template.ehtml:8:26 getProducts Try: - Put the server code inside a function with an @rpc build tag, then call that function. /** @rpc */ export function fn() {} [browser] [230001]
The error names the call to db.query, prints the call stack from the event handler to that call, and gives a step to try.
An agent reads the same error with elements build -json. The command prints the error as JSON, with the error code, the message, the call stack and the step to try as separate fields:
{
"code": 230001,
"level": "error",
"message": "Security error: cannot call server code from the browser without going through an rpc function.",
"path": "app/pages/home/template.ehtml",
"try": [
{
"text": "Put the server code inside a function with an @rpc build tag, then call that function.",
"code": "/** @rpc */\nexport function fn() {}"
}
],
"stack": [
{
"path": "app/pages/home/template.ehtml",
"loc": { "start": 71, "finish": 79, "line": 4, "column": 10 },
"name": "db.query"
},
{
"path": "app/pages/home/template.ehtml",
"loc": { "start": 140, "finish": 153, "line": 8, "column": 26 },
"name": "getProducts"
}
],
"targets": ["browser"]
}
The @rpc (opens in a new tab) Build Tag
We add the @rpc build tag to getProducts, in a comment on the line above the function. We also check that the user is logged in before the query runs:
app/pages/home/template.ehtmlimport { db, session } from "@elements/app"; /** @rpc */ function getProducts() { session.isLoggedInOrThrow(); return db.query`select * from products`; } <html> <button onclick={() => getProducts()}>Get Products</button> </html>
We save, and elements start prints State: Ok.
Because getProducts has the @rpc build tag, the build writes two versions of the function: one for the browser and one for the server. In the browser's version, the body of the function is replaced with a call that names the server function:
/** @rpc */
async function getProducts() {
return app.callAsync("app/pages/home/template.ehtml:getProducts", ...arguments);
}
That call goes from the browser to the app server. The server's version of getProducts has the login check and the query, and it runs on the server:
async function getProducts() {
app_2.session.isLoggedInOrThrow();
return await (app_1.withLoc({ start: 153, finish: 185 }, () => app_2.db.queryAsync `select * from products`));
}
The browser's JavaScript has the name of the function and nothing from its body. An attacker who opens the developer tools cannot read the query, and cannot delete the login check, because the login check runs on the server.
An Environment Variable Read in the Browser Is a Compile Error
Environment variables hold secrets such as the database password. The demo app's config/env/development.env has this line:
config/env/development.envDB_PASSWORD=dev_password
We read that environment variable in the page:
app/pages/home/template.ehtml<html> <button onclick={() => getProducts()}>Get Products</button> {process.env.DB_PASSWORD} </html>
We save, and the build fails:
Terminal1. app/pages/home/template.ehtml:11:4 Security error: cannot read the environment variable `DB_PASSWORD` from the browser. Try: - Add a key to config.jsoc with a @browser build tag, then read `config.name` in place of the process.env read. /** @browser */ name: env("DB_PASSWORD"), - If the value is a secret, leave out the @browser build tag and read `config.name` on the server, inside a function with an @rpc build tag. [browser] [230047]
Elements does not allow browser code to read an environment variable. The error gives two steps to try. In both, you add a property to config.jsoc and read that property in place of process.env.
Config Properties and the @browser (opens in a new tab) Build Tag
config.jsoc is the one file for an app's config and its build config. JSOC is our config language. By default, every config property in config.jsoc stays on the server.
Sometimes the browser does need a config property, such as a Stripe public key. We add a publicKey property to a stripe block inside the app block:
config.jsocapp: { stripe: { publicKey: env("STRIPE_PUBLIC_KEY", "stripe_dev_key"), }, },
env is a function you can call in a .jsoc file. It reads an environment variable by name, and its second argument is an optional default. The environment variable can come from the system or from an env file. Each environment has its own env file, config/env/development.env and config/env/production.env, so the property can have one value in development and another in production.
In the page, we import config like any other module and read the property. config has types, so the editor completes each name:
app/pages/home/template.ehtmlimport config from "#config"; <html> <button onclick={() => getProducts()}>Get Products</button> {config.app.stripe.publicKey} </html>
We save, and the build fails again:
Terminal1. app/pages/home/template.ehtml:12:22 Security error: cannot use server config values in the browser. Try: - Add a @browser build tag to `app.stripe.publicKey` in config.jsoc. /** @browser */ publicKey: ..., [browser] [230043]
Browser code is not allowed to read a config property unless the property has the @browser build tag. We add the build tag in a comment on the line above the property, and the build passes:
config.jsocapp: { stripe: { /** @browser */ publicKey: env("STRIPE_PUBLIC_KEY", "stripe_dev_key"), }, },
A @browser build tag on a block applies to every config property in that block:
config.jsocapp: { /** @browser */ stripe: { publicKey: env("STRIPE_PUBLIC_KEY", "stripe_dev_key"), }, },
The build writes the value of the property into the page's JavaScript for the browser. In the developer tools, the page's JavaScript now has the string "stripe_dev_key". That value is visible to everyone, as intended.
How the Build Knows the Browser Is Calling Server Code
The build starts at each event handler in your HTML and follows the call graph, from each function to the functions it calls, until it reaches server code. db.query is server code because its declaration in the @elements/app package has the @server build tag.
The check also works when another function is between the event handler and the query. We take the @rpc build tag off getProducts, add a function named onClick that calls getProducts, and make onClick the button's event handler:
app/pages/home/template.ehtmlfunction getProducts() { session.isLoggedInOrThrow(); return db.query`select * from products`; } function onClick() { return getProducts(); } <html> <button onclick={onClick}>Get Products</button> </html>
The build fails with the same security error, and the call stack now has three lines: the button's onclick, the call to getProducts inside onClick, and the call to db.query:
TerminalCall stack: 3. app/pages/home/template.ehtml:7:10 db.query 2. app/pages/home/template.ehtml:11:10 getProducts 1. app/pages/home/template.ehtml:15:20 onclick
When we put the @rpc build tag back on getProducts, that function is the rpc boundary. The browser calls getProducts by name, and getProducts and every function it calls run on the server.
If nothing in your HTML calls a function, the tree shaker leaves the function out of the browser's JavaScript, and the build reports no error. We delete onclick={onClick} from the button and save. The build passes, and the browser's JavaScript for the page has no onClick and no getProducts.
Three Compile Errors
In an Elements app, each of these in browser code is a compile error:
- a call to server code that does not go through a function with the
@rpcbuild tag - a read of an environment variable
- a read of a config property that does not have the
@browserbuild tag
The build server makes these checks at build time, each time you save a file. The checks do not depend on whether you or your coding agent remembered to keep a secret out of browser code.
Add a comment