Scope /health/terminal to the caller's tenant, and backfill the blank codes

The outlet check could not go in the middleware with the others: it scopes a
request by the location it names, and this route names only a terminal code —
free text minted at the till. The outlet is not known until after the lookup,
so the check happens in the handler, reading the outlet off the heartbeat
itself rather than off the request.

Guessing "T4A9" now answers 403 instead of another shop's pending bills,
takings so far today, and app version. Silent when no token is presented, in
step with middleware.PosAuth: while POS_AUTH_REQUIRED is off, real tills are
still calling this unauthenticated and refusing them would blank the fleet
board for exactly the terminals it watches.

scratch/termbackfill repairs the rows left behind by the posTerminalFor bug.
The code was never lost — it is the third segment of the invoice number the
till printed in the same transaction — so this derives rather than guesses,
and refuses to write a code that outlet has never filed a bill under. 39 of
40 recovered; the holdout is a synthetic proof bill whose only sibling is
itself, and unverifiable codes stay blank rather than becoming plausible.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Suriya
2026-08-10 11:22:15 +05:30
parent f7895d3ccf
commit 5c04d40f0e
3 changed files with 252 additions and 0 deletions

View File

@@ -181,6 +181,12 @@ func (ctl *PosController) Catalogue(c *fiber.Ctx) error {
// TerminalHealth returns one till's live state, for a support call that starts
// with a terminal code.
//
// The outlet check cannot live in the middleware like every other POS route's
// does. The middleware scopes a request by the location it *names*, and this
// request names none — only a terminal code, which is free text minted at the
// till and belongs to whichever shop is holding that device. So the outlet is
// not known until after the lookup, and the check has to happen here.
func (ctl *PosController) TerminalHealth(c *fiber.Ctx) error {
terminalID := strings.TrimSpace(c.Query("terminal_id"))
if terminalID == "" {
@@ -200,6 +206,11 @@ func (ctl *PosController) TerminalHealth(c *fiber.Ctx) error {
// Not an error. The till has simply not reported inside its TTL, which
// is the answer the caller wanted — said plainly rather than as a 404
// that reads like the terminal does not exist.
//
// Answered without an outlet check, and safely so: there is nothing to
// check against and nothing to leak. "Offline" is the same answer for a
// terminal code that was never issued, so guessing codes reveals only
// that guessing does not work.
return c.JSON(fiber.Map{
"code": http.StatusOK,
"status": true,
@@ -211,9 +222,54 @@ func (ctl *PosController) TerminalHealth(c *fiber.Ctx) error {
})
}
if err := ctl.posTerminalInScope(c, fields); err != nil {
return c.Status(http.StatusForbidden).JSON(fiber.Map{
"code": http.StatusForbidden, "status": false, "message": err.Error(),
})
}
return c.JSON(fiber.Map{"code": http.StatusOK, "status": true, "details": fields})
}
// posTerminalInScope refuses a heartbeat belonging to somebody else's shop.
//
// Reads the outlet off the heartbeat itself, because that — not the request —
// is the authority on which shop a terminal code belongs to. A caller who
// guesses "T4A9" gets a 403 rather than another shop's pending-bill count,
// takings so far today, and app version.
//
// Silent when the request carries no token, matching middleware.PosAuth: while
// POS_AUTH_REQUIRED is off, tills in the field are still calling these routes
// unauthenticated, and refusing them here would take the fleet board down for
// exactly the terminals it exists to watch. Once the flag is on, an untokened
// request never reaches this handler.
func (ctl *PosController) posTerminalInScope(c *fiber.Ctx, fields map[string]string) error {
claims, ok := middleware.PosClaimsFrom(c)
if !ok {
return nil
}
locationID, err := strconv.Atoi(strings.TrimSpace(fields["location_id"]))
if err != nil || locationID <= 0 {
// A heartbeat that cannot say where it came from cannot be shown to a
// caller who must be scoped. Refusing beats guessing.
return fmt.Errorf("this terminal's outlet could not be determined")
}
if locationID == claims.Locationid {
return nil
}
allowed, err := ctl.posService.LocationAllowed(claims.Tenantid, locationID)
if err != nil {
return fmt.Errorf("could not verify outlet access")
}
if !allowed {
return fmt.Errorf("this terminal belongs to an outlet this session cannot reach")
}
return nil
}
// LocationHealth returns every till at a shop — the "which counters are dark"
// board. Tills that have stopped reporting come back marked offline rather than
// being omitted, because a missing till is exactly what somebody is looking for.