WEBVTT

00:00:00.800 --> 00:00:02.020
In Elements, leaking a server secret

00:00:02.040 --> 00:00:03.820
to the browser is a compile error.

00:00:04.270 --> 00:00:05.330
Here's why that matters.

00:00:05.520 --> 00:00:06.420
Your database and your server

00:00:06.440 --> 00:00:07.240
are locked down,

00:00:07.260 --> 00:00:09.280
but anybody can open your app in a browser,

00:00:09.300 --> 00:00:10.420
including a bad guy.

00:00:10.720 --> 00:00:12.080
If there's a secret in the code you send

00:00:12.100 --> 00:00:13.910
to the browser, the bad guy has it.

00:00:14.040 --> 00:00:15.640
And that usually happens by accident,

00:00:15.660 --> 00:00:16.520
whether you wrote the code

00:00:16.540 --> 00:00:17.580
or your agent did.

00:00:17.880 --> 00:00:19.320
This guy's API key leaked,

00:00:19.340 --> 00:00:20.960
and strangers used it to make requests,

00:00:20.980 --> 00:00:22.240
which were billed to him.

00:00:22.540 --> 00:00:23.920
This developer got $2,200

00:00:23.940 --> 00:00:25.810
in charges from a leaked key.

00:00:26.110 --> 00:00:27.660
And this one exposed API keys

00:00:27.680 --> 00:00:29.410
to 50,000 users.

00:00:29.860 --> 00:00:31.720
So how does a bad guy get your secrets?

00:00:32.020 --> 00:00:33.310
He opens your app in his browser,

00:00:33.330 --> 00:00:35.270
opens the developer tools, clicks Sources,

00:00:35.290 --> 00:00:36.260
and there's your code.

00:00:36.560 --> 00:00:37.900
There's two kinds of leaks here.

00:00:38.090 --> 00:00:38.970
The first is information

00:00:38.990 --> 00:00:40.550
about how your system is designed,

00:00:40.570 --> 00:00:42.210
like the names of your database tables,

00:00:42.230 --> 00:00:43.690
and that gives an attacker clues

00:00:43.710 --> 00:00:45.040
about how to attack it.

00:00:45.230 --> 00:00:46.990
The second is outright theft of a password

00:00:47.010 --> 00:00:48.070
or a secret key,

00:00:48.090 --> 00:00:49.370
and that can let him connect straight

00:00:49.390 --> 00:00:50.300
to your database,

00:00:50.470 --> 00:00:52.060
or use a service you're billed for.

00:00:52.510 --> 00:00:53.180
Let's see what would happen

00:00:53.200 --> 00:00:54.490
if you try this in Elements.

00:00:54.520 --> 00:00:55.780
Here's an HTML page.

00:00:56.080 --> 00:00:57.190
Let's query the database.

00:00:57.420 --> 00:00:58.480
We write the code in a function

00:00:58.500 --> 00:01:00.320
right next to our HTML, so it's clear

00:01:00.340 --> 00:01:02.000
which code goes with which page.

00:01:02.300 --> 00:01:03.250
And we call getProducts

00:01:03.270 --> 00:01:04.170
from an event handler,

00:01:04.190 --> 00:01:05.740
so when the user clicks the button,

00:01:05.850 --> 00:01:07.270
the getProducts function runs.

00:01:07.570 --> 00:01:08.410
When I save the file,

00:01:08.430 --> 00:01:09.390
I get a security error,

00:01:09.410 --> 00:01:11.110
and it says I can't call server code

00:01:11.130 --> 00:01:12.120
from the browser.

00:01:12.170 --> 00:01:13.600
And it gives us steps to try.

00:01:13.900 --> 00:01:15.030
Your agent gets the same error

00:01:15.050 --> 00:01:15.770
as structured data,

00:01:15.790 --> 00:01:17.510
so it can act on it right away.

00:01:17.810 --> 00:01:19.630
So we add the @rpc build tag to getProducts,

00:01:19.650 --> 00:01:20.850
and the build is green.

00:01:21.150 --> 00:01:22.180
Now we can make sure the user

00:01:22.200 --> 00:01:24.050
is logged in before the query runs.

00:01:24.500 --> 00:01:26.590
Because getProducts has the @rpc build tag,

00:01:26.610 --> 00:01:28.150
the build writes two versions of it:

00:01:28.170 --> 00:01:30.130
one for the browser, and one for the server.

00:01:30.430 --> 00:01:31.290
In the browser's version,

00:01:31.310 --> 00:01:32.550
the body of the function is replaced

00:01:32.570 --> 00:01:33.840
with an RPC call,

00:01:33.950 --> 00:01:35.130
so the browser only specifies

00:01:35.150 --> 00:01:36.870
the server function by name.

00:01:37.170 --> 00:01:39.090
That call goes over the wire to the server,

00:01:39.120 --> 00:01:40.260
and the server's version runs

00:01:40.280 --> 00:01:41.830
the login check and the query.

00:01:42.480 --> 00:01:43.480
And back in the browser,

00:01:43.590 --> 00:01:45.060
this is all the bad guy sees.

00:01:45.090 --> 00:01:46.010
He can't read the query,

00:01:46.030 --> 00:01:47.870
and he can't delete the login check.

00:01:48.320 --> 00:01:49.340
The next common source of leaks

00:01:49.360 --> 00:01:50.520
is environment variables,

00:01:50.540 --> 00:01:51.560
because that's where secrets

00:01:51.580 --> 00:01:53.150
like the database password go.

00:01:53.450 --> 00:01:54.470
Leaking an environment variable

00:01:54.490 --> 00:01:56.270
to the browser is extremely common.

00:01:56.290 --> 00:01:57.690
But in Elements it's a compile error,

00:01:57.710 --> 00:01:59.230
because we don't allow environment variables

00:01:59.250 --> 00:02:00.590
to go to the browser at all.

00:02:00.890 --> 00:02:02.860
And again, the error gives us steps to try.

00:02:03.160 --> 00:02:04.480
This is config.jsoc,

00:02:04.500 --> 00:02:06.380
where all of our app and build config goes,

00:02:06.400 --> 00:02:07.270
in one place.

00:02:07.440 --> 00:02:08.120
And by default,

00:02:08.140 --> 00:02:10.100
every config property stays on the server.

00:02:10.400 --> 00:02:11.050
Sometimes we do need

00:02:11.070 --> 00:02:12.680
a config property in the browser.

00:02:12.770 --> 00:02:15.210
We'll add one under app, stripe, publicKey,

00:02:15.230 --> 00:02:16.090
and its value will come

00:02:16.110 --> 00:02:17.420
from an environment variable.

00:02:17.630 --> 00:02:18.530
So we use env,

00:02:18.550 --> 00:02:20.490
which is a special function in .jsoc files

00:02:20.510 --> 00:02:22.270
that reads an environment variable by name,

00:02:22.290 --> 00:02:23.670
with an optional default.

00:02:23.970 --> 00:02:25.380
We want a different value in development

00:02:25.400 --> 00:02:26.300
than in production.

00:02:26.320 --> 00:02:28.520
So each environment has its own env file,

00:02:28.540 --> 00:02:29.960
and a variable can also come

00:02:29.980 --> 00:02:30.850
from the system.

00:02:31.150 --> 00:02:32.780
Back in the page, we import config

00:02:32.800 --> 00:02:33.920
like any other module,

00:02:33.940 --> 00:02:35.490
and since config has types,

00:02:35.520 --> 00:02:37.530
the editor completes each name as we type.

00:02:37.830 --> 00:02:38.560
But when I save,

00:02:38.580 --> 00:02:39.990
it's a security error again,

00:02:40.040 --> 00:02:41.560
because browser code isn't allowed

00:02:41.580 --> 00:02:43.450
to read a config property, by default.

00:02:43.750 --> 00:02:45.620
To fix that, we add the @browser build tag

00:02:45.640 --> 00:02:46.910
in the config property.

00:02:47.210 --> 00:02:48.920
And if we put the build tag on a block,

00:02:48.940 --> 00:02:50.440
it applies to every config property

00:02:50.460 --> 00:02:51.360
in that block.

00:02:51.810 --> 00:02:52.510
In the browser,

00:02:52.620 --> 00:02:53.600
there's the public Stripe key,

00:02:53.620 --> 00:02:55.580
visible to everyone, as intended.

00:02:56.030 --> 00:02:56.930
So how does Elements know

00:02:56.950 --> 00:02:58.650
the browser is calling server code?

00:02:58.950 --> 00:03:00.310
The build starts at the event handler

00:03:00.330 --> 00:03:00.970
in your HTML,

00:03:00.990 --> 00:03:02.690
and works its way up the call graph

00:03:02.710 --> 00:03:04.290
until it reaches server code.

00:03:04.590 --> 00:03:06.250
And db.query is server code

00:03:06.270 --> 00:03:07.230
because its declaration

00:03:07.250 --> 00:03:08.850
has the @server build tag.

00:03:09.150 --> 00:03:10.030
It even works if we add

00:03:10.050 --> 00:03:11.330
another function in between,

00:03:11.350 --> 00:03:12.690
because the new function shows up

00:03:12.710 --> 00:03:13.590
in the call graph.

00:03:13.890 --> 00:03:14.670
When we mark getProducts

00:03:14.690 --> 00:03:16.010
with the @rpc build tag,

00:03:16.030 --> 00:03:17.320
that's the RPC boundary.

00:03:17.690 --> 00:03:19.390
The browser calls the function by name,

00:03:19.410 --> 00:03:21.440
and everything past it runs on the server.

00:03:21.740 --> 00:03:22.800
And if nothing in your HTML

00:03:22.820 --> 00:03:23.920
calls a function,

00:03:23.940 --> 00:03:25.000
the tree shaker leaves it out

00:03:25.020 --> 00:03:26.710
of the browser's JavaScript entirely,

00:03:26.780 --> 00:03:27.740
and there's no error.

00:03:28.190 --> 00:03:29.130
So in the browser,

00:03:29.220 --> 00:03:30.520
if you call server code,

00:03:30.540 --> 00:03:31.720
read an environment variable,

00:03:31.740 --> 00:03:32.800
or read a config property

00:03:32.820 --> 00:03:34.310
that isn't tagged for the browser,

00:03:34.340 --> 00:03:35.870
it's a security compile error.

00:03:36.200 --> 00:03:37.600
The build server makes your app secure

00:03:37.620 --> 00:03:39.830
at build time, every time you save.

00:03:40.140 --> 00:03:41.320
And it doesn't even depend

00:03:41.340 --> 00:03:42.440
on how your coding agent

00:03:42.460 --> 00:03:43.580
is feeling that day.
