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
6 changes: 3 additions & 3 deletions src/cline-sdk/cline-mcp-runtime-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type {
import { z } from "zod";

import type { RuntimeClineMcpServer } from "../core/api-contract";
import { buildKanbanRuntimeUrl } from "../core/runtime-endpoint";
import { getKanbanOauthRedirectOrigin } from "../core/runtime-endpoint";
import { lockedFileSystem } from "../fs/locked-file-system";
import { createClineMcpSettingsService, resolveMcpSettingsPath } from "./cline-mcp-settings-service";
import {
Expand Down Expand Up @@ -440,7 +440,7 @@ class RuntimeMcpServerClient implements SdkMcpServerClient {
serverName: this.server.name,
redirectUrl:
parseOauthSettings(this.oauthSettingsPath).servers[this.server.name]?.redirectUrl ??
buildKanbanRuntimeUrl(OAUTH_CALLBACK_PATH),
getKanbanOauthRedirectOrigin() + OAUTH_CALLBACK_PATH,
});
}

Expand Down Expand Up @@ -546,7 +546,7 @@ class RuntimeMcpServerClient implements SdkMcpServerClient {
}

function buildMcpOauthCallbackUrl(requestId: string): string {
const callbackUrl = new URL(buildKanbanRuntimeUrl(OAUTH_CALLBACK_PATH));
const callbackUrl = new URL(getKanbanOauthRedirectOrigin() + OAUTH_CALLBACK_PATH);
callbackUrl.searchParams.set(OAUTH_CALLBACK_REQUEST_ID_PARAM, requestId);
return callbackUrl.toString();
}
Expand Down
6 changes: 6 additions & 0 deletions src/core/runtime-endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ export function getKanbanRuntimeOrigin(): string {
return `${scheme}://${getKanbanRuntimeHost()}:${getKanbanRuntimePort()}`;
}

export function getKanbanOauthRedirectOrigin(): string {
const scheme = isKanbanRuntimeHttps() ? "https" : "http";
const host = runtimeHost === "0.0.0.0" ? "127.0.0.1" : runtimeHost;
return `${scheme}://${host}:${getKanbanRuntimePort()}`;
}

export function getKanbanRuntimeWsOrigin(): string {
const scheme = isKanbanRuntimeHttps() ? "wss" : "ws";
return `${scheme}://${getKanbanRuntimeHost()}:${getKanbanRuntimePort()}`;
Expand Down
41 changes: 27 additions & 14 deletions src/server/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ export function getAllowedHostHeaders(): ReadonlySet<string> {

if (isKanbanRemoteHost()) {
addHostPort(boundHost);
if (boundHost === "0.0.0.0") {
addHostPort("localhost");
addHostPort("127.0.0.1");
}
return allowed;
}

Expand Down Expand Up @@ -109,18 +113,22 @@ function rejectSocket(socket: Duplex): { end: boolean } {
}

export function handleHttpRequest(req: IncomingMessage, res: ServerResponse): { end: boolean } {
const hostDecision = evaluateHost({
hostHeader: req.headers.host,
allowedHosts: getAllowedHostHeaders(),
});
if (hostDecision.kind === "reject") {
return rejectRequest(res, "Host not allowed.");
const boundHost = getKanbanRuntimeHost().toLowerCase();
if (boundHost !== "0.0.0.0") {
const hostDecision = evaluateHost({
hostHeader: req.headers.host,
allowedHosts: getAllowedHostHeaders(),
});
if (hostDecision.kind === "reject") {
return rejectRequest(res, "Host not allowed.");
}
}

const allowedOrigin = boundHost === "0.0.0.0" && req.headers.origin ? req.headers.origin : getKanbanRuntimeOrigin();
const corsDecision = evaluateCors({
method: req.method,
originHeader: req.headers.origin,
allowedOrigin: getKanbanRuntimeOrigin(),
allowedOrigin,
});

switch (corsDecision.kind) {
Expand All @@ -146,18 +154,23 @@ export function handleHttpRequest(req: IncomingMessage, res: ServerResponse): {
}

export function handleSocketUpgrade(request: IncomingMessage, socket: Duplex): { end: boolean } {
const hostDecision = evaluateHost({
hostHeader: request.headers.host,
allowedHosts: getAllowedHostHeaders(),
});
if (hostDecision.kind === "reject") {
return rejectSocket(socket);
const boundHost = getKanbanRuntimeHost().toLowerCase();
if (boundHost !== "0.0.0.0") {
const hostDecision = evaluateHost({
hostHeader: request.headers.host,
allowedHosts: getAllowedHostHeaders(),
});
if (hostDecision.kind === "reject") {
return rejectSocket(socket);
}
}

const allowedOrigin =
boundHost === "0.0.0.0" && request.headers.origin ? request.headers.origin : getKanbanRuntimeOrigin();
const corsDecision = evaluateCors({
method: request.method,
originHeader: request.headers.origin,
allowedOrigin: getKanbanRuntimeOrigin(),
allowedOrigin,
});
if (corsDecision.kind === "reject") {
return rejectSocket(socket);
Expand Down