Skip to content

fix: harden HTTP server and request handling - #144

Merged
joachimschmidt557 merged 1 commit into
timewarrior-synchronize:mainfrom
schtobia:dev/harden_http_server
Aug 19, 2026
Merged

joachimschmidt557 merged 1 commit into
timewarrior-synchronize:mainfrom
schtobia:dev/harden_http_server

Conversation

@schtobia

Copy link
Copy Markdown
Contributor

This PR hardens the HTTP server against common abuse scenarios and tightens request handling on the sync endpoint.

Changes

  • Configure explicit server timeouts (main.go): Replace the bare http.ListenAndServe call with an http.Server configured with ReadHeaderTimeout (10s), ReadTimeout (30s), WriteTimeout (60s), and IdleTimeout (120s). This mitigates Slowloris-style attacks and prevents connections from exhausting server resources by staying open indefinitely.
  • Restrict /api/sync to POST (sync/handle.go): The sync handler now rejects non-POST requests with 405 Method Not Allowed instead of attempting to parse them as sync payloads.
  • Remove redundant file close (sync/user_management.go): Drop the explicit destFile.Close() in the error path of AddKey; log.Fatalf terminates the process anyway, and the file handle is already managed by the deferred close.
  • Minor cleanup: Grouped var declarations, fixed import ordering, and corrected indentation of the health handler.

Motivation

Running with Go's default http.Server settings means no timeouts at all, leaving the server vulnerable to slow-client resource exhaustion. Additionally, the sync endpoint accepted any HTTP method, which made request handling less strict than necessary. These changes improve robustness without altering the sync protocol or API behavior for legitimate clients.

Testing

  • Verified the server starts and responds on /api/health as before.
  • Confirmed GET /api/sync now returns 405 Method Not Allowed, while well-formed POST requests continue to sync successfully.

@schtobia schtobia closed this Aug 18, 2026
@schtobia
schtobia deleted the dev/harden_http_server branch August 18, 2026 13:20
@schtobia
schtobia restored the dev/harden_http_server branch August 18, 2026 19:00
@schtobia

Copy link
Copy Markdown
Contributor Author

sorry, this was a mistake

@schtobia schtobia reopened this Aug 18, 2026
@joachimschmidt557
joachimschmidt557 merged commit c95c7ff into timewarrior-synchronize:main Aug 19, 2026
3 checks passed
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.

2 participants