MCPcopy Create free account
hub / github.com/Noumena-Network/code / isCommandReadOnly

Function isCommandReadOnly

src/tools/BashTool/readOnlyValidation.ts:1768–1842  ·  view source on GitHub ↗

* Checks if a single command string is read-only based on READONLY_COMMAND_REGEXES. * Internal helper function that validates individual commands. * * @param command The command string to check * @returns true if the command is read-only

(command: string)

Source from the content-addressed store, hash-verified

1766 * @returns true if the command is read-only
1767 */
1768function isCommandReadOnly(command: string): boolean {
1769 // Handle common stderr-to-stdout redirection pattern
1770 // This handles both "command 2>&1" at the end of a full command
1771 // and "command 2>&1" as part of a pipeline component
1772 let testCommand = command.trim()
1773 if (testCommand.endsWith(' 2>&1')) {
1774 // Remove the stderr redirection for pattern matching
1775 testCommand = testCommand.slice(0, -5).trim()
1776 }
1777
1778 // Check for Windows UNC paths that could be vulnerable to WebDAV attacks
1779 // Do this early to prevent any command with UNC paths from being marked as read-only
1780 if (containsVulnerableUncPath(testCommand)) {
1781 return false
1782 }
1783
1784 // Check for unquoted glob characters and expandable `$` variables that could
1785 // bypass our regex-based security checks. We can't know what these expand to
1786 // at runtime, so we can't verify the command is read-only.
1787 //
1788 // Globs: `python *` could expand to `python --help` if such a file exists.
1789 //
1790 // Variables: `uniq --skip-chars=0$_` — bash expands `$_` at runtime to the
1791 // last arg of the previous command. With IFS word splitting, this smuggles
1792 // positional args past "flags-only" regexes like uniq's `\S+`. The `$` token
1793 // check inside isCommandSafeViaFlagParsing only covers COMMAND_ALLOWLIST
1794 // commands; hand-written regexes in READONLY_COMMAND_REGEXES (uniq, jq, cd)
1795 // have no such guard. See containsUnquotedExpansion for full analysis.
1796 if (containsUnquotedExpansion(testCommand)) {
1797 return false
1798 }
1799
1800 // Tools like git allow `--upload-pack=cmd` to be abbreviated as `--up=cmd`
1801 // Regex filters can be bypassed, so we use strict allowlist validation instead.
1802 // This requires defining a set of known safe flags. NCode can help with this,
1803 // but please look over it to ensure it didn't add any flags that allow file writes
1804 // code execution, or network requests.
1805 if (isCommandSafeViaFlagParsing(testCommand)) {
1806 return true
1807 }
1808
1809 for (const regex of READONLY_COMMAND_REGEXES) {
1810 if (regex.test(testCommand)) {
1811 // Prevent git commands with -c flag to avoid config options that can lead to code execution
1812 // The -c flag allows setting arbitrary git config values inline, including dangerous ones like
1813 // core.fsmonitor, diff.external, core.gitProxy, etc. that can execute arbitrary commands
1814 // Check for -c preceded by whitespace and followed by whitespace or equals
1815 // Using regex to catch spaces, tabs, and other whitespace (not part of other flags like --cached)
1816 if (testCommand.includes('git') && /\s-c[\s=]/.test(testCommand)) {
1817 return false
1818 }
1819
1820 // Prevent git commands with --exec-path flag to avoid path manipulation that can lead to code execution
1821 // The --exec-path flag allows overriding the directory where git looks for executables
1822 if (
1823 testCommand.includes('git') &&
1824 /\s--exec-path[\s=]/.test(testCommand)
1825 ) {

Callers 1

checkReadOnlyConstraintsFunction · 0.85

Calls 3

Tested by

no test coverage detected