Skip to content

osctrl-tls: hardening — constant-time secrets, per-IP enroll rate-limit, audit-log on failed enrolls (round 1b of 3)#816

Merged
javuto merged 2 commits into
jmpsec:mainfrom
alvarofraguas:pr/round-1b-osctrl-tls-hardening
May 17, 2026
Merged

osctrl-tls: hardening — constant-time secrets, per-IP enroll rate-limit, audit-log on failed enrolls (round 1b of 3)#816
javuto merged 2 commits into
jmpsec:mainfrom
alvarofraguas:pr/round-1b-osctrl-tls-hardening

Conversation

@alvarofraguas
Copy link
Copy Markdown
Collaborator

Carves the osctrl-tls-facing changes out of #813 into their own PR, as requested in #813 (comment), so the TLS surface can be reviewed and tested in isolation.

Stack order: depends on #813. The shared infrastructure consumed here — pkg/auditlog.FailedEnroll, pkg/ratelimit, utils.SetTrustedProxies / updated GetIP — ships in #813. If you read the diff here against main it will include those #813 changes; the carve-out is the 5 files under cmd/tls/ + deploy/config/tls.yml. Once #813 merges this PR's diff drops to those 5 files only.

What this changes

5 files in cmd/tls/ + deploy/config/tls.yml. Three independent defenses on the only osctrl service that is internet-facing in a typical deployment:

1. Constant-time secret comparison

cmd/tls/handlers/utils.gocheckValidSecret and checkValidRemovePath use subtle.ConstantTimeCompare instead of the previous byte-by-byte short-circuiting compare. Removes a response-time timing oracle on the public enroll endpoint.

2. Per-IP rate-limit on /enroll

