Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prevent loading of illegal modules in the browser #7507

Merged
merged 32 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
5eab07a
prevent loading of illegal modules in the browser
Rich-Harris Nov 4, 2022
204d862
update test
Rich-Harris Nov 4, 2022
5261021
rename function
Rich-Harris Nov 4, 2022
b0c2282
fix
Rich-Harris Nov 4, 2022
b6d8a2a
remove dev test
Rich-Harris Nov 4, 2022
1e3256f
remove unused consts
Rich-Harris Nov 4, 2022
9132a66
simpler checking code
Rich-Harris Nov 4, 2022
b847be2
move tests (still failing)
Rich-Harris Nov 7, 2022
057a688
merge
Rich-Harris Nov 7, 2022
dde14a1
fix tests
Rich-Harris Nov 7, 2022
9737b04
remove only
Rich-Harris Nov 7, 2022
bca015d
fix
Rich-Harris Nov 7, 2022
923ea2d
Merge branch 'master' into client-side-illegal-module-guard
benmccann Nov 7, 2022
ca9c8eb
remove unnecessary default parameter
Rich-Harris Nov 8, 2022
caa38c4
move path normalization out of graph_analysis
Rich-Harris Nov 8, 2022
72989f4
reinstate tests
Rich-Harris Nov 8, 2022
6776dd9
ugh
Rich-Harris Nov 8, 2022
72bc526
doh, we need the dev-only test app
Rich-Harris Nov 8, 2022
96b7a37
no longer necessary
Rich-Harris Nov 8, 2022
86400f7
fix
Rich-Harris Nov 8, 2022
6d10cef
fixes
Rich-Harris Nov 8, 2022
d18047d
slow clap for windows
Rich-Harris Nov 8, 2022
0c6cd04
ah come on
Rich-Harris Nov 8, 2022
7d83036
get tests passing + windows path shenanigans
dummdidumm Nov 9, 2022
b398243
fix build tests
dummdidumm Nov 9, 2022
922ff31
more crossplatform posixify stuff
dummdidumm Nov 9, 2022
6223832
use consistent terminology between dev and build
Rich-Harris Nov 9, 2022
b7f65cc
ugh. print error messages so we can see why windows is being a dick
Rich-Harris Nov 9, 2022
c537d21
fml. can everyone just stop using fucking windows
Rich-Harris Nov 9, 2022
269c456
try this
Rich-Harris Nov 9, 2022
2cea023
ok turn everything back on
Rich-Harris Nov 9, 2022
d79cc59
Create soft-gorillas-hear.md
Rich-Harris Nov 10, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/soft-gorillas-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sveltejs/kit": patch
---

prevent loading of illegal modules in the browser, rather than during SSR
2 changes: 1 addition & 1 deletion packages/kit/scripts/special-types/$env+dynamic+private.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
This module provides access to runtime environment variables, as defined by the platform you're running on. For example if you're using [`adapter-node`](https://github.com/sveltejs/kit/tree/master/packages/adapter-node) (or running [`vite preview`](https://kit.svelte.dev/docs/cli)), this is equivalent to `process.env`. This module only includes variables that _do not_ begin with [`config.kit.env.publicPrefix`](https://kit.svelte.dev/docs/configuration#env).

This module cannot be imported into public-facing code.
This module cannot be imported into client-side code.

```ts
import { env } from '$env/dynamic/private';
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/scripts/special-types/$env+static+private.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Environment variables [loaded by Vite](https://vitejs.dev/guide/env-and-mode.html#env-files) from `.env` files and `process.env`. Like [`$env/dynamic/private`](https://kit.svelte.dev/docs/modules#$env-dynamic-private), this module cannot be imported into public-facing code. This module only includes variables that _do not_ begin with [`config.kit.env.publicPrefix`](https://kit.svelte.dev/docs/configuration#env).
Environment variables [loaded by Vite](https://vitejs.dev/guide/env-and-mode.html#env-files) from `.env` files and `process.env`. Like [`$env/dynamic/private`](https://kit.svelte.dev/docs/modules#$env-dynamic-private), this module cannot be imported into client-side code. This module only includes variables that _do not_ begin with [`config.kit.env.publicPrefix`](https://kit.svelte.dev/docs/configuration#env).

_Unlike_ [`$env/dynamic/private`](https://kit.svelte.dev/docs/modules#$env-dynamic-private), the values exported from this module are statically injected into your bundle at build time, enabling optimisations like dead code elimination.

Expand Down
2 changes: 1 addition & 1 deletion packages/kit/scripts/special-types/$lib.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ This is a simple alias to `src/lib`, or whatever directory is specified as [`con

#### `$lib/server`

A subdirectory of `$lib`. SvelteKit will prevent you from importing any modules in `$lib/server` into public-facing code. See [server-only modules](/docs/server-only-modules).
A subdirectory of `$lib`. SvelteKit will prevent you from importing any modules in `$lib/server` into client-side code. See [server-only modules](/docs/server-only-modules).
16 changes: 0 additions & 16 deletions packages/kit/src/exports/vite/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import { load_error_page, load_template } from '../../../core/config/index.js';
import { SVELTE_KIT_ASSETS } from '../../../constants.js';
import * as sync from '../../../core/sync/sync.js';
import { get_mime_lookup, runtime_base, runtime_prefix } from '../../../core/utils.js';
import { prevent_illegal_vite_imports } from '../graph_analysis/index.js';
import { compact } from '../../../utils/array.js';
import { normalizePath } from 'vite';

// Vite doesn't expose this so we just copy the list for now
// https://github.com/vitejs/vite/blob/3edd1af56e980aef56641a5a51cf2932bb580d41/packages/vite/src/node/plugins/css.ts#L96
Expand Down Expand Up @@ -43,8 +41,6 @@ export async function dev(vite, vite_config, svelte_config) {
/** @type {import('types').SSRManifest} */
let manifest;

const extensions = [...svelte_config.extensions, ...svelte_config.kit.moduleExtensions];

/** @param {string} id */
async function resolve(id) {
const url = id.startsWith('..') ? `/@fs${path.posix.resolve(id)}` : `/${id}`;
Expand Down Expand Up @@ -94,12 +90,6 @@ export async function dev(vite, vite_config, svelte_config) {
module_nodes.push(module_node);
result.file = url.endsWith('.svelte') ? url : url + '?import'; // TODO what is this for?

prevent_illegal_vite_imports(
module_node,
normalizePath(svelte_config.kit.files.lib),
extensions
);

return module.default;
};
}
Expand All @@ -110,12 +100,6 @@ export async function dev(vite, vite_config, svelte_config) {
module_nodes.push(module_node);

result.shared = module;

prevent_illegal_vite_imports(
module_node,
normalizePath(svelte_config.kit.files.lib),
extensions
);
}

if (node.server) {
Expand Down
Loading