Skip to content

Commit bd8c5fc

Browse files
feat(terminal): restrict default allowed commands to 'ls' and 'echo'
Change the default value of `terminal.allowCommands` from an empty array to `['ls', 'echo']`. This change prevents users from running all available commands by default in tutorials, enhancing security and focus. Lesson authors can still allow all commands by specifying `terminal.allowCommands: []` in the metadata. BREAKING CHANGE: The default value of `terminal.allowCommands` is now restricted to `['ls', 'echo']`. To allow all commands, explicitly set `terminal.allowCommands: []` in the metadata. Closes #302
1 parent e335d17 commit bd8c5fc

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

docs/tutorialkit.dev/src/content/docs/reference/configuration.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ You can set terminal open by default by specifying the `open` value.
312312

313313
An interactive terminal will disable the output redirect syntax by default. For instance, you cannot create a file `world.txt` with the contents `hello` using the command `echo hello > world.txt`. The reason is that this could disrupt the lesson if a user overwrites certain files. To allow output redirection, you can change the behavior with the `allowRedirects` setting. You can define this setting either per panel or for all panels at once.
314314

315-
Additionally, you may not want users to run arbitrary commands. For example, if you are creating a lesson about `vitest`, you could specify that the only command the user can run is `vitest` by providing a list of `allowCommands`. Any other command executed by the user will be blocked. You can define the `allowCommands` setting either per panel or for all panels at once.
315+
Additionally, you may not want users to run arbitrary commands. For example, if you are creating a lesson about vitest, you could specify that the only command the user can run is vitest by providing a list of allowCommands. By default, the allowed commands are ls and echo. Providing a list of allowCommands will override these defaults, and specifying an empty list will allow all commands. Any other command executed by the user will be blocked. You can define the allowCommands setting either per panel or for all panels at once.
316316

317317
By default, in every new lesson terminals start a new session. If you want to keep the terminal session between lessons, you can specify the `id` property for a given terminal panel and keep the same `id` across lessons.
318318
<PropertyTable inherited type="Terminal" />

packages/runtime/src/webcontainer/terminal-config.ts

+17-2
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ export class TerminalPanel implements ITerminal {
200200
}
201201
}
202202

203+
// set the default commands for the terminal
204+
const DEFAULT_COMMANDS = ['ls', 'echo'];
205+
203206
/**
204207
* Normalize the provided configuration to a configuration which is easier to parse.
205208
*
@@ -232,9 +235,21 @@ function normalizeTerminalConfig(config?: TerminalSchema): NormalizedTerminalCon
232235

233236
const panels: TerminalPanel[] = [];
234237

238+
const resolveAllowCommands = (allowCommands?: string[]): string[] | undefined => {
239+
if (allowCommands === undefined) {
240+
return DEFAULT_COMMANDS;
241+
}
242+
243+
if (Array.isArray(allowCommands) && allowCommands.length === 0) {
244+
return undefined;
245+
}
246+
247+
return allowCommands;
248+
};
249+
235250
const options = {
236251
allowRedirects: config.allowRedirects,
237-
allowCommands: config.allowCommands,
252+
allowCommands: resolveAllowCommands(config.allowCommands),
238253
};
239254

240255
if (config.panels) {
@@ -258,7 +273,7 @@ function normalizeTerminalConfig(config?: TerminalSchema): NormalizedTerminalCon
258273
id: panel.id,
259274
title: panel.title,
260275
allowRedirects: panel.allowRedirects ?? config.allowRedirects,
261-
allowCommands: panel.allowCommands ?? config.allowCommands,
276+
allowCommands: panel.allowCommands ?? DEFAULT_COMMANDS,
262277
});
263278
}
264279

0 commit comments

Comments
 (0)