--- title: "Secure server function calls built directly into Elements" author: "@chris" author_url: https://elements.dev/u/@chris url: https://elements.dev/blog/secure-server-function-calls-built-directly-into-elements-ao26yr3u kind: post format: video video: https://elements.dev/blog/secure-server-function-calls-built-directly-into-elements-ao26yr3u/video.mp4 poster: https://elements.dev/blog/secure-server-function-calls-built-directly-into-elements-ao26yr3u/poster.jpg duration: "5:12" transcript_vtt: https://elements.dev/blog/secure-server-function-calls-built-directly-into-elements-ao26yr3u/transcript.vtt --- # Secure server function calls built directly into Elements by [@chris](https://elements.dev/u/@chris) ## Description In Elements, browser code calls a function on the server with a regular function call. The build type checks the call and adds async and await. The server calls only a function that has the @rpc build tag, and it reads the arguments as data. ## Notes ## A Function Call From the Browser to the Server Every developer and every coding agent knows how to call a function. We designed rpc in Elements so that browser code calls a function on the server with a regular function call, and the build type checks that call. Calling server code from the browser has to be engineered carefully: a request must not be able to choose which code the server runs. In December 2025, a bug in the way React handled remote procedure calls let an attacker run code on the server. The last three sections of this post describe that bug and the checks the Elements server makes on every request. ## The @rpc Build Tag 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. The page is a form with one message input. When you submit the form, the `onsubmit` function runs in the browser: ```ehtml app/pages/home/template.ehtml import "./style.css"; interface Form { id?: string; message: string; } function onsubmit(form: Form) { }
``` We want to save the message in the database, and the database is on the server. We write a function named `saveForm` with one insert query, and we put the `@rpc` build tag on `saveForm`, in a comment on the line above the function. Rpc stands for remote procedure call: the browser sends a request over the network to call `saveForm` on the server. Then we call `saveForm` from `onsubmit` like any other function, clear the input when `saveForm` returns, and log the result: ```ehtml app/pages/home/template.ehtml import "./style.css"; import { db } from "@elements/app"; interface Form { id?: string; message: string; } /** @rpc */ function saveForm(form: Form): Form { return db.query` insert into messages (message) values (${form.message}) returning id, message `.firstOrThrow(); } function onsubmit(form: Form) { let result = saveForm(form); form.message = ""; console.log(result); } ``` We save the file. `elements start` is running in a terminal and builds the app each time a file is saved, and it prints `State: Ok`. In the browser we type "hello world" and click Send. The input clears, and the browser's console prints the object that `saveForm` returned, with the `id` and the `message` of the new row. In a second terminal, `elements db` opens a `psql` prompt on the app's database: ```bash Terminal elements db ``` At that prompt, `select * from messages;` prints the new row, with the `id` that the console printed. ## The Type Error When the Call Passes a String The call to `saveForm` goes over the network, and the build type checks it like any other function call. We pass a string where `saveForm` takes a `Form`: ```ts function onsubmit(form: Form) { let result = saveForm(form.message); form.message = ""; console.log(result); } ``` We save the file, and `elements start` prints the type error: ```text Terminal State: Error ERRORS: You have 1 error: 1. app/pages/home/template.ehtml:19:25 Argument of type 'string' is not assignable to parameter of type 'Form'. 18: function onsubmit(form: Form) { 19: let result = saveForm(form.message); 20: form.message = ""; [TS2345] ``` The build reports the mistake when the file is saved. Without the type check, the call would fail at runtime, in front of the people who use the app. We change the call back to `saveForm(form)`, and `elements start` prints `State: Ok`. ## The Security Error Without the @rpc Build Tag We delete the `/** @rpc */` comment above `saveForm` and save. The build fails with a security error: ```text Terminal State: Error ERRORS: You have 1 error: 1. app/pages/home/template.ehtml:10:10 Security error: cannot call server code from the browser without going through an rpc function. 9: function saveForm(form: Form): Form { 10: return db.query` 11: insert into messages (message) Call stack: 3. app/pages/home/template.ehtml:10:10 db.query 2. app/pages/home/template.ehtml:18:16 saveForm 1. app/pages/home/template.ehtml:26:23 onsubmit Try: - Put the server code inside a function with an @rpc build tag, then call that function. /** @rpc */ export function fn() {} [browser] [230001] ``` `db.query` is server code. The form's `onsubmit` event handler runs in the browser, and the call stack in the error goes from that event handler to `saveForm` to `db.query`. Browser code can call server code only through a function with the `@rpc` build tag. We put the `@rpc` build tag back on `saveForm`, and `elements start` prints `State: Ok`. ## The Two Versions the Build Writes 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 of `saveForm` is replaced with one call: ```js /** @rpc */ async function saveForm(form) { return app.callAsync("app/pages/home/template.ehtml:saveForm", ...arguments); } ``` The string names the file and the function, and `...arguments` passes the arguments of the call. The server's version of `saveForm` has the insert query, and it runs on the server. The browser's JavaScript does not have the query, so it does not have the name of the `messages` table or the names of its columns. ## Async Await Added by the Build A call over the network is async, and we never wrote `await`. The build adds `async` to `saveForm`, to `onsubmit` and to the form's event handler, and adds `await` at each call. The browser's version of `onsubmit` is: ```js async function onsubmit(form) { let result = await (saveForm(form)); form.message = ""; console.log(result); } ``` The form's event handler becomes: ```js "onsubmit": async () => await (onsubmit(this.attrs.form)) ``` Because of the `await`, the line `form.message = "";` runs after `saveForm` has returned. If you write `async` and `await` yourself and forget the `await` at the call to `saveForm`, that line runs before `saveForm` has returned. In Elements the build adds every `await`, so no `await` is forgotten. To write `async` and `await` yourself, declare your rpc function `async`. The build then leaves the functions that call it as you wrote them, and you write `async`, `await` and the `Promise` return type: ```ts /** @rpc */ async function saveForm(form: Form): Promise