Leaked server secrets are a security compile error in Elements
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.
Add a comment