mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Block shell websocket connections from non-lens renderers
Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
90d4899ece
commit
4e264d3c40
@ -82,7 +82,7 @@ export class LensProxy extends Singleton {
|
|||||||
const reqHandler = isInternal ? shellApiRequest : kubeApiRequest;
|
const reqHandler = isInternal ? shellApiRequest : kubeApiRequest;
|
||||||
|
|
||||||
(async () => reqHandler({ req, socket, head }))()
|
(async () => reqHandler({ req, socket, head }))()
|
||||||
.catch(error => logger.error(logger.error(`[LENS-PROXY]: failed to handle proxy upgrade: ${error}`)));
|
.catch(error => logger.error("[LENS-PROXY]: failed to handle proxy upgrade", error));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -19,29 +19,38 @@
|
|||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import type http from "http";
|
|
||||||
import url from "url";
|
import url from "url";
|
||||||
import logger from "../logger";
|
import logger from "../logger";
|
||||||
import * as WebSocket from "ws";
|
import { Server as WebSocketServer } from "ws";
|
||||||
import { NodeShellSession, LocalShellSession } from "../shell-session";
|
import { NodeShellSession, LocalShellSession } from "../shell-session";
|
||||||
import type { ProxyApiRequestArgs } from "./types";
|
import type { ProxyApiRequestArgs } from "./types";
|
||||||
import { ClusterManager } from "../cluster-manager";
|
import { ClusterManager } from "../cluster-manager";
|
||||||
|
import { appName, appSemVer } from "../../common/vars";
|
||||||
|
import { LensProxy } from "../lens-proxy";
|
||||||
|
|
||||||
export function shellApiRequest({ req, socket, head }: ProxyApiRequestArgs) {
|
export function shellApiRequest({ req, socket, head }: ProxyApiRequestArgs): void {
|
||||||
const ws = new WebSocket.Server({ noServer: true });
|
const cluster = ClusterManager.getInstance().getClusterForRequest(req);
|
||||||
|
|
||||||
ws.on("connection", ((socket: WebSocket, req: http.IncomingMessage) => {
|
if (
|
||||||
const cluster = ClusterManager.getInstance().getClusterForRequest(req);
|
socket.remoteAddress !== "127.0.0.1"
|
||||||
|
|| !req.headers["user-agent"]?.includes(` ${appName}/${appSemVer} `)
|
||||||
|
|| !req.headers.origin?.endsWith(`.localhost:${LensProxy.getInstance().port}`)
|
||||||
|
|| !cluster
|
||||||
|
) {
|
||||||
|
socket.write("Invalid shell request");
|
||||||
|
|
||||||
|
return void socket.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
const ws = new WebSocketServer({ noServer: true });
|
||||||
|
|
||||||
|
ws.handleUpgrade(req, socket, head, (webSocket) => {
|
||||||
const nodeParam = url.parse(req.url, true).query["node"]?.toString();
|
const nodeParam = url.parse(req.url, true).query["node"]?.toString();
|
||||||
const shell = nodeParam
|
const shell = nodeParam
|
||||||
? new NodeShellSession(socket, cluster, nodeParam)
|
? new NodeShellSession(webSocket, cluster, nodeParam)
|
||||||
: new LocalShellSession(socket, cluster);
|
: new LocalShellSession(webSocket, cluster);
|
||||||
|
|
||||||
shell.open()
|
shell.open()
|
||||||
.catch(error => logger.error(`[SHELL-SESSION]: failed to open: ${error}`, { error }));
|
.catch(error => logger.error(`[SHELL-SESSION]: failed to open a ${nodeParam ? "node" : "local"} shell`, error));
|
||||||
}));
|
|
||||||
|
|
||||||
ws.handleUpgrade(req, socket, head, (con) => {
|
|
||||||
ws.emit("connection", con, req);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -19,7 +19,7 @@
|
|||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import type * as WebSocket from "ws";
|
import type WebSocket from "ws";
|
||||||
import { v4 as uuid } from "uuid";
|
import { v4 as uuid } from "uuid";
|
||||||
import * as k8s from "@kubernetes/client-node";
|
import * as k8s from "@kubernetes/client-node";
|
||||||
import type { KubeConfig } from "@kubernetes/client-node";
|
import type { KubeConfig } from "@kubernetes/client-node";
|
||||||
|
|||||||
@ -22,7 +22,7 @@
|
|||||||
import fse from "fs-extra";
|
import fse from "fs-extra";
|
||||||
import type { Cluster } from "../cluster";
|
import type { Cluster } from "../cluster";
|
||||||
import { Kubectl } from "../kubectl";
|
import { Kubectl } from "../kubectl";
|
||||||
import type * as WebSocket from "ws";
|
import type WebSocket from "ws";
|
||||||
import { shellEnv } from "../utils/shell-env";
|
import { shellEnv } from "../utils/shell-env";
|
||||||
import { app } from "electron";
|
import { app } from "electron";
|
||||||
import { clearKubeconfigEnvVars } from "../utils/clear-kube-env-vars";
|
import { clearKubeconfigEnvVars } from "../utils/clear-kube-env-vars";
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user