cmd/tls/main.go — 20 req/min per remote IP, idle eviction after 10 min, backed by pkg/ratelimit (from #813). Rejected requests:

  • return 429
  • record AuditLog.FailedEnroll(ip, env, "rate limit exceeded", 0) so SoC tooling can alert

3. Audit-log on failed enrollment

  • cmd/tls/handlers/handlers.goHandlersTLS gains an AuditLog field + WithAuditLog option; defensive default is a disabled manager so handler code can call h.AuditLog.FailedEnroll(...) without nil-checks.
  • cmd/tls/handlers/post.goEnrollHandler records FailedEnroll on the invalid-secret path.
  • cmd/tls/main.go — initializes auditlog.CreateAuditLogManager and threads it via WithAuditLog. Service name osctrl-tls flows through.

4. Trusted-proxies plumbing

cmd/tls/main.go — reads flagParams.Service.TrustedProxies (CIDR list) and calls utils.SetTrustedProxies before any request reaches GetIP. Empty default = GetIP ignores X-Forwarded-For / X-Real-IP, preventing internet-side header-spoofed bypass of the rate-limiter or audit-log poisoning.

Config

deploy/config/tls.yml:

  • auditLog: true (on by default; was false)
  • trustedProxies: "" (empty default; operators behind a trusted reverse proxy can list CIDRs)

Verified

  • go build ./... clean
  • go vet ./... clean
  • go test ./cmd/tls/... green
  • Live smoke on a dev stack: 4 osquery nodes enrolled, audit_logs records enroll_failure rows with correct IPs for forced bad-secret attempts

Test plan

  • CI: lint + build + tests pass once workflows are approved
  • Manual: with trustedProxies empty, confirm X-Forwarded-For: 1.2.3.4 doesn't override RemoteAddr in audit_logs
  • Manual: trigger >20 enroll requests from the same IP in <1 min, confirm 429 + audit-log entry
  • Manual: bad enroll-secret produces an enroll_failure audit-log row with the source IP

@javuto javuto self-assigned this May 14, 2026
@javuto javuto added ✨ enhancement New feature or request osctrl-tls osctrl-tls related changes 🔐 security Security related issues labels May 14, 2026
@alvarofraguas alvarofraguas force-pushed the pr/round-1b-osctrl-tls-hardening branch from 53f2e99 to 2511d7b Compare May 15, 2026 15:10
…it, audit-log on failed enrolls

Carves the osctrl-tls-facing changes out of the original "round 1"
security PR (jmpsec#813) into their own PR so the TLS surface
can be isolated for review and testing. Depends on the shared
infrastructure (pkg/auditlog FailedEnroll, pkg/ratelimit, and
utils.SetTrustedProxies / GetIP changes) that lands in jmpsec#813.

== Threats addressed ==

osctrl-tls is the only osctrl service that is internet-facing in a
typical deployment — every osquery node POSTs to /enroll, /config,
/log, etc. before any authentication exists. That makes its surface
the most exposed and the most worth hardening.

This PR adds three independent defenses:

== 1. Constant-time secret comparison ==

cmd/tls/handlers/utils.go: checkValidSecret and checkValidRemovePath
  switched from `strings.TrimSpace(secret) == env.Secret` (byte-by-byte
  short-circuiting compare) to `subtle.ConstantTimeCompare`. The old
  form leaked secret bytes via response-time timing — an attacker on
  the open internet could iterate one byte at a time. Constant-time
  compare eliminates that oracle.

== 2. Per-IP rate-limit on /enroll ==

cmd/tls/main.go: 20 requests/minute per remote IP, idle eviction after
  10 minutes. Rejected requests:
    - return 429
    - record an `AuditLog.FailedEnroll(ip, env, "rate limit exceeded", 0)`
      so SoC tooling can alert on enroll abuse / spray.
  Backed by pkg/ratelimit (from jmpsec#813). The default 20/min easily covers
  any realistic enroll burst (a wave of provisioning at site bring-up)
  while shutting down brute-force / DoS attempts.

== 3. Audit-log on failed enrollment ==

cmd/tls/handlers/handlers.go: HandlersTLS grows an AuditLog field +
  WithAuditLog option. Defensive default is a disabled manager so
  handler code can call h.AuditLog.FailedEnroll(...) without nil checks.

cmd/tls/handlers/post.go: EnrollHandler records FailedEnroll on the
  invalid-secret path. Combined with the rate-limit failures above,
  the audit log now carries the full picture of unwanted enroll
  traffic.

cmd/tls/main.go: initializes auditlog.CreateAuditLogManager and
  threads it into handlers via WithAuditLog. Service name "osctrl-tls"
  flows through to audit rows.

== 4. Trusted-proxies plumbing ==

cmd/tls/main.go: reads flagParams.Service.TrustedProxies (CIDR list)
  and calls utils.SetTrustedProxies before any request can reach
  GetIP. Empty default means GetIP ignores X-Forwarded-For /
  X-Real-IP and uses RemoteAddr — preventing internet-side
  header-spoofed bypass of the rate-limiter or audit-log poisoning.

== Config ==

deploy/config/tls.yml:
  - auditLog: true (on by default; was false)
  - trustedProxies: ""  (empty by default; operators behind a trusted
    reverse proxy can list the proxy CIDRs)

Verified: go build ./... clean, go vet ./... clean, go test ./cmd/tls/...
green. Live smoke on the dev stack: 4 osquery nodes enrolled, the
audit_logs table records `enroll_failure` rows with correct IPs for
forced bad-secret attempts.
@alvarofraguas alvarofraguas force-pushed the pr/round-1b-osctrl-tls-hardening branch from 2511d7b to b017293 Compare May 16, 2026 08:06
The {env} path segment matches any non-slash string, so a sprayer
hitting /<arbitrary-junk>/enroll could stuff up to one path segment of
attacker-controlled text per rate-limit-rejected request into
audit_logs.line. Not SQL-injectable (gorm parameterized) and not XSS
in the React SPA (escaped), but it polluted the table and made SoC
alerting noisier without adding signal.

The EnrollHandler already validates the env UUID via utils.CheckUUID
before any audit write — the rate-limit callback now does the same.
Rejected segments are recorded as "<invalid>" so SoC tooling still
sees the activity (and can alert on the literal "<invalid>" if it
wants), but the table can't get polluted with arbitrary attacker text.

Verified on the dev stack: 25 spray requests against
/totally-not-a-uuid-xxxxxxx/enroll trigger the rate limit at request
21+; all 5 rate-limit-rejected audit rows now show
`failed enroll for env <invalid>: rate limit exceeded`, source_ip
captured correctly.
@javuto javuto merged commit 93552d4 into jmpsec:main May 17, 2026
2 checks passed
@alvarofraguas alvarofraguas deleted the pr/round-1b-osctrl-tls-hardening branch May 17, 2026 17:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

✨ enhancement New feature or request osctrl-tls osctrl-tls related changes 🔐 security Security related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants