--- title: "Leaked server secrets are a security compile error in Elements" author: "@chris" author_url: https://elements.dev/u/@chris published: 2026-12-04T02:13:01.680Z url: https://elements.dev/blog/leaked-server-secrets-are-a-security-compile-error-in-ya7h4crb kind: post format: video video: https://elements.dev/blog/leaked-server-secrets-are-a-security-compile-error-in-ya7h4crb/video.mp4 poster: https://elements.dev/blog/leaked-server-secrets-are-a-security-compile-error-in-ya7h4crb/poster.jpg duration: "3:45" transcript_vtt: https://elements.dev/blog/leaked-server-secrets-are-a-security-compile-error-in-ya7h4crb/transcript.vtt --- # Leaked server secrets are a security compile error in Elements by [@chris](https://elements.dev/u/@chris) ยท 2026-12-04 ## Description 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. ## Notes ## 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: ```js app.js async 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](https://x.com/leojrr/status/1901560276488511759). Another reported [about $2,200 in unauthorized API charges](https://discuss.ai.google.dev/t/urgent-2200-in-unauthorized-gemini-api-charges-from-leaked-client-side-key-requesting-refund/141634) 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 `` tag. We write a function that queries the database, and we call the function from the button's `onclick` event handler: ```ehtml app/pages/home/template.ehtml import { db } from "@elements/app"; function getProducts() { return db.query`select * from products`; } ``` 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: ```text Terminal State: 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: ```json { "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 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: ```ehtml app/pages/home/template.ehtml import { db, session } from "@elements/app"; /** @rpc */ function getProducts() { session.isLoggedInOrThrow(); return db.query`select * from products`; } ``` 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: ```js /** @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: ```js 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: ```text config/env/development.env DB_PASSWORD=dev_password ``` We read that environment variable in the page: ```ehtml app/pages/home/template.ehtml {process.env.DB_PASSWORD} ``` We save, and the build fails: ```text Terminal 1. 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 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: ```jsoc config.jsoc app: { 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: ```ehtml app/pages/home/template.ehtml import config from "#config"; {config.app.stripe.publicKey} ``` We save, and the build fails again: ```text Terminal 1. 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: ```jsoc config.jsoc app: { 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: ```jsoc config.jsoc app: { /** @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: ```ehtml app/pages/home/template.ehtml function getProducts() { session.isLoggedInOrThrow(); return db.query`select * from products`; } function onClick() { return getProducts(); } ``` 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`: ```text Terminal Call 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 `@rpc` build tag - a read of an environment variable - a read of a config property that does not have the `@browser` build 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. ## Transcript In Elements, leaking a server secret to the browser is a compile error. Here's why that matters. Your database and your server are locked down, but anybody can open your app in a browser, including a bad guy. If there's a secret in the code you send to the browser, the bad guy has it. And that usually happens by accident, whether you wrote the code or your agent did. This guy's API key leaked, and strangers used it to make requests, which were billed to him. This developer got $2,200 in charges from a leaked key. And this one exposed API keys to 50,000 users. So how does a bad guy get your secrets? He opens your app in his browser, opens the developer tools, clicks Sources, and there's your code. There's two kinds of leaks here. The first is information about how your system is designed, like the names of your database tables, and that gives an attacker clues about how to attack it. The second is outright theft of a password or a secret key, and that can let him connect straight to your database, or use a service you're billed for. Let's see what would happen if you try this in Elements. Here's an HTML page. Let's query the database. We write the code in a function right next to our HTML, so it's clear which code goes with which page. And we call getProducts from an event handler, so when the user clicks the button, the getProducts function runs. When I save the file, I get a security error, and it says I can't call server code from the browser. And it gives us steps to try. Your agent gets the same error as structured data, so it can act on it right away. So we add the @rpc build tag to getProducts, and the build is green. Now we can make sure the user is logged in before the query runs. Because getProducts has the @rpc build tag, the build writes two versions of it: one for the browser, and one for the server. In the browser's version, the body of the function is replaced with an RPC call, so the browser only specifies the server function by name. That call goes over the wire to the server, and the server's version runs the login check and the query. And back in the browser, this is all the bad guy sees. He can't read the query, and he can't delete the login check. The next common source of leaks is environment variables, because that's where secrets like the database password go. Leaking an environment variable to the browser is extremely common. But in Elements it's a compile error, because we don't allow environment variables to go to the browser at all. And again, the error gives us steps to try. This is config.jsoc, where all of our app and build config goes, in one place. And by default, every config property stays on the server. Sometimes we do need a config property in the browser. We'll add one under app, stripe, publicKey, and its value will come from an environment variable. So we use env, which is a special function in .jsoc files that reads an environment variable by name, with an optional default. We want a different value in development than in production. So each environment has its own env file, and a variable can also come from the system. Back in the page, we import config like any other module, and since config has types, the editor completes each name as we type. But when I save, it's a security error again, because browser code isn't allowed to read a config property, by default. To fix that, we add the @browser build tag in the config property. And if we put the build tag on a block, it applies to every config property in that block. In the browser, there's the public Stripe key, visible to everyone, as intended. So how does Elements know the browser is calling server code? The build starts at the event handler in your HTML, and works its way up the call graph until it reaches server code. And db.query is server code because its declaration has the @server build tag. It even works if we add another function in between, because the new function shows up in the call graph. When we mark getProducts with the @rpc build tag, that's the RPC boundary. The browser calls the function by name, and everything past it runs on the server. And if nothing in your HTML calls a function, the tree shaker leaves it out of the browser's JavaScript entirely, and there's no error. So in the browser, if you call server code, read an environment variable, or read a config property that isn't tagged for the browser, it's a security compile error. The build server makes your app secure at build time, every time you save. And it doesn't even depend on how your coding agent is feeling that day.