|
| 1 | +//go:build dev |
| 2 | + |
| 3 | +// Dev-only loopback auto-login bypass. Compiled in only when the binary |
| 4 | +// is built with `-tags dev`. The tag is set by the `dev:` target in |
| 5 | +// gearbox/Makefile via `air --build.cmd "$(DEV_BUILD_CMD)"`; the |
| 6 | +// production build paths (`make build`, `make deploy-build`) deliberately |
| 7 | +// omit it, so this file and its symbols are not in release binaries at |
| 8 | +// all. See issue #83. |
| 9 | + |
| 10 | +package auth |
| 11 | + |
| 12 | +import ( |
| 13 | + "log/slog" |
| 14 | + "net" |
| 15 | + "net/http" |
| 16 | + "os" |
| 17 | + "sync" |
| 18 | + |
| 19 | + "github.com/sarg3nt/gearbox/internal/framework/database" |
| 20 | + "github.com/sarg3nt/gearbox/internal/framework/models" |
| 21 | +) |
| 22 | + |
| 23 | +const ( |
| 24 | + // devBypassEnvVar gates whether the bypass is allowed to fire even |
| 25 | + // when the binary was built with `-tags dev`. Set to "1" to enable. |
| 26 | + devBypassEnvVar = "GEARBOX_DEV_AUTO_LOGIN" |
| 27 | + |
| 28 | + // devBypassEmail is the email/username of the seeded dev account that |
| 29 | + // the bypass auto-authenticates as. The account must exist (and be |
| 30 | + // active) in the users table; the bypass never creates sessions or |
| 31 | + // auto-promotes a non-existent user. |
| 32 | + devBypassEmail = "dev" |
| 33 | +) |
| 34 | + |
| 35 | +var devBypassBannerOnce sync.Once |
| 36 | + |
| 37 | +// tryDevBypass returns the seeded `dev` user when ALL of these hold: |
| 38 | +// |
| 39 | +// 1. The binary was built with `-tags dev` (gearbox/Makefile's `dev:` |
| 40 | +// target adds the tag via `air --build.cmd "$(DEV_BUILD_CMD)"`; this |
| 41 | +// file is compiled in. The production sibling dev_bypass_off.go is |
| 42 | +// compiled in for tag-less builds and provides a no-op stub.). |
| 43 | +// 2. GEARBOX_DEV_AUTO_LOGIN=1 is set in the process environment. |
| 44 | +// 3. r.RemoteAddr is a loopback address (127.0.0.0/8 or ::1). |
| 45 | +// |
| 46 | +// In production builds the bypass never enters the binary at all — the |
| 47 | +// sibling dev_bypass_off.go provides a hard-coded `nil, false` stub. |
| 48 | +func tryDevBypass(m *Manager, r *http.Request) (*models.User, bool) { |
| 49 | + if os.Getenv(devBypassEnvVar) != "1" { |
| 50 | + return nil, false |
| 51 | + } |
| 52 | + if !requestIsLoopback(r) { |
| 53 | + return nil, false |
| 54 | + } |
| 55 | + user, err := m.db.GetUserByEmail(devBypassEmail) |
| 56 | + if err != nil || user == nil { |
| 57 | + m.logger.Warn("dev auto-login: dev user missing from database; bypass inactive", |
| 58 | + "user", devBypassEmail, "error", err) |
| 59 | + return nil, false |
| 60 | + } |
| 61 | + if user.Status != models.UserStatusActive { |
| 62 | + m.logger.Warn("dev auto-login: dev user is not active; bypass inactive", |
| 63 | + "status", user.Status) |
| 64 | + return nil, false |
| 65 | + } |
| 66 | + return user, true |
| 67 | +} |
| 68 | + |
| 69 | +// requestIsLoopback reports whether r.RemoteAddr resolves to a loopback IP. |
| 70 | +// chi.middleware.RealIP rewrites RemoteAddr from X-Forwarded-For / X-Real-IP, |
| 71 | +// so a proxy between the browser and gearbox would surface the proxy's IP |
| 72 | +// here, not the browser's — that's the intended behavior: requests routed |
| 73 | +// through any proxy aren't loopback and the bypass declines. |
| 74 | +func requestIsLoopback(r *http.Request) bool { |
| 75 | + host, _, err := net.SplitHostPort(r.RemoteAddr) |
| 76 | + if err != nil { |
| 77 | + host = r.RemoteAddr |
| 78 | + } |
| 79 | + ip := net.ParseIP(host) |
| 80 | + if ip == nil { |
| 81 | + return false |
| 82 | + } |
| 83 | + return ip.IsLoopback() |
| 84 | +} |
| 85 | + |
| 86 | +// SeedDevUserIfEnabled creates the `dev` user used by the loopback bypass |
| 87 | +// when GEARBOX_DEV_AUTO_LOGIN=1. The user is given an unusable bcrypt hash |
| 88 | +// (the package-level dummyPasswordHash) so the form-login path can never |
| 89 | +// authenticate as it — only the loopback bypass can. |
| 90 | +// |
| 91 | +// Production builds replace this with a no-op (dev_bypass_off.go). |
| 92 | +func SeedDevUserIfEnabled(db *database.DB, logger *slog.Logger) error { |
| 93 | + if os.Getenv(devBypassEnvVar) != "1" { |
| 94 | + return nil |
| 95 | + } |
| 96 | + created, err := db.EnsureDevUserExists(devBypassEmail, dummyPasswordHash) |
| 97 | + if err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + if created { |
| 101 | + logger.Info("dev auto-login: seeded `dev` user for loopback bypass", |
| 102 | + "email", devBypassEmail) |
| 103 | + } |
| 104 | + return nil |
| 105 | +} |
| 106 | + |
| 107 | +// LogDevBypassStartupBanner emits a loud warning at startup whenever the |
| 108 | +// dev auto-login bypass is potentially active in the running process. |
| 109 | +// Designed to be impossible to miss in logs so an operator never confuses |
| 110 | +// a dev binary for a production one. |
| 111 | +// |
| 112 | +// Production builds replace this with a no-op (dev_bypass_off.go) — the |
| 113 | +// production build targets in gearbox/Makefile (`build`, `deploy-build`) |
| 114 | +// omit `-tags dev`, so this banner cannot fire on a release artifact. |
| 115 | +func LogDevBypassStartupBanner(logger *slog.Logger) { |
| 116 | + if os.Getenv(devBypassEnvVar) != "1" { |
| 117 | + logger.Info("dev auto-login: bypass compiled in (`-tags dev`) but disabled — set GEARBOX_DEV_AUTO_LOGIN=1 to enable") |
| 118 | + return |
| 119 | + } |
| 120 | + devBypassBannerOnce.Do(func() { |
| 121 | + logger.Warn("==========================================================") |
| 122 | + logger.Warn("dev auto-login ACTIVE — loopback requests log in as `dev`") |
| 123 | + logger.Warn("DO NOT USE IN PRODUCTION. Rebuild without `-tags dev` to remove entirely.") |
| 124 | + logger.Warn("==========================================================") |
| 125 | + }) |
| 126 | +} |
0 commit comments