WEBVTT

00:00:00.800 --> 00:00:02.040
Every developer and every agent

00:00:02.060 --> 00:00:03.410
knows how to call a function.

00:00:03.590 --> 00:00:04.900
So we designed rpc in Elements

00:00:04.920 --> 00:00:06.460
to be a type checked function call

00:00:06.480 --> 00:00:07.930
from the browser to the server.

00:00:08.230 --> 00:00:09.230
But we have to do this carefully,

00:00:09.250 --> 00:00:10.830
to avoid major security issues

00:00:10.850 --> 00:00:12.240
like the one in 2025.

00:00:12.690 --> 00:00:14.030
At the end of this video, I'll explain

00:00:14.050 --> 00:00:15.880
that incident and how we prevent it.

00:00:16.330 --> 00:00:17.300
Say we want to save a message

00:00:17.320 --> 00:00:18.770
from this form in the database.

00:00:19.000 --> 00:00:19.800
When you submit it,

00:00:19.820 --> 00:00:21.840
the onsubmit function runs in the browser,

00:00:22.140 --> 00:00:23.600
but the database is on the server.

00:00:23.700 --> 00:00:25.650
So we write a function called saveForm,

00:00:25.670 --> 00:00:27.150
with one insert query,

00:00:27.500 --> 00:00:29.610
and we put the @rpc build tag on it.

00:00:29.650 --> 00:00:31.710
Rpc stands for remote procedure call,

00:00:31.730 --> 00:00:33.390
which means the browser sends a request

00:00:33.410 --> 00:00:34.430
over the network to call

00:00:34.450 --> 00:00:36.270
the saveForm function on the server.

00:00:36.570 --> 00:00:38.230
Then we call saveForm from onsubmit

00:00:38.250 --> 00:00:39.440
like any other function,

00:00:39.590 --> 00:00:40.850
clear the input when it returns,

00:00:40.870 --> 00:00:42.040
and log the result.

00:00:42.390 --> 00:00:43.880
When I send a message, the input clears,

00:00:43.900 --> 00:00:45.740
and I see the result in the console,

00:00:46.090 --> 00:00:47.040
and in a terminal,

00:00:47.210 --> 00:00:48.610
elements db shows the new row

00:00:48.630 --> 00:00:49.890
in the messages table.

00:00:50.340 --> 00:00:52.090
The call to saveForm goes over the network,

00:00:52.110 --> 00:00:53.330
but the build type checks it

00:00:53.350 --> 00:00:54.700
like any other function call.

00:00:54.860 --> 00:00:56.780
If I pass a string instead of the form,

00:00:56.930 --> 00:00:57.570
I get a type error

00:00:57.590 --> 00:00:58.980
as soon as I save the file.

00:00:59.280 --> 00:01:00.350
That's important because we catch

00:01:00.370 --> 00:01:01.430
the mistake at build time,

00:01:01.450 --> 00:01:02.330
and not at runtime,

00:01:02.350 --> 00:01:03.940
where the end users would see the error.

00:01:04.240 --> 00:01:05.300
When I pass the form again,

00:01:05.320 --> 00:01:06.240
the build is green.

00:01:06.540 --> 00:01:08.530
If I take the @rpc build tag off and save,

00:01:08.550 --> 00:01:09.670
I get a security error.

00:01:09.970 --> 00:01:11.070
A function that queries the database

00:01:11.090 --> 00:01:11.950
is server code,

00:01:11.970 --> 00:01:13.350
and browser code can't call it

00:01:13.370 --> 00:01:15.210
without going through an rpc function.

00:01:15.510 --> 00:01:17.160
With the @rpc build tag back on,

00:01:17.180 --> 00:01:18.060
the build is green.

00:01:18.410 --> 00:01:19.440
The build writes two versions

00:01:19.460 --> 00:01:20.960
of every rpc function,

00:01:20.980 --> 00:01:23.220
one for the browser and one for the server.

00:01:23.570 --> 00:01:24.490
In the browser's version,

00:01:24.510 --> 00:01:26.170
the body is replaced with one call,

00:01:26.190 --> 00:01:27.510
which names the file and the function

00:01:27.530 --> 00:01:29.180
and passes the arguments along.

00:01:29.530 --> 00:01:30.750
The query stays on the server,

00:01:30.770 --> 00:01:31.690
so you don't leak information

00:01:31.710 --> 00:01:33.710
about your database schema to the browser.

00:01:34.060 --> 00:01:35.600
A call over the network is async,

00:01:35.620 --> 00:01:36.910
but we never wrote await.

00:01:37.160 --> 00:01:38.240
That's because async await

00:01:38.260 --> 00:01:40.000
is handled automatically during the build,

00:01:40.020 --> 00:01:41.540
so the next line waits for saveForm

00:01:41.560 --> 00:01:43.360
to return automatically.

00:01:43.380 --> 00:01:45.460
Written by hand, the same code needs async

00:01:45.480 --> 00:01:46.460
on all three functions,

00:01:46.480 --> 00:01:47.990
and await at every call,

00:01:48.090 --> 00:01:49.650
and a Promise in the return type.

00:01:50.000 --> 00:01:51.710
If you forget the await, the next line runs

00:01:51.730 --> 00:01:53.520
before saveForm is returned.

00:01:53.670 --> 00:01:55.600
So Elements does the right thing by default.

00:01:55.900 --> 00:01:57.650
If you want to manage async await yourself,

00:01:57.680 --> 00:01:59.410
you can make your rpc function async,

00:01:59.560 --> 00:02:00.800
and then Elements won't automatically

00:02:00.820 --> 00:02:02.270
transform the call graph.

00:02:02.620 --> 00:02:04.260
Every page keeps one WebSocket connection

00:02:04.280 --> 00:02:05.170
to the server.

00:02:05.550 --> 00:02:07.490
WebSockets are cheap, so it stays open

00:02:07.510 --> 00:02:09.070
for as long as the page does.

00:02:09.420 --> 00:02:10.930
An rpc call sends one request

00:02:10.950 --> 00:02:12.550
over the WebSocket, with an id,

00:02:12.570 --> 00:02:14.510
the method, and the arguments as JSON,

00:02:14.810 --> 00:02:15.950
the server runs saveForm,

00:02:15.970 --> 00:02:18.170
and sends back one reply with the same id

00:02:18.190 --> 00:02:19.130
and the result.

00:02:19.480 --> 00:02:20.400
Back in the editor,

00:02:20.420 --> 00:02:22.490
we add a createdAt date to the form.

00:02:22.750 --> 00:02:24.670
Plain JSON has no Date type,

00:02:24.690 --> 00:02:25.950
so it would arrive in the browser

00:02:25.970 --> 00:02:27.000
as a string.

00:02:27.350 --> 00:02:28.990
Elements has its own extended JSON

00:02:29.010 --> 00:02:30.330
that handles advanced types,

00:02:30.350 --> 00:02:32.800
so createdAt arrives as a proper Date.

00:02:33.100 --> 00:02:33.970
Elements JSON works

00:02:33.990 --> 00:02:35.400
for your own classes too,

00:02:35.530 --> 00:02:37.080
if you add the @json decorator.

00:02:37.250 --> 00:02:38.230
So we make a Form class

00:02:38.250 --> 00:02:39.270
with a toString method,

00:02:39.290 --> 00:02:40.790
and add the decorator.

00:02:41.140 --> 00:02:43.130
Now saveForm returns an instance of Form

00:02:43.150 --> 00:02:44.960
in the browser, with its methods.

00:02:45.410 --> 00:02:47.360
How do rpc functions compare to routes?

00:02:47.660 --> 00:02:48.810
A route is a function that's called

00:02:48.830 --> 00:02:50.540
when the browser requests the page,

00:02:50.640 --> 00:02:52.270
so that's where we pass in the initial data,

00:02:52.290 --> 00:02:54.750
and the server returns complete HTML.

00:02:55.050 --> 00:02:56.220
That's important because the page

00:02:56.240 --> 00:02:57.040
is rendered immediately,

00:02:57.060 --> 00:02:59.450
and the user never waits on a blank screen.

00:02:59.740 --> 00:03:01.460
It's also important because search engines

00:03:01.480 --> 00:03:02.980
can read it without loading JavaScript,

00:03:03.000 --> 00:03:05.100
and that can only help your SEO score.

00:03:05.400 --> 00:03:06.700
In contrast, an rpc function

00:03:06.720 --> 00:03:08.320
is typically called after the page

00:03:08.340 --> 00:03:10.470
is loaded, like when we save our form.

00:03:10.720 --> 00:03:12.040
But we can also call it from the route,

00:03:12.060 --> 00:03:13.260
like any other function.

00:03:13.710 --> 00:03:15.110
The session is available automatically

00:03:15.130 --> 00:03:17.120
inside routes and rpc functions.

00:03:17.370 --> 00:03:18.390
So before we run the query,

00:03:18.410 --> 00:03:20.560
we call session.isLoggedInOrThrow,

00:03:20.590 --> 00:03:22.050
and if the user isn't logged in,

00:03:22.070 --> 00:03:22.920
we get an error.

00:03:23.270 --> 00:03:24.560
When saveForm throws an error,

00:03:24.580 --> 00:03:25.930
the error goes over the wire,

