Secure server function calls built directly into Elements

0

Every developer and every agent knows how to call a function. So we designed rpc in Elements to be a type checked function call from the browser to the server. But we have to do this carefully, to avoid major security issues like the one in 2025. At the end of this video, I'll explain that incident and how we prevent it. Say we want to save a message from this form in the database. When you submit it, the onsubmit function runs in the browser, but the database is on the server. So we write a function called saveForm, with one insert query, and we put the @rpc build tag on it. Rpc stands for remote procedure call, which means the browser sends a request over the network to call the saveForm function on the server.

Then we call saveForm from onsubmit like any other function, clear the input when it returns, and log the result. When I send a message, the input clears, and I see the result in the console, and in a terminal, elements db shows the new row in the messages table. The call to saveForm goes over the network, but the build type checks it like any other function call. If I pass a string instead of the form, I get a type error as soon as I save the file. That's important because we catch the mistake at build time, and not at runtime, where the end users would see the error. When I pass the form again, the build is green.

If I take the @rpc build tag off and save, I get a security error. A function that queries the database is server code, and browser code can't call it without going through an rpc function. With the @rpc build tag back on, the build is green. The build writes two versions of every rpc function, one for the browser and one for the server. In the browser's version, the body is replaced with one call, which names the file and the function and passes the arguments along. The query stays on the server, so you don't leak information about your database schema to the browser. A call over the network is async, but we never wrote await. That's because async await is handled automatically during the build, so the next line waits for saveForm to return automatically.

Written by hand, the same code needs async on all three functions, and await at every call, and a Promise in the return type. If you forget the await, the next line runs before saveForm is returned. So Elements does the right thing by default. If you want to manage async await yourself, you can make your rpc function async, and then Elements won't automatically transform the call graph. Every page keeps one WebSocket connection to the server. WebSockets are cheap, so it stays open for as long as the page does. An rpc call sends one request over the WebSocket, with an id, the method, and the arguments as JSON, the server runs saveForm, and sends back one reply with the same id and the result.

Back in the editor, we add a createdAt date to the form. Plain JSON has no Date type, so it would arrive in the browser as a string. Elements has its own extended JSON that handles advanced types, so createdAt arrives as a proper Date. Elements JSON works for your own classes too, if you add the @json decorator. So we make a Form class with a toString method, and add the decorator. Now saveForm returns an instance of Form in the browser, with its methods. How do rpc functions compare to routes? A route is a function that's called when the browser requests the page, so that's where we pass in the initial data, and the server returns complete HTML.

That's important because the page is rendered immediately, and the user never waits on a blank screen. It's also important because search engines can read it without loading JavaScript, and that can only help your SEO score. In contrast, an rpc function is typically called after the page is loaded, like when we save our form. But we can also call it from the route, like any other function. The session is available automatically inside routes and rpc functions. So before we run the query, we call session.isLoggedInOrThrow, and if the user isn't logged in, we get an error. When saveForm throws an error, the error goes over the wire, and we can catch it right here in our event handler in the browser.

I'm not logged in, so when I send a message, we catch an AuthError, and here it is, printed in the console. In December 2025, one security bug almost broke the entire internet. The bug was in the way React handled remote procedure calls. An attacker could run his own arbitrary code on the server. The React team rated it 10 out of 10 for severity. Within hours, Amazon saw Chinese state hackers trying to exploit it. 28% of Cloudflare's traffic went down because of a change they pushed out to block the attack. The bug was in how React read the arguments of a request. By changing them, an attacker could control which code the server ran. Calling server code from the browser is serious, and it takes careful engineering to keep it secure.

We designed rpc in Elements so a request can only name a file and a function in that file, and the arguments and return value are sent over the wire as data, not executable code. If a bad guy sends a path to a file outside your app's folder, the server refuses it. For an rpc request, the server only loads a file that starts with a "use rpc" directive, and the build puts that directive in every file that has rpc functions. Once the file is loaded, the server will only invoke the function if it has an rpc property set to true. The build tool writes that property, only on functions with the @rpc build tag, and an attacker has no way to set it.

So we have another exported function that we don't want accessible from the browser. If a bad guy tries to call it, the server refuses and throws a not found error. The only functions on your server that a browser can call are the ones you tagged with rpc. The server reads the arguments as Elements JSON and gives them to saveForm, so they're never run as code. In Elements, calling a function on the server from the browser is a regular function call. It's type checked, async await is handled automatically, and it's secure.

Add a comment