Skip to content

fix: handle removed HTB API v4 endpoints without panicking - #142

Closed
krisdewa wants to merge 1 commit into
GoToolSharing:mainfrom
krisdewa:fix/api-v4-drift
Closed

krisdewa wants to merge 1 commit into
GoToolSharing:mainfrom
krisdewa:fix/api-v4-drift

Conversation

@krisdewa

Copy link
Copy Markdown

Summary

Several commands currently panic instead of failing gracefully because endpoints used by the CLI no longer exist in HTB's API v4. This PR makes those paths tolerant and adds a plain-text fallback for non-TTY usage.

Endpoints observed returning 404 (verified against labs.hackthebox.com/api/v4)

Endpoint Affected command Symptom
/machine/unreleased/ htb-cli machines panic: interface conversion: interface {} is nil, not []interface {} in createFlex
/user/profile/activity/ htb-cli info -u <user> Error fetching data for Activity then nil primitive in TUI
/user/profile/basic/ (without id) htb-cli info -u <user> 404 — the route now requires /user/profile/basic/{id} (this part already works, the id is appended by the CLI)

Still working: /machine/paginated/, /machine/list/retired/paginated/, /user/profile/progress/fortress/{id}, /user/profile/progress/prolab/{id}, /sherlocks?state=*.

Changes

  • cmd/machines.go — tolerate missing/empty payloads, drop the hard os.Exit(1) when the scheduled list is unavailable, and add a plain-text renderer used when stdout is not a TTY (previously instant panic under a pipe/redirect).
  • cmd/info.go — stop requesting the removed activity endpoint, guard against non-map JSON bodies instead of a type assertion panic, and print the user profile (rank, owns, team, fortresses, Pro Labs) as plain text when stdout is not a terminal.
  • cmd/sherlocks.go — same non-TTY plain-text listing fallback.
  • lib/utils/tui.go — never hand a nil tview primitive to a Flex; unavailable sections get a placeholder panel. This is what caused the segfault in DisplayInformationsGUI.

Behaviour is unchanged when a real terminal is attached — the TUI is still used; only the failure modes are fixed.

Verification

Built with Go 1.27.3 on Debian 13, tested against a real (non-VIP) account:

$ htb-cli machines
== Active ==
  DutyBound            Linux    Medium   user:user root:root 2026-10-24
  Autopilot            Windows  Insane   user:user root:root 2026-10-17
== Retired ==
  Cap                  Linux    Easy     user: -    root: -    2021-06-05

$ htb-cli info -u krisdew
Name              : krisdew
Rank              : Hacker (68)
Global Rank       : 940
User Owns         : 43
System Owns       : 39
Rank Ownership    : 22.09% (req 20)
Team              : zerobyteid (#207)
Fortresses:
  - Jet (0/11)
Pro Labs:
  - RastaLabs (0/22)

$ htb-cli sherlocks
== Active ==
  Baggage              Very Easy    -    solves:1698   rating:4.1  DFIR

TUI mode was also exercised under a pty: no panic, the Scheduled pane shows a notice instead of crashing.

go build ./... and go vet ./... pass.

Relation to #141

#141 (HTB API and Sherlocks improvements) is a broader rewrite that also moves some calls to API v5 (/v5/machines?..., v5 activity). This PR is intentionally minimal and orthogonal: it does not change which data is fetched, only makes the current v4 behaviour non-fatal and usable without a TTY. If #141 lands first, the machines/info parts here become redundant, but the non-TTY fallbacks and the nil-guard in lib/utils/tui.go still apply.

Happy to rebase or drop overlapping parts depending on what maintainers prefer.

Several endpoints used by the CLI no longer exist upstream, which made
the affected commands panic with nil pointer / interface conversion
errors instead of failing gracefully.

Endpoints observed as removed (404):
  - /machine/unreleased/            -> machines: panic in createFlex
  - /user/profile/activity/         -> info -u: nil map -> TUI segfault
  - /user/profile/basic/ (no id)    -> replaced by /user/profile/basic/{id}

Changes:
  - machines: tolerate missing/empty payloads, drop the hard exit when the
    scheduled list is unavailable, and add a plain-text renderer for
    non-TTY output instead of crashing under a pipe.
  - info: stop requesting the removed activity endpoint, guard against nil
    JSON bodies, and print the user profile (rank, owns, fortresses/pro
    labs) as plain text when stdout is not a terminal.
  - sherlocks: add the same non-TTY plain-text listing fallback.
  - tui: never hand a nil tview primitive to a Flex (placeholder panels),
    which removes the segfault seen when a section could not be loaded.
@krisdewa krisdewa closed this Oct 26, 2026
@krisdewa
krisdewa deleted the fix/api-v4-drift branch October 26, 2026 07:12
@krisdewa
krisdewa restored the fix/api-v4-drift branch October 26, 2026 07:14
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