fix: allow exec on shared hosts (#1362)

This commit is contained in:
ZacharyZcR
2026-08-29 00:30:49 +08:00
committed by GitHub
parent 0f949c5e24
commit 9706426421
2 changed files with 27 additions and 23 deletions
+7 -23
View File
@@ -12,8 +12,8 @@ import {
resolveSnippetCommand,
} from "./snippets-execution.js";
import { logAudit, getRequestMeta } from "../../utils/audit-logger.js";
import { resolveHostById } from "../../hosts/host-resolver.js";
import {
createCurrentHostResolutionRepository,
createCurrentRbacAccessRepository,
createCurrentRoleRepository,
createCurrentSnippetRepository,
@@ -587,32 +587,16 @@ router.post(
}
const { Client } = await import("ssh2");
const repository = createCurrentHostResolutionRepository();
const host = await repository.findHostById(parseInt(hostId), userId);
const host = await resolveHostById(parseInt(hostId), userId);
if (!host || host.userId !== userId) {
if (!host) {
return res.status(404).json({ error: "Host not found" });
}
let password = host.password;
let privateKey = host.key;
let passphrase = host.keyPassword;
let authType = host.authType;
if (host.credentialId) {
const cred = await repository.findCredentialByIdForUser(
host.credentialId as number,
userId,
);
if (cred) {
authType = (cred.authType || authType) as string;
password = (cred.password || undefined) as string | undefined;
privateKey = (cred.privateKey || cred.key || undefined) as
string | undefined;
passphrase = (cred.keyPassword || undefined) as string | undefined;
}
}
const password = host.password;
const privateKey = host.key;
const passphrase = host.keyPassword;
const authType = host.authType;
const conn = new Client();
let output = "";
@@ -0,0 +1,20 @@
import fs from "fs";
import path from "path";
import { describe, expect, it } from "vitest";
describe("snippet execution host access", () => {
it("uses the shared-host-aware resolver", () => {
const source = fs.readFileSync(
path.resolve(__dirname, "../../../database/routes/snippets.ts"),
"utf8",
);
const executeRoute = source.slice(
source.indexOf('router.post(\n "/execute"'),
);
expect(executeRoute).toContain(
"const host = await resolveHostById(parseInt(hostId), userId)",
);
expect(executeRoute).not.toContain("host.userId !== userId");
});
});