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

Code execution of .svelte file open #2670

Open
delamonpansie opened this issue Jan 20, 2025 · 2 comments
Open

Code execution of .svelte file open #2670

delamonpansie opened this issue Jan 20, 2025 · 2 comments
Labels
bug Something isn't working

Comments

@delamonpansie
Copy link

Describe the bug

A code is executed from vite.config.ts upon opening .svelte file. For example, opening file from non-trusted svelte source may perform unwanted changed to a filesystem: e.g. rm -rf $HOME. This is a surprising behavior in a default configuration.

Reproduction

#!/bin/sh
npx sv create --template minimal --types ts --no-add-ons --no-install baz
cd baz

cat > src/routes/+page.svelte <<EOF
<script lang="ts">
</script>

<form method="POST">
</form>

<style>
</style>
EOF

cat >> vite.config.ts <<EOF
import { writeFile } from "fs";
writeFile("/tmp/gotcha.txt", "", () => {});
process.exit(1);
EOF

npm i svelte-language-server
export PATH=$(pwd)/node_modules/.bin:$PATH # so emacs can find svelteserver

emacs src/routes/+page.svelte

ls -l /tmp/gotcha.txt # <<< file has been created

Expected behaviour

No code executed from a project on file open in a default configuration. If code execution is necessary for some features, it should be explicit opt-in.

System Info

npm:

[email protected] /tmp/baz
├── @sveltejs/[email protected]
├── @sveltejs/[email protected]
├── @sveltejs/[email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected]

emacs: 30.0.93
lsp-mode: 20250116.14

Which package is the issue about?

svelte-language-server

Additional Information, eg. Screenshots

No response

@delamonpansie delamonpansie added the bug Something isn't working label Jan 20, 2025
@jasonlyu123
Copy link
Member

Reading svelte.config.js, thus vite.config.ts, for preprocess setup is a core part of the svelte compiler diagnostic. So I am not sure if it's possible to make it opt-in without affecting most people. This also requires the language server client to provide a way to configure this opt-in since reading a config file has the same problem.

In VSCode, there is a "workspace trust" feature that svelte-language-server supports. The first time you open a directory it'll ask if trust the workspace. If you don't trust it, the supported extension can disable specific features.

The most possible solution I can think of is that you can hook into this "trust" feature on the language server side. You can pass in isTrusted in the initialization option for the svelte language server to "untrust" the workspace.

const isTrusted: boolean = evt.initializationOptions?.isTrusted ?? true;

But I don't how and if it's possible to add this in emacs, Maybe https://emacs-lsp.github.io/lsp-mode/page/settings/mode/#lsp-before-initialize-hook?

@delamonpansie
Copy link
Author

Thanks, that is helpful! Though, it is surprising, that the default is "trusted". From security conscious point of view the default should be closed, not open.

Apparently, there is no builtin way of configuring out "trusted workspace" in emacs. Emacs does configure bunch of svelte-server setting: https://github.com/emacs-lsp/lsp-mode/blob/master/clients/lsp-svelte.el#L228 . Do you know what it the way for enabling isTrusted? I assume it is also a part of the initialization message?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants