Skip to content

Commit e3bf4dc

Browse files
authored
Test Terminal Follows Scrolling (#78)
1 parent aebbb20 commit e3bf4dc

4 files changed

Lines changed: 292 additions & 201 deletions

File tree

components/terminal/terminal-display.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,22 @@ export function TerminalDisplay({ ttydUrl, status, tabId }: TerminalDisplayProps
4545
// Listen to postMessage from ttyd iframe (autoscroll status updates)
4646
useEffect(() => {
4747
const handleMessage = (event: MessageEvent) => {
48-
// Security: Verify message format
48+
// Security: Verify message format and origin
4949
if (typeof event.data !== 'object' || !event.data) return;
5050

51+
// Verify message comes from ttyd iframe (check if origin matches ttydUrl)
52+
if (ttydUrl) {
53+
try {
54+
const ttydOrigin = new URL(ttydUrl).origin;
55+
if (event.origin !== ttydOrigin) {
56+
// Silently ignore messages from other origins
57+
return;
58+
}
59+
} catch {
60+
// Invalid URL, skip origin check
61+
}
62+
}
63+
5164
// Handle autoscroll status updates
5265
if (event.data.type === 'ttyd-scroll-status') {
5366
const newStatus = event.data.status;
@@ -62,7 +75,7 @@ export function TerminalDisplay({ ttydUrl, status, tabId }: TerminalDisplayProps
6275

6376
window.addEventListener('message', handleMessage);
6477
return () => window.removeEventListener('message', handleMessage);
65-
}, [tabId]);
78+
}, [tabId, ttydUrl]);
6679

6780
// Only show terminal iframe if status is RUNNING and URL is available
6881
if (status === 'RUNNING' && ttydUrl) {

sandbox/entrypoint.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ THEME='theme={
3737

3838
# Start ttyd with authentication wrapper, theme, and custom HTML for auto-scroll injection
3939
# -b: Set base path for serving static files (index.html and autoscroll script)
40-
# -I: Custom index.html path
40+
# -I: Custom index.html path (required for autoscroll to work)
4141
ttyd -T xterm-256color -W -a -t "$THEME" \
4242
-b /usr/local/share/ttyd \
43+
-I /usr/local/share/ttyd/index.html \
4344
/usr/local/bin/ttyd-auth.sh

0 commit comments

Comments
 (0)