Skip to content

chore: tighten up REPL security #300

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

Merged
merged 2 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import { mapbox_setup } from '../../../../config.js';
import AppControls from './AppControls.svelte';
import { compress_and_encode_text, decode_and_decompress_text } from './gzip.js';
import { page } from '$app/stores';

let { data } = $props();

Expand All @@ -18,6 +19,10 @@
let version = data.version;
let setting_hash: any = null;

// Hashed URLs are less safe (we can't delete malicious REPLs), therefore
// don't allow links to escape the sandbox restrictions
const can_escape = browser && !$page.url.hash;

onMount(() => {
if (version !== 'local') {
fetch(`https://unpkg.com/svelte@${version}/package.json`)
Expand Down Expand Up @@ -127,6 +132,7 @@
bind:this={repl}
{svelteUrl}
{relaxed}
{can_escape}
vim={data.vim}
injectedJS={mapbox_setup}
showModified
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
bind:this={repl}
{svelteUrl}
{relaxed}
can_escape
injectedJS={mapbox_setup}
showModified
showAst
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
{#if browser}
<Viewer
relaxed
can_escape
onLog={(l: Log[]) => logs = l}
{bundle}
theme={$theme.current}
Expand Down
2 changes: 2 additions & 0 deletions packages/repl/src/lib/Output/Output.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
export let runtimeError: Error | null = null;
export let embedded = false;
export let relaxed = false;
export let can_escape = false;
export let injectedJS: string;
export let injectedCSS: string;
export let showAst = false;
Expand Down Expand Up @@ -61,6 +62,7 @@
bind:error={runtimeError}
{status}
{relaxed}
{can_escape}
{injectedJS}
{injectedCSS}
theme={previewTheme}
Expand Down
27 changes: 4 additions & 23 deletions packages/repl/src/lib/Output/Viewer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
export let status: string | null;
/** sandbox allow-same-origin */
export let relaxed = false;
/** sandbox allow-popups-to-escape-sandbox (i.e. links within the REPL to other pages work) */
export let can_escape = false;
/** Any additional JS you may want to inject */
export let injectedJS = '';
/** Any additional CSS you may want to inject */
Expand Down Expand Up @@ -247,13 +249,12 @@
class:inited
bind:this={iframe}
sandbox={[
'allow-popups-to-escape-sandbox',
'allow-scripts',
'allow-popups',
'allow-forms',
'allow-pointer-lock',
'allow-top-navigation',
'allow-modals',
can_escape ? 'allow-popups-to-escape-sandbox' : '',
relaxed ? 'allow-same-origin' : ''
Comment on lines 256 to 258
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same question

Suggested change
'allow-modals',
can_escape ? 'allow-popups-to-escape-sandbox' : '',
relaxed ? 'allow-same-origin' : ''
'allow-modals'

Copy link
Member Author

@dummdidumm dummdidumm Oct 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without allow-popups-to-escape-sandbox, when you click on an outgoing link, you'll get a "browser has blocked the response" on the page that opens. It's an edge case for sure, but maybe there's playgrounds which showcase a certain aspect of their library and have a link with "you can read more about this here". Because it's potentially malicious it's only allowed on saved-to-db-REPLs which we could delete if the need arises.

Without allow-same-origin you can't access the parents localstorage,cookies, etc. With the headers being set due to the web container stuff, you also can't access the static images served from the parent domain (e.g. the Rick Roll wouldn't work anymore in the tutorial) - though that part could also be solved through #301. Currently this is only set for things that aren't user-content, i.e. the tutorial, because else this would be dangerous. I'm not sure why this was originally added (preceedes my time at Svelte)

].join(' ')}
class={error || pending || pending_imports ? 'greyed-out' : ''}
Expand All @@ -269,27 +270,7 @@
{#if !onLog}
<PaneWithPanel pos="90%" panel="Console">
<div slot="main">
<iframe
title="Result"
class:inited
bind:this={iframe}
sandbox={[
'allow-popups-to-escape-sandbox',
'allow-scripts',
'allow-popups',
'allow-forms',
'allow-pointer-lock',
'allow-top-navigation',
'allow-modals',
relaxed ? 'allow-same-origin' : ''
].join(' ')}
class={error || pending || pending_imports ? 'greyed-out' : ''}
srcdoc={BROWSER ? srcdoc : ''}
></iframe>

{#if $bundle?.error}
<ErrorOverlay error={$bundle.error} />
{/if}
{@render main()}
</div>

<div slot="panel-header">
Expand Down
2 changes: 2 additions & 0 deletions packages/repl/src/lib/Repl.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
export let embedded = false;
export let orientation: 'columns' | 'rows' = 'columns';
export let relaxed = false;
export let can_escape = false;
export let fixed = false;
export let fixedPos = 50;
export let injectedJS = '';
Expand Down Expand Up @@ -312,6 +313,7 @@
status={status_visible ? status : null}
{embedded}
{relaxed}
{can_escape}
{injectedJS}
{injectedCSS}
{showAst}
Expand Down
Loading