The POS surface was open. A till named its own outlet — `store_id` in a query or in an ingest batch — and was believed, so one number changed in Settings read another tenant's catalogue or posted bills into their books. There was no middleware in the codebase at all, and the `JWT_SECRET_KEY` in the config was read and never used. Products were never mis-scoped: `resolvePosStore` already derived the tenant from the location and the catalogue query already filtered on both. The tenant was never taken from the wire. What was missing was any check that the caller was entitled to the location they named. So the outlet now comes *out* of a sign-in rather than going *in* from the till. `POST /pos/login` authenticates against the same `app_users` rows the web console uses — one account store, so deactivating a leaver closes both doors — and answers with the outlets that account may reach, sealed in an HMAC-SHA256 token the terminal cannot edit. Two checks then guard everything else, in order: the token verifies, and the outlet named in the request belongs to the token's tenant. The second is the one that matters — a valid token is a licence to name *your* outlets, not any. Notes on the awkward parts: - The guard reads the outlet from the body as well as the query. The two routes that write carry `store_id` in a JSON batch and never in the URL, so a query-only check would have left exactly the dangerous call unguarded. - Three spellings of one thing survive — `store_id`, `locationid`, `location_id`. All three are read rather than normalised, because renaming them breaks terminals already in the field. - `POS_AUTH_REQUIRED` defaults to false. Tills are billing real customers against the open endpoints right now and enforcing at deploy would stop every one mid-trade. A token is still verified when sent, and a wrong-tenant token still refused; the flag only governs requests carrying none. - `POS_TOKEN_SECRET` has no baked-in fallback and fails loudly. A development secret in source is the same as no signature at all. - `configid` is inferred when the till does not send it, because a person at a counter has no way to know theirs. `authname` is not unique in this schema — live data has one address twice under one configid — so an ambiguous match is refused rather than resolved by LIMIT 1, which could bill into the wrong tenant's books. Verified against live data: 58 accounts across 34 tenants can open a till, an account pinned to a location resolves to it alone, a tenant-level account gets all six of its outlets, and a cross-tenant outlet request is refused. Passwords are still plaintext platform-wide. Flagged at the comparison site; fixing it is a migration touching every login path, not this endpoint. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
93 lines
3.4 KiB
Go
93 lines
3.4 KiB
Go
// Who can actually open a till, across the whole platform.
|
|
//
|
|
// Read-only. Answers the question the account model raises the moment sign-in
|
|
// becomes real: the endpoint is open to every tenant, so *which* of them can
|
|
// genuinely reach it, and does anyone reach it who should not.
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
|
|
"github.com/joho/godotenv"
|
|
"gorm.io/driver/postgres"
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/logger"
|
|
)
|
|
|
|
func main() {
|
|
_ = godotenv.Load()
|
|
dsn := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
|
|
os.Getenv("DB_HOST"), os.Getenv("DB_PORT"), os.Getenv("DB_USER"),
|
|
os.Getenv("DB_PASSWORD"), os.Getenv("DB_NAME"))
|
|
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{Logger: logger.Default.LogMode(logger.Silent)})
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// The exact predicate posLoginCandidates + PosLogin apply.
|
|
eligible := `
|
|
FROM app_users a
|
|
WHERE LOWER(COALESCE(a.status,'active')) <> 'inactive'
|
|
AND COALESCE(a.password,'') <> ''
|
|
AND COALESCE(a.authname,'') <> ''
|
|
AND COALESCE(a.tenantid,0) > 0
|
|
AND EXISTS (SELECT 1 FROM tenantlocations l
|
|
WHERE l.tenantid = a.tenantid
|
|
AND LOWER(COALESCE(l.status,'active')) <> 'inactive'
|
|
AND (COALESCE(a.locationid,0) = 0 OR l.locationid = a.locationid))`
|
|
|
|
var total, tenants int
|
|
db.Raw(`SELECT COUNT(*) ` + eligible).Scan(&total)
|
|
db.Raw(`SELECT COUNT(DISTINCT a.tenantid) ` + eligible).Scan(&tenants)
|
|
|
|
var allUsers, allTenants int
|
|
db.Raw(`SELECT COUNT(*) FROM app_users`).Scan(&allUsers)
|
|
db.Raw(`SELECT COUNT(*) FROM tenants`).Scan(&allTenants)
|
|
|
|
fmt.Printf("app_users rows %d\n", allUsers)
|
|
fmt.Printf(" can open a till %d\n", total)
|
|
fmt.Printf("tenants %d\n", allTenants)
|
|
fmt.Printf(" with a usable login %d\n\n", tenants)
|
|
|
|
// Which tenants, and whether they have a catalogue to sell.
|
|
var rows []struct {
|
|
Tenantid int
|
|
Tenantname string
|
|
Users int
|
|
Outlets int
|
|
Products int
|
|
}
|
|
db.Raw(`
|
|
SELECT t.tenantid, COALESCE(t.tenantname,'') AS tenantname,
|
|
(SELECT COUNT(*) FROM app_users a WHERE a.tenantid=t.tenantid
|
|
AND LOWER(COALESCE(a.status,'active'))<>'inactive'
|
|
AND COALESCE(a.password,'')<>'' AND COALESCE(a.authname,'')<>'') AS users,
|
|
(SELECT COUNT(*) FROM tenantlocations l WHERE l.tenantid=t.tenantid
|
|
AND LOWER(COALESCE(l.status,'active'))<>'inactive') AS outlets,
|
|
(SELECT COUNT(*) FROM productlocations p WHERE p.tenantid=t.tenantid) AS products
|
|
FROM tenants t
|
|
WHERE EXISTS (SELECT 1 FROM app_users a WHERE a.tenantid=t.tenantid
|
|
AND LOWER(COALESCE(a.status,'active'))<>'inactive'
|
|
AND COALESCE(a.password,'')<>'' AND COALESCE(a.authname,'')<>'')
|
|
ORDER BY products DESC, t.tenantid`).Scan(&rows)
|
|
|
|
fmt.Printf("%-8s %-34s %6s %8s %9s\n", "tenant", "name", "logins", "outlets", "products")
|
|
fmt.Println("---------------------------------------------------------------------------")
|
|
for _, r := range rows {
|
|
fmt.Printf("%-8d %-34s %6d %8d %9d\n", r.Tenantid, r.Tenantname, r.Users, r.Outlets, r.Products)
|
|
}
|
|
|
|
// Roles. The POS login does not check one, so this says who slips through.
|
|
var roles []struct {
|
|
Roleid int
|
|
C int
|
|
}
|
|
db.Raw(`SELECT COALESCE(a.roleid,0) AS roleid, COUNT(*) AS c ` + eligible + ` GROUP BY 1 ORDER BY c DESC`).Scan(&roles)
|
|
fmt.Println("\neligible logins by roleid:")
|
|
for _, r := range roles {
|
|
fmt.Printf(" role %-4d %d\n", r.Roleid, r.C)
|
|
}
|
|
}
|