BUGFIX: Block hacking-related actions on player-owned servers (#1585)

This commit is contained in:
catloversg 2024-08-16 16:05:22 +07:00 committed by GitHub
parent a9cb46606c
commit 4a1506cf7e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 8 deletions

@ -9,28 +9,28 @@ import { IReturnStatus } from "../types";
import { Player } from "@player";
import { Server } from "../Server/Server";
function baseCheck(server: Server, fnName: string): IReturnStatus {
function baseCheck(server: Server, actionName: string): IReturnStatus {
const hostname = server.hostname;
if (!("requiredHackingSkill" in server)) {
if (server.purchasedByPlayer) {
return {
res: false,
msg: `Cannot ${fnName} ${hostname} server because it is a Hacknet Node`,
msg: `Cannot ${actionName} ${hostname} server because it is your server`,
};
}
if (!server.hasAdminRights) {
return {
res: false,
msg: `Cannot ${fnName} ${hostname} server because you do not have root access`,
msg: `Cannot ${actionName} ${hostname} server because you do not have root access`,
};
}
return { res: true };
}
export function netscriptCanHack(server: Server): IReturnStatus {
const initialCheck = baseCheck(server, "hack");
export function netscriptCanHack(server: Server, customActionName?: string): IReturnStatus {
const initialCheck = baseCheck(server, customActionName ?? "hack");
if (!initialCheck.res) {
return initialCheck;
}

@ -559,14 +559,14 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
helpers.checkSingularityAccess(ctx);
const baseserver = Player.getCurrentServer();
if (!(baseserver instanceof Server)) {
helpers.log(ctx, () => "cannot backdoor this kind of server");
helpers.log(ctx, () => "Cannot backdoor this kind of server");
return Promise.resolve();
}
const server = baseserver;
const installTime = (calculateHackingTime(server, Player) / 4) * 1000;
// No root access or skill level too low
const canHack = netscriptCanHack(server);
const canHack = netscriptCanHack(server, "backdoor");
if (!canHack.res) {
throw helpers.errorMessage(ctx, canHack.msg || "");
}