Skip to content
Open
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
8 changes: 7 additions & 1 deletion apps/web/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,19 @@ export const resolveServerUrl = (options?: {
pathname?: string | undefined;
searchParams?: Record<string, string> | undefined;
}): string => {
const rawUrl = firstNonEmptyString(
let rawUrl = firstNonEmptyString(
options?.url,
window.desktopBridge?.getWsUrl(),
import.meta.env.VITE_WS_URL,
window.location.origin,
);

// When accessing from a remote host (e.g. mobile), replace localhost in the
// resolved URL with the actual page hostname so the connection reaches the server.
if (rawUrl.includes("localhost") && window.location.hostname !== "localhost") {
rawUrl = rawUrl.replace("localhost", window.location.hostname);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Naive string replace risks matching localhost in non-hostname positions

Low Severity

rawUrl.includes("localhost") and rawUrl.replace("localhost", ...) perform raw string matching rather than targeting the URL's hostname specifically. Since new URL(rawUrl) is constructed on the very next line, the hostname check and replacement could be done on the parsed URL object (e.g., parsedUrl.hostname === "localhost" and parsedUrl.hostname = window.location.hostname), avoiding any risk of matching "localhost" as a substring in paths, query params, or other hostname parts like notlocalhost.example.com.

Fix in Cursor Fix in Web


const parsedUrl = new URL(rawUrl);
if (options?.protocol) {
parsedUrl.protocol = options.protocol;
Expand Down
13 changes: 6 additions & 7 deletions apps/web/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { defineConfig } from "vite";
import pkg from "./package.json" with { type: "json" };

const port = Number(process.env.PORT ?? 5733);
const host = process.env.T3CODE_HOST ?? "localhost";
const sourcemapEnv = process.env.T3CODE_WEB_SOURCEMAP?.trim().toLowerCase();

const buildSourcemap =
Expand Down Expand Up @@ -42,14 +43,12 @@ export default defineConfig({
},
server: {
port,
host,
strictPort: true,
hmr: {
// Explicit config so Vite's HMR WebSocket connects reliably
// inside Electron's BrowserWindow. Vite 8 uses console.debug for
// connection logs — enable "Verbose" in DevTools to see them.
protocol: "ws",
host: "localhost",
},
hmr:
host === "localhost"
? { protocol: "ws", host: "localhost" }
: { protocol: "ws" },
},
build: {
outDir: "dist",
Expand Down
3 changes: 2 additions & 1 deletion turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"T3CODE_NO_BROWSER",
"T3CODE_HOME",
"T3CODE_AUTH_TOKEN",
"T3CODE_DESKTOP_WS_URL"
"T3CODE_DESKTOP_WS_URL",
"T3CODE_HOST"
],
"tasks": {
"build": {
Expand Down
Loading