GHSA-F539-XGC6-XW7Q
Authorization Bypass and Path Traversal in code.replace allows Arbitrary File Overwrite
CVSS:3.1/AV:N/AC:L/PR:N/UI:R/S:U/C:H/I:H/A:H
Fuente: GitHub
Ciclo de vida de la vulnerabilidad
| Fecha | Fase | Evento | Detalle |
|---|---|---|---|
| Divulgación | Publicada | GHSA-F539-XGC6-XW7Q divulgada · CVSS 8.8 (GitHub) | |
| Remediación | Parche 0.30.0 | knowns · 0 d desde divulgación | |
| Divulgación | Modificada | Última actualización del registro | |
| Monitoreo | Detectada | Ingresó al índice | |
| Monitoreo | Verificada | Última verificación contra las fuentes |
Overview
Verified. The `code` MCP tool, specifically the `replace` action (`code.replace`), is vulnerable to an Arbitrary File Overwrite via Path Traversal and Absolute Path Injection. The handler accepts user-supplied file paths without validating that they reside within the project root sandbox. Furthermore, because the `replace` action is multiplexed under the generic `code` tool and not explicitly registered as a write-capable operation in the permission registry, it bypasses capability guards and is permitted under read-only presets. This allows an attacker (or a prompt-injected LLM agent) to overwrite arbitrary files on the host system (e.g., `~/.ssh/authorized_keys`, `~/.bashrc`, or system configs), completely escaping the project sandbox.
Affected paths
| File Path / Component | Role | Vulnerability & Execution Impact |
| :--- | :--- | :--- |
| `internal/mcp/handlers/code.go` | Execution Sink | Arbitrary File Overwrite (CWE-22 / CWE-73): `handleCodeReplace` takes an attacker-controlled `path` argument and passes it directly to `os.ReadFile` and `os.WriteFile` without verifying it is contained within `projectRoot(store)`. |
| MCP Tool Registry / Permissions | Authorization Bypass | Missing Capability Registration (CWE-863): The `code` tool with `action=replace` is not explicitly flagged as a write operation in the permission registry. It defaults to read capabilities, bypassing write-restriction presets (e.g., read-only mode). |
Root Cause
Missing Path Containment Check
In `internal/mcp/handlers/code.go`, the `handleCodeReplace` function resolves the target file path:
// /internal/mcp/handlers/code.go (line 611 - 644)
absPath := path
if !filepath.IsAbs(absPath) {
absPath = filepath.Join(projectRoot(store), absPath)
}
data, err := os.ReadFile(absPath)
if err != nil {
return errResult(err.Error())
}
content := string(data)
replacements := 0
next := content
switch mode {
case "literal":
replacements = strings.Count(content, needle)
if replacements > 1 && !allowMultiple {
return errResultf("multiple occurrences found (%d); set allow_multiple_occurrences=true", replacements)
}
next = strings.ReplaceAll(content, needle, repl)
case "regex":
re, err := regexp.Compile(needle)
if err != nil {
return errResult(err.Error())
}
replacements = len(re.FindAllStringIndex(content, -1))
if replacements > 1 && !allowMultiple {
return errResultf("multiple occurrences found (%d); set allow_multiple_occurrences=true", replacements)
}
next = re.ReplaceAllString(content, repl)
default:
return errResult("mode must be literal or regex")
}
if err := os.WriteFile(absPath, []byte(next), 0644); err != nil {
return errResult(err.Error())
}
If `path` is an absolute path (e.g., `/etc/cron.d/malicious`), it is used as-is. If it contains traversal sequences (e.g., `../../.ssh/authorized_keys`), `filepath.Join` natively resolves them. Crucially, there is no post-resolution check (e.g., `strings.HasPrefix(absPath, projectRoot(store))`) to ensure the target remains inside the project sandbox.
Additionally, when using `mode=regex`, an attacker can supply `needle="(?s).*"`. This regex matches the entire file content exactly once. This cleverly bypasses the multi-occurrence guard (`if replacements > 1 && !allowMultiple`), allowing the entire file to be overwritten in a single operation without requiring the `allow_multiple_occurrences` flag.
Authorization Bypass via Action Routing
The MCP server registers the tool generically as `code` with an `action` parameter to multiplex multiple operations (read-only like `symbols`, `definition` and write-heavy like `replace`, `delete`). The permission middleware evaluates capabilities based on the tool name. Because `code.replace` (a destructive write operation) is not explicitly registered in the permission registry with a `CapWrite` or equivalent destructive class, it falls back to the tool's base capability (`CapRead`). Consequently, the server allows `code.replace` to execute even when the agent or user is restricted to a read-only preset. Phase-2 path rules exist only as schema definitions and are not enforced server-side.
Attack Vector
| Phase | Request / Action | Effect |
| :--- | :--- | :--- |
| 1. Capability Check | Agent/User operates in "read-only" mode | The system permits execution of the `code` tool because it is classified under read capabilities (`CapRead`). |
| 2. Path Injection | MCP `tools/call` with `name="code"`, `action="replace"`, `path="/target/file"` | The handler resolves the absolute path or traverses out of the project root without raising a sandbox violation error. |
| 3. File Overwrite | `mode="regex"`, `needle="(?s).*"`, `repl="PWNED"` | `os.WriteFile` overwrites the entire target file with the attacker's payload, achieving persistence or privilege escalation. |
Preconditions: Knowns MCP server running; attacker has access to the MCP interface (e.g., via an LLM agent prompt injection or direct MCP client connection).
Analysis
Path Traversal in `handleCodeReplace`
The code correctly identifies the project root via `projectRoot(store)` but fails to enforce it as a strict boundary. By allowing `filepath.IsAbs(absPath)` to pass through unmodified, an attacker can target any file the Knowns process has write permissions for. For relative paths, `filepath.Join` resolves `../` sequences, allowing escapes from the workspace directory.
// internal/mcp/handlers/code.go (vulnerable sink)
absPath := path
if !filepath.IsAbs(absPath) {
absPath = filepath.Join(projectRoot(store), absPath)
}
// NO containment check: strings.HasPrefix(absPath, projectRoot(store)) is missing
data, err := os.ReadFile(absPath)Authorization Bypass via Action Routing
The `code` tool multiplexes multiple actions under a single tool name. The permission registry evaluates the tool name `code` and grants it read access. It does not inspect the `action` argument at the middleware level to dynamically escalate the required capability to `Write`. This design flaw means that any read-only agent can invoke destructive write actions simply by specifying `action=replace`. The schema validation only checks for required parameters, not security boundaries.
Fix
*Patch is available right now at New Release https://github.com/knowns-dev/knowns/releases.*
Desglose de métricas
Rangos afectados
Estado del parche por paquete
| Paquete / producto | Estado | Corregida en | Desde divulgación | Última versión |
|---|---|---|---|---|
npmknowns |
Corregida | 0.30.0 | 0 d | 0.35.0 no afectada |
0.30.0Arsenal público
Sin exploits, módulos ni PoC públicos conocidos a la fecha de verificación.
https://github.com/knowns-dev/knowns/security/advisories/GHSA-f539-xgc6-xw7qLas referencias externas se muestran para copiar: cve.llama.ec no lo redirige a otros sitios.
