Skip to content

Commit 2273445

Browse files
author
env1-agent
committed
fix(service): reject numeric prefixes and unsafe integers in manager parsing
1 parent 9bd3043 commit 2273445

2 files changed

Lines changed: 58 additions & 23 deletions

File tree

‎apps/server/src/cloud/bootService.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,34 @@ it("parses only well-formed manager output and exact versions, rejecting bad cou
10131013
expect(BootService.launchdNotFound("launchctl: input/output error")).toBe(false);
10141014
});
10151015

1016+
it("rejects a numeric prefix and unrepresentable integers in manager output", () => {
1017+
// `pid = 12junk` must not be truncated to 12.
1018+
expect(BootService.parseLaunchdPrint("\tstate = running\n\tpid = 12junk\n")?.pid).toBeUndefined();
1019+
// The last-exit-code field has the same suffix hazard.
1020+
expect(
1021+
BootService.parseLaunchdPrint("\tstate = running\n\tlast exit code = 7junk\n")?.lastExitCode,
1022+
).toBeUndefined();
1023+
// An unrepresentable pid must not become an unsafe number.
1024+
expect(
1025+
BootService.parseLaunchdPrint("\tstate = running\n\tpid = 99999999999999999999\n")?.pid,
1026+
).toBeUndefined();
1027+
// Whole fields still parse, including a signed last exit code.
1028+
expect(
1029+
BootService.parseLaunchdPrint("\tstate = running\n\tpid = 12\n\tlast exit code = -9\n"),
1030+
).toMatchObject({ pid: 12, lastExitCode: -9 });
1031+
// systemd MainPID must be safe before it can imply a live process.
1032+
expect(
1033+
BootService.parseSystemdShow(
1034+
"LoadState=loaded\nActiveState=active\nSubState=running\nMainPID=99999999999999999999",
1035+
)?.mainPid,
1036+
).toBeUndefined();
1037+
expect(
1038+
BootService.parseSystemdShow(
1039+
"LoadState=loaded\nActiveState=active\nSubState=running\nMainPID=4242",
1040+
)?.mainPid,
1041+
).toBe(4242);
1042+
});
1043+
10161044
it.layer(NodeServices.layer)("program path binding", (it) => {
10171045
it.effect("binds only normalized paths inside the selected runtime tree", () =>
10181046
Effect.gen(function* () {

‎apps/server/src/cloud/bootService.ts‎

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -695,11 +695,26 @@ export interface BootServiceSystemdProperties {
695695
readonly result?: string;
696696
}
697697

698+
/**
699+
* Reads a whole field as a safe integer. A numeric prefix with trailing junk
700+
* (`12junk`), a non-decimal spelling, or a value outside `Number.MAX_SAFE_INTEGER`
701+
* stays unknown rather than being truncated or rounded into a misleading
702+
* number. Sign and positivity are decided by the caller's domain rules.
703+
*/
704+
function parseWholeSafeInteger(text: string | undefined): number | undefined {
705+
if (text === undefined) return undefined;
706+
const trimmed = text.trim();
707+
if (!/^-?\d+$/.test(trimmed)) return undefined;
708+
const value = Number.parseInt(trimmed, 10);
709+
return Number.isSafeInteger(value) ? value : undefined;
710+
}
711+
698712
/**
699713
* Parses `systemctl --user show` key=value output. Missing LoadState or
700714
* ActiveState means the answer is unusable and must stay unknown rather than
701715
* defaulting to a healthy value. A malformed or nonpositive `MainPID` and a
702-
* malformed or negative `NRestarts` are dropped rather than coerced.
716+
* malformed or negative `NRestarts` are dropped rather than coerced, including
717+
* values too large to represent exactly as a safe integer.
703718
*/
704719
export function parseSystemdShow(stdout: string): BootServiceSystemdProperties | undefined {
705720
const values = new Map<string, string>();
@@ -711,25 +726,19 @@ export function parseSystemdShow(stdout: string): BootServiceSystemdProperties |
711726
const loadState = values.get("LoadState");
712727
const activeState = values.get("ActiveState");
713728
if (loadState === undefined || activeState === undefined) return undefined;
714-
const restartText = values.get("NRestarts");
715-
const nRestarts =
716-
restartText !== undefined && /^\d+$/.test(restartText)
717-
? Number.parseInt(restartText, 10)
718-
: undefined;
719-
const mainPidText = values.get("MainPID");
720-
const mainPid =
721-
mainPidText !== undefined && /^\d+$/.test(mainPidText)
722-
? Number.parseInt(mainPidText, 10)
723-
: undefined;
729+
const restartValue = parseWholeSafeInteger(values.get("NRestarts"));
730+
const nRestarts = restartValue !== undefined && restartValue >= 0 ? restartValue : undefined;
731+
const mainPidValue = parseWholeSafeInteger(values.get("MainPID"));
732+
const mainPid = mainPidValue !== undefined && mainPidValue > 0 ? mainPidValue : undefined;
724733
const result = values.get("Result");
725734
return {
726735
loadState,
727736
activeState,
728737
subState: values.get("SubState") ?? "",
729738
unitFileState: values.get("UnitFileState") ?? "",
730739
execStart: values.get("ExecStart") ?? "",
731-
...(mainPid !== undefined && mainPid > 0 ? { mainPid } : {}),
732-
...(nRestarts !== undefined && Number.isSafeInteger(nRestarts) ? { nRestarts } : {}),
740+
...(mainPid === undefined ? {} : { mainPid }),
741+
...(nRestarts === undefined ? {} : { nRestarts }),
733742
...(result !== undefined && result !== "" ? { result } : {}),
734743
};
735744
}
@@ -744,21 +753,19 @@ export interface BootServiceLaunchdPrint {
744753
/**
745754
* `launchctl print` has no stable machine format, so only a few anchored tokens
746755
* are read. A response with none of them is malformed and stays unknown. A
747-
* `pid` is only observed when it is a positive integer; a zero or malformed
748-
* value is not a live process.
756+
* `pid` is only observed when the whole field is a positive safe integer; a
757+
* zero, a numeric prefix with trailing junk, or an unrepresentable value is not
758+
* a live process. `last exit code` is read as a whole safe signed integer.
749759
*/
750760
export function parseLaunchdPrint(stdout: string): BootServiceLaunchdPrint | undefined {
751761
if (!/(?:^|\n)[ \t]*(?:state|pid|program|last exit code)[ \t]*=/.test(stdout)) return undefined;
752762
const state = /(?:^|\n)[ \t]*state[ \t]*=[ \t]*([^\n]*)/.exec(stdout)?.[1]?.trim();
753-
const pidText = /(?:^|\n)[ \t]*pid[ \t]*=[ \t]*(\d+)/.exec(stdout)?.[1];
763+
const pidText = /(?:^|\n)[ \t]*pid[ \t]*=[ \t]*([^\n]*)/.exec(stdout)?.[1];
754764
const program = /(?:^|\n)[ \t]*program[ \t]*=[ \t]*([^\n]*)/.exec(stdout)?.[1]?.trim();
755-
const lastExitText = /(?:^|\n)[ \t]*last exit code[ \t]*=[ \t]*(-?\d+)/.exec(stdout)?.[1];
756-
const pidValue = pidText === undefined ? undefined : Number.parseInt(pidText, 10);
757-
const pid =
758-
pidValue !== undefined && Number.isSafeInteger(pidValue) && pidValue > 0 ? pidValue : undefined;
759-
const lastExitValue = lastExitText === undefined ? undefined : Number.parseInt(lastExitText, 10);
760-
const lastExitCode =
761-
lastExitValue !== undefined && Number.isSafeInteger(lastExitValue) ? lastExitValue : undefined;
765+
const lastExitText = /(?:^|\n)[ \t]*last exit code[ \t]*=[ \t]*([^\n]*)/.exec(stdout)?.[1];
766+
const pidValue = parseWholeSafeInteger(pidText);
767+
const pid = pidValue !== undefined && pidValue > 0 ? pidValue : undefined;
768+
const lastExitCode = parseWholeSafeInteger(lastExitText);
762769
return {
763770
...(state === undefined || state === "" ? {} : { state }),
764771
...(pid === undefined ? {} : { pid }),

0 commit comments

Comments
 (0)