00:03:26.060 --> 00:03:27.380
and we can catch it right here

00:03:27.400 --> 00:03:29.040
in our event handler in the browser.

00:03:29.390 --> 00:03:31.240
I'm not logged in, so when I send a message,

00:03:31.260 --> 00:03:32.570
we catch an AuthError,

00:03:32.620 --> 00:03:34.530
and here it is, printed in the console.

00:03:35.180 --> 00:03:37.380
In December 2025, one security bug

00:03:37.400 --> 00:03:38.970
almost broke the entire internet.

00:03:39.250 --> 00:03:40.570
The bug was in the way React handled

00:03:40.590 --> 00:03:41.890
remote procedure calls.

00:03:42.190 --> 00:03:44.050
An attacker could run his own arbitrary code

00:03:44.070 --> 00:03:44.960
on the server.

00:03:45.260 --> 00:03:47.030
The React team rated it 10 out of 10

00:03:47.050 --> 00:03:48.030
for severity.

00:03:48.330 --> 00:03:49.260
Within hours,

00:03:49.350 --> 00:03:50.930
Amazon saw Chinese state hackers

00:03:50.950 --> 00:03:52.150
trying to exploit it.

00:03:52.450 --> 00:03:54.710
28% of Cloudflare's traffic went down

00:03:54.730 --> 00:03:56.150
because of a change they pushed out

00:03:56.170 --> 00:03:57.350
to block the attack.

00:03:57.650 --> 00:03:59.570
The bug was in how React read the arguments

00:03:59.590 --> 00:04:00.580
of a request.

00:04:00.930 --> 00:04:02.790
By changing them, an attacker could control

00:04:02.810 --> 00:04:04.410
which code the server ran.

00:04:04.860 --> 00:04:06.050
Calling server code from the browser

00:04:06.070 --> 00:04:08.370
is serious, and it takes careful engineering

00:04:08.390 --> 00:04:09.620
to keep it secure.

00:04:09.920 --> 00:04:11.080
We designed rpc in Elements

00:04:11.100 --> 00:04:12.920
so a request can only name a file

00:04:12.940 --> 00:04:14.430
and a function in that file,

00:04:14.600 --> 00:04:16.220
and the arguments and return value are sent

00:04:16.240 --> 00:04:18.810
over the wire as data, not executable code.

00:04:19.110 --> 00:04:20.720
If a bad guy sends a path to a file

00:04:20.740 --> 00:04:22.110
outside your app's folder,

00:04:22.180 --> 00:04:23.440
the server refuses it.

00:04:23.790 --> 00:04:24.950
For an rpc request,

00:04:25.080 --> 00:04:26.340
the server only loads a file

00:04:26.360 --> 00:04:28.230
that starts with a "use rpc" directive,

00:04:28.440 --> 00:04:29.740
and the build puts that directive

00:04:29.760 --> 00:04:32.210
in every file that has rpc functions.

00:04:32.510 --> 00:04:33.680
Once the file is loaded,

00:04:33.730 --> 00:04:35.430
the server will only invoke the function

00:04:35.450 --> 00:04:37.880
if it has an rpc property set to true.

00:04:38.180 --> 00:04:39.560
The build tool writes that property,

00:04:39.580 --> 00:04:41.990
only on functions with the @rpc build tag,

00:04:42.200 --> 00:04:43.940
and an attacker has no way to set it.

00:04:44.290 --> 00:04:45.760
So we have another exported function

00:04:45.780 --> 00:04:46.920
that we don't want accessible

00:04:46.940 --> 00:04:48.020
from the browser.

00:04:48.370 --> 00:04:49.880
If a bad guy tries to call it,

00:04:49.930 --> 00:04:51.090
the server refuses and throws

00:04:51.110 --> 00:04:52.240
a not found error.

00:04:52.540 --> 00:04:53.660
The only functions on your server

00:04:53.680 --> 00:04:54.680
that a browser can call

00:04:54.700 --> 00:04:56.700
are the ones you tagged with rpc.

00:04:56.950 --> 00:04:57.930
The server reads the arguments

00:04:57.950 --> 00:05:00.510
as Elements JSON and gives them to saveForm,

00:05:00.530 --> 00:05:02.120
so they're never run as code.

00:05:02.570 --> 00:05:03.610
In Elements, calling a function

00:05:03.630 --> 00:05:04.730
on the server from the browser

00:05:04.750 --> 00:05:06.260
is a regular function call.

00:05:06.350 --> 00:05:07.240
It's type checked,

00:05:07.430 --> 00:05:08.960
async await is handled automatically,

00:05:09.150 --> 00:05:10.100
and it's secure.
