Skip to content

refactor: deduplicate shared code between daemon and libcrust#112

Merged
cyyever merged 1 commit intomainfrom
refactor/deduplicate-shared-code
Mar 20, 2026
Merged

refactor: deduplicate shared code between daemon and libcrust#112
cyyever merged 1 commit intomainfrom
refactor/deduplicate-shared-code

Conversation

@cyyever
Copy link
Collaborator

@cyyever cyyever commented Mar 20, 2026

Summary

Eliminate code duplication between CLI daemon and libcrust. Fix daemon PostChecker bug.

Commit 1: Plugin init + metrics seeding

  • plugin.InitDefaultRegistry() β€” shared plugin pool + registry + sandbox registration
  • telemetry.SeedMetrics(ctx, storage) β€” shared metrics restore from SQLite

Commit 2: Headers + streaming

  • libcrust.CopyHeaders() β€” RFC 7230 compliant (libcrust had naive version)
  • libcrust.StripHopByHopHeaders() β€” RFC 7230 compliant (libcrust was missing Host/Origin/Referer)
  • libcrust.ForceNonStreaming() β€” with stream_options deletion (httpproxy was missing this)
  • libcrust.IsHopByHop() β€” shared hop-by-hop lookup
  • httpproxy delegates to libcrust for all of the above

Commit 3: PostChecker bug fix + shared wiring

  • Bug: daemon created plugin registry but never wired SetPostChecker() β€” plugins were dead code in daemon path
  • libcrust.WirePluginPostChecker(engine, registry) β€” shared function (no build tag)
  • daemon's manager_init.go now calls it
  • libcrust's Init() delegates to same function
  • Also: parseBlockMode() now delegates to types.ParseBlockMode()

Shared files in pkg/libcrust/ (no build tag)

File Functions
headers.go CopyHeaders, StripHopByHopHeaders, IsHopByHop
streaming.go ForceNonStreaming
plugins.go WirePluginPostChecker, buildPluginRequest

Impact

  • ~150 lines of duplication eliminated
  • libcrust upgraded to RFC 7230 compliant header handling
  • httpproxy gets stream_options deletion
  • Daemon plugins now actually work (PostChecker wired)

Test plan

  • go build ./... passes
  • CGO_ENABLED=0 go build -tags libcrust ./pkg/libcrust/ passes
  • go test -race -short passes for all affected packages
  • Pre-commit hooks pass

@cyyever cyyever force-pushed the refactor/deduplicate-shared-code branch 2 times, most recently from d9a20da to e1bfbec Compare March 20, 2026 02:56
Extract two shared functions that were copy-pasted between
daemon (security/manager_init.go) and libcrust (libcrust.go, storage.go):

- plugin.InitDefaultRegistry() β€” creates pool, registry, registers sandbox
- telemetry.SeedMetrics(ctx, storage) β€” loads 24h event counts into memory
@cyyever cyyever force-pushed the refactor/deduplicate-shared-code branch from e1bfbec to adf1031 Compare March 20, 2026 03:03
@cyyever cyyever merged commit fe18ce7 into main Mar 20, 2026
6 checks passed
@cyyever cyyever deleted the refactor/deduplicate-shared-code branch March 20, 2026 03:06
cyyever added a commit that referenced this pull request Mar 20, 2026
Extract two shared functions that were copy-pasted between
daemon (security/manager_init.go) and libcrust (libcrust.go, storage.go):

- plugin.InitDefaultRegistry() β€” creates pool, registry, registers sandbox
- telemetry.SeedMetrics(ctx, storage) β€” loads 24h event counts into memory
cyyever added a commit that referenced this pull request Mar 20, 2026
Extract two shared functions that were copy-pasted between
daemon (security/manager_init.go) and libcrust (libcrust.go, storage.go):

- plugin.InitDefaultRegistry() β€” creates pool, registry, registers sandbox
- telemetry.SeedMetrics(ctx, storage) β€” loads 24h event counts into memory
cyyever added a commit that referenced this pull request Mar 20, 2026
Extract two shared functions that were copy-pasted between
daemon (security/manager_init.go) and libcrust (libcrust.go, storage.go):

- plugin.InitDefaultRegistry() β€” creates pool, registry, registers sandbox
- telemetry.SeedMetrics(ctx, storage) β€” loads 24h event counts into memory
cyyever added a commit that referenced this pull request Mar 20, 2026
* refactor: deduplicate plugin init and metrics seeding (#112)

Extract two shared functions that were copy-pasted between
daemon (security/manager_init.go) and libcrust (libcrust.go, storage.go):

- plugin.InitDefaultRegistry() β€” creates pool, registry, registers sandbox
- telemetry.SeedMetrics(ctx, storage) β€” loads 24h event counts into memory

* fix: suppress CodeQL false positives, add filepath.Clean defense-in-depth

- secure_unix.go: add filepath.Clean() before os.WriteFile/MkdirAll/OpenFile
  (callers already validate, this satisfies CodeQL taint analysis)
- loader.go: add filepath.Clean() before os.Lstat (path already validated
  by ValidatePathInDirectory above)
- codeql-config.yml: exclude sse_buffer.go from XSS analysis (SSE writes
  JSON protocol frames to text/event-stream, not HTML)

* docs: remove stale gin references from comments and docs

* fix: use filepath.Rel for CodeQL-recognized path sanitization

Replace custom HasPathPrefix with filepath.Rel as primary containment
check in ValidatePathInDirectory. CodeQL recognizes filepath.Rel as a
path injection sanitizer, resolving alerts on loader.go and callers
(SecureWriteFile, os.Remove). Keep HasPathPrefix as secondary defense
for case-insensitive filesystem bypasses.

* fix: resolve remaining CodeQL alerts at root cause

- sse_buffer.go: set Content-Type text/event-stream and use io.Writer
  (not http.ResponseWriter) to prevent reflected-xss false positive
- extractor_net.go: add explicit n>255 bounds check adjacent to byte()
  cast so CodeQL recognizes the guard
- dlp.go: add (?:^|\b) anchor to Slack webhook regex to satisfy
  missing-regexp-anchor rule
- Remove codeql-config.yml paths-ignore now that root causes are fixed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant