# Code Review

Date: 2026-05-19

## Findings

1. High: front matter `slug` can write outside `--out`

   Explicit slugs are accepted verbatim in `internal/content/content.go:125`, then used to form `Post.URL` in `internal/site/builder.go:137`, and finally converted to a filesystem path in `internal/site/builder.go:342`. A slug containing path components such as `../mkblog-escape-check/pwned` makes the post render outside the configured output directory.

   I reproduced this through the CLI with a temporary fixture:

   ```yaml
   ---
   title: "Escape"
   date: 2026-01-01
   slug: ../mkblog-escape-check/pwned
   ---
   ```

   Command:

   ```sh
   go run ./cmd/mkblog build \
     --config /tmp/mkblog-review-slug/config.yaml \
     --content /tmp/mkblog-review-slug/content \
     --static /tmp/mkblog-review-slug/static \
     --themes themes \
     --out /tmp/mkblog-review-slug/out
   ```

   Observed output included:

   ```text
   /tmp/mkblog-review-slug/mkblog-escape-check/pwned/index.html
   ```

   The build should reject explicit slugs that are not a single normalized URL segment, or normalize them with the same `Slugify` path used for inferred slugs. Add a regression test that asserts slugs containing `/`, `\`, `.`, `..`, leading separators, or empty normalized output fail before rendering.

2. High: `--out` under `--static` recursively copies generated output into itself

   The build resets `opts.OutDir`, then immediately copies the entire static tree into it at `internal/site/builder.go:159` and `internal/site/builder.go:162`. `copyTree` walks the source directory while creating destination directories under that same tree (`internal/site/builder.go:791` to `internal/site/builder.go:807`). If a user runs with `--static static --out static/public`, the walk descends into `static/public`, then `static/public/public`, and so on until the build fails.

   I reproduced this through the CLI:

   ```sh
   timeout 5s go run ./cmd/mkblog build \
     --config /tmp/mkblog-review-out-static/config.yaml \
     --content /tmp/mkblog-review-out-static/content \
     --static /tmp/mkblog-review-out-static/static \
     --themes themes \
     --out /tmp/mkblog-review-out-static/static/public
   ```

   Observed failure:

   ```text
   build failed: create /tmp/mkblog-review-out-static/static/public/public/public/.../asset.txt: file name too long
   ```

   The builder should reject output paths that are equal to or nested under input roots it copies or walks (`ContentDir`, `StaticDir`, and the effective theme directory), or make `copyTree` skip the destination subtree when destination is inside source.

3. Medium: raw HTML in Markdown is always emitted unsanitized

   `internal/render/markdown.go:12` to `internal/render/markdown.go:15` enables Goldmark `gmhtml.WithUnsafe()`, and `internal/site/builder.go:371` passes the rendered body through `template.HTML`. That means post content can include raw `<script>` or event-handler HTML and have it emitted directly into generated pages.

   This may be intentional for a personal static site generator, but it is not documented in `README.md` and is unsafe if content can come from imports, collaborators, comments, or any less-trusted source. Either document that Markdown is trusted code, or make unsafe HTML opt-in via config/CLI and keep the default renderer escaped.

## Test Coverage Reviewed

- Existing tests pass: `go test ./...`
- Current tests cover content parsing, markdown table rendering, server helper behavior, shortcodes, and site build behavior.
- I did not find regression coverage for explicit slug path traversal or output directory nesting under copied input directories.

## Notes

- The black box findings in `BLACK_BOX_MKBLOG.md` still hold: normal build, serve, draft/future filtering, hidden posts, assets, and shortcodes worked in the documented paths.
- The worktree had many pre-existing changes; this review only added `CODE_REVIEW.md`.
