-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Description
Describe the bug
Thanks for the awesome open source work.
I'm quite new to Svelte and SvelteKit and recently decided to use in a project to learn it more. I think, I've discovered some bug or issue in SvelteKit with my very limited experience using it.
I'm loading some data in +layout.server.ts from an external url which requires sending api key as query param. I've stored the api key in .env file. I'm loading the data in +layout.server.ts specifically because I don't want to expose api key to the client. But even this file is also exposing it to client as part of url dependency for the fetch.
I tried calling depends inside load but it adds additional dependencies along with fetch url. I couldn't find any way to prevent SvelteKit using url as dependency.
As an alternate solution to this, I could've created an internal api route which calls the external api with api key but I think +layout.server.ts shouldn't expose the url (and secrets certainly not) to client as it guarantees to run the code only server side. I don't see any benefit of using server only load of +layout.server.ts over universal load in +layout.ts, if it exposes the url (and secrets) to client.
Reproduction
- Create an
.envfile and add a secret such asAPI_KEY=top-secret-key. - Create a load function in
+layout.server.tswhich usesfetchto load data from external api. Import and passAPI_KEYenvironment variable to fetch url as query param. - Use the server fetched data in
+layout.svelte. - Open the browser dev tools and view source. At the bottom in
scripttag, you'll find that your secretAPI_KEYis exposed to the client.
const data = [
{
type: 'data',
data: {
todos: [
{ id: 1, title: 'one' },
{ id: 2, name: 'two' }
]
},
uses: {
dependencies: [
'https://jsonplaceholder.typicode.com/todos?api_key=top-secret-key' // <---- Secret exposed
]
}
},
null
]Here's a demo project I've created to reproduce this issue:
https://stackblitz.com/edit/sveltejs-kit-template-default-eeb3rv?file=src/routes/+layout.server.js
Also note that in my testing with @sveltejs/kit@next version the dependencies array is empty which is what I was expecting.
Logs
No response
System Info
System:
OS: Linux 5.0 undefined
CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
Memory: 0 Bytes / 0 Bytes
Shell: 1.0 - /bin/jsh
Binaries:
Node: 16.14.2 - /usr/local/bin/node
Yarn: 1.22.19 - /usr/local/bin/yarn
npm: 7.17.0 - /usr/local/bin/npm
npmPackages:
@sveltejs/adapter-auto: ^2.0.0 => 2.0.1
@sveltejs/kit: ^1.5.0 => 1.15.9
svelte: ^3.54.0 => 3.58.0
vite: ^4.3.0 => 4.3.3Severity
blocking all usage of SvelteKit
Additional Information
No response