mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-21 23:50:45 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c457867535 |
No files matched your search
@@ -967,63 +967,24 @@ test("posix wrapper keeps cd contained in the subshell (documented trade-off)",
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Issue #3485: interactive bash 4.4+ expands PS0 after reading a command and
|
// A cancelled paced delivery may execute only the first physical line.
|
||||||
// before executing it, so a user-defined PS0 hook (commonly a timestamp from
|
// Neither partial delivery nor a complete command may change user shell hooks.
|
||||||
// ~/.bashrc; root usually has none) prints extra output inside the hidden
|
for (const [partial, hasPS0] of [[true, true], [false, true], [true, false], [false, false]]) {
|
||||||
// wrapper's echo window. The wrapper must silence it for its own reads and
|
test(`posix ${partial ? "partial" : "complete"} wrapper preserves ${hasPS0 ? "set" : "unset"} PS0 and existing shell state`, () => {
|
||||||
// restore the original value afterwards.
|
const marker = "__NCMCP_TEST__";
|
||||||
test("posix wrapper saves PS0 before wrapping and restores it after execution", () => {
|
const wrapped = buildWrappedCommand("echo HOOK_STATE_TEST", "posix", marker);
|
||||||
const marker = "__NCMCP_TEST__";
|
const script = partial ? wrapped.slice(0, wrapped.indexOf("\n") + 1) : wrapped;
|
||||||
const wrapped = buildWrappedCommand("echo PS0_HOOK_TEST", "posix", marker);
|
const result = spawnSync("bash", ["--norc", "-c",
|
||||||
assert.match(wrapped, /\[ -n "\$\{PS0-\}" \]&&\{ __NCMCP_PS0=\$\{PS0-\}; PS0=; \}; printf/);
|
(hasPS0 ? 'PS0="[hook] "; ' : 'unset PS0; ') + '__NCMCP_PS0=user_value\n' + script
|
||||||
assert.match(
|
+ 'printf "HOOK_STATE=<%s> SCRATCH=<%s>\\n" "$PS0" "$__NCMCP_PS0"',
|
||||||
wrapped,
|
], { encoding: "utf8" });
|
||||||
/\[ -n "\$\{__NCMCP_PS0-\}" \]&&\{ PS0=\$\{__NCMCP_PS0-\}; unset __NCMCP_PS0; \}; \(exit \$__NCMCP_rc\); \}/,
|
|
||||||
);
|
|
||||||
|
|
||||||
const result = spawnSync(
|
assert.equal(result.error, undefined);
|
||||||
"bash",
|
assert.equal(result.status, 0, result.stderr);
|
||||||
["--norc", "-c", 'PS0="[hook] "\n' + wrapped + 'printf "RESTORED_PS0=%s\\n" "$PS0"'],
|
assert.ok(result.stdout.includes(`HOOK_STATE=<${hasPS0 ? "[hook] " : ""}> SCRATCH=<user_value>`), result.stdout);
|
||||||
{ encoding: "utf8" },
|
if (!partial) assert.match(result.stdout, new RegExp(`${marker}_E:0`));
|
||||||
);
|
});
|
||||||
|
}
|
||||||
assert.equal(result.error, undefined);
|
|
||||||
assert.equal(result.status, 0);
|
|
||||||
assert.match(result.stdout, new RegExp(`${marker}_E:0`));
|
|
||||||
assert.match(result.stdout, /PS0_HOOK_TEST/);
|
|
||||||
// The user's PS0 value is back once the wrapper group finishes, so their
|
|
||||||
// own subsequent commands still run the hook.
|
|
||||||
assert.match(result.stdout, /RESTORED_PS0=\[hook\] /);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("posix wrapper leaves PS0 alone when the user has none set", () => {
|
|
||||||
const marker = "__NCMCP_TEST__";
|
|
||||||
const wrapped = buildWrappedCommand("echo NO_PS0_TEST", "posix", marker);
|
|
||||||
const result = spawnSync(
|
|
||||||
"bash",
|
|
||||||
["--norc", "-c", `unset PS0\n${wrapped}printf "PS0_SET=[%s]\\n" "\${PS0-}"`],
|
|
||||||
{ encoding: "utf8" },
|
|
||||||
);
|
|
||||||
|
|
||||||
assert.equal(result.error, undefined);
|
|
||||||
assert.equal(result.status, 0);
|
|
||||||
assert.match(result.stdout, new RegExp(`${marker}_E:0`));
|
|
||||||
// Nothing was saved and nothing is restored: PS0 stays unset afterwards.
|
|
||||||
assert.match(result.stdout, /NO_PS0_TEST/);
|
|
||||||
const ps0Line = result.stdout.split("\n").find((line) => line.startsWith("PS0_SET="));
|
|
||||||
assert.equal(ps0Line, "PS0_SET=[]");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("posix wrapper silencing still works under sh (dash) where PS0 does not exist", () => {
|
|
||||||
const marker = "__NCMCP_TEST__";
|
|
||||||
const wrapped = buildWrappedCommand("echo DASH_TEST", "posix", marker);
|
|
||||||
const result = spawnSync("sh", ["-c", `${wrapped}printf 'SH_ALIVE\\n'`], { encoding: "utf8" });
|
|
||||||
|
|
||||||
assert.equal(result.error, undefined);
|
|
||||||
assert.equal(result.status, 0);
|
|
||||||
assert.match(result.stdout, new RegExp(`${marker}_E:0`));
|
|
||||||
assert.match(result.stdout, /SH_ALIVE/);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Issue #3265: with plain HISTCONTROL the wrapper line is recorded in bash
|
// Issue #3265: with plain HISTCONTROL the wrapper line is recorded in bash
|
||||||
// history; arrow-up recall then redraws the marker text, the preload __NCMCP_
|
// history; arrow-up recall then redraws the marker text, the preload __NCMCP_
|
||||||
|
|||||||
@@ -274,27 +274,16 @@ function buildPosixWrapperBody(command, marker, startFormat, { omitBashHistoryCl
|
|||||||
cmdAssign = `${marker}_cmd=$(${writes.join(`; \\\n: '${marker}'; `)})`;
|
cmdAssign = `${marker}_cmd=$(${writes.join(`; \\\n: '${marker}'; `)})`;
|
||||||
}
|
}
|
||||||
const historyCleanup = omitBashHistoryCleanup ? [] : [buildBashHistoryCleanup(marker)];
|
const historyCleanup = omitBashHistoryCleanup ? [] : [buildBashHistoryCleanup(marker)];
|
||||||
// Issue #3485: interactive bash 4.4+ expands PS0 after reading a command
|
|
||||||
// and before executing it. A user-set PS0 hook (commonly a pre-exec
|
|
||||||
// timestamp/hook defined in ~/.bashrc — root usually has none) prints extra
|
|
||||||
// output inside the hidden wrapper's echo window, garbling the agent's
|
|
||||||
// command echo while manual typing renders natively. Save PS0 only while it
|
|
||||||
// is currently set (so an interrupted run cannot overwrite the saved value
|
|
||||||
// with empty), silence it for every wrapper read, and restore it after the
|
|
||||||
// wrapper group finishes. zsh/dash/fish have no PS0, so both guards are
|
|
||||||
// no-ops there.
|
|
||||||
const ps0Save = '[ -n "${PS0-}" ]&&{ __NCMCP_PS0=${PS0-}; PS0=; }';
|
|
||||||
const ps0Restore = '[ -n "${__NCMCP_PS0-}" ]&&{ PS0=${__NCMCP_PS0-}; unset __NCMCP_PS0; }';
|
|
||||||
// OpenWrt builds BusyBox with a 512-byte interactive line editor. Keep
|
// OpenWrt builds BusyBox with a 512-byte interactive line editor. Keep
|
||||||
// every physical line below that limit, even for short user commands.
|
// every physical line below that limit, even for short user commands.
|
||||||
// Emit a short standalone input marker before the shell reads any PS2
|
// Emit a short standalone input marker before the shell reads any PS2
|
||||||
// continuation. A leading newline separates it from concurrent PTY echo.
|
// continuation. A leading newline separates it from concurrent PTY echo.
|
||||||
return `${marker}=0; ${ps0Save}; printf '\\n%s\\n' '${marker}_I'\n` + [
|
return `${marker}=0; printf '\\n%s\\n' '${marker}_I'\n` + [
|
||||||
` : '${marker}'; ${cmdAssign}`,
|
` : '${marker}'; ${cmdAssign}`,
|
||||||
`{ printf '${startFormat}' '${marker}_S'; trap ':' INT; ( ${noPager}eval "$${marker}_cmd" ); __NCMCP_rc=$?; trap - INT`,
|
`{ printf '${startFormat}' '${marker}_S'; trap ':' INT; ( ${noPager}eval "$${marker}_cmd" ); __NCMCP_rc=$?; trap - INT`,
|
||||||
`printf '%s\\n' '${marker}_E:'"$__NCMCP_rc"`,
|
`printf '%s\\n' '${marker}_E:'"$__NCMCP_rc"`,
|
||||||
...historyCleanup,
|
...historyCleanup,
|
||||||
`${ps0Restore}; (exit $__NCMCP_rc); }`,
|
`(exit $__NCMCP_rc); }`,
|
||||||
].join(`; \\\n: '${marker}'; `);
|
].join(`; \\\n: '${marker}'; `);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user