Laravel Go✓ Human-reviewed

Informational browser and OS labels in Laravel Go

Replace display-only Agent browser/platform calls with an app-owned ua-parser projection; explicitly exclude device, bot, version and language compatibility.

Approved 2026-12-04T13:20:39.653304Z · Submission 49

Informational browser and OS labels in Laravel Go

Scope and attribution

Replace only Agent::browser() and Agent::platform() consumers that display informational labels. Do not recreate Mobile Detect or its evolving regular-expression database. Keep a maintained parser dependency and own the small application projection. The package and framework refs this recipe was checked against are in its metadata; reconcile them with the destination application's lockfile before editing.

Dependency source checked on 2026-12-04: ua-parser/uap-go, commit eb720b3ba9950c627fca6008287b140b6c02ede8. Inspect uaparser/parser.go, uaparser/race_test.go and the embedded regex definitions at that ref. This is a research baseline, not a request to upgrade blindly.

Jens Segers' Agent is MIT licensed. The inspected files are src/Agent.php, src/AgentServiceProvider.php, tests/AgentTest.php and composer.json. Preserve the copyright/license notice if copying source or fixtures. The proposed ua-parser Go implementation and its data carry Apache-2.0 licensing; retain applicable notices. This is guidance, not an executed conversion of your app.

At the time of writing, the Package Port catalog listed Go as needing a port and suggested github.com/ua-parser/uap-go as a general-purpose alternative, not a Laravel port. Check get-recipe's current port catalog before choosing to inline.

Source behavior and compatibility conditions

If your Agent source uses the inspected ordered rule tables, browser identification gives more specific tokens such as Edge and Opera priority over Chrome. Platform lookup merges Mobile Detect's operating-system rules with Agent's additions. An unmatched name is PHP false, not the word desktop. Agent delegates mobile/tablet detection and crawler detection to separate libraries; a parser's device family cannot reproduce isPhone(), isDesktop() or isRobot().

ua-parser returns different names and uses its own rule precedence. For example, Windows labels may be versioned rather than simply Windows. This recipe deliberately permits label changes for display; it is unsuitable for persisted enum keys, browser-based feature gates or exact Agent output compatibility without a separately reviewed mapping. Do not derive robot/desktop flags from Other or an empty device family.

Adapt the existing handler

  1. Inventory facades, injected Agent instances, Blade directives and stored labels. Stop removal if any caller still needs device categories, versions, language negotiation or crawler detection. Migrate those separately instead of silently changing their meaning.
  2. Add the researched github.com/ua-parser/uap-go/uaparser revision using Go modules and commit go.mod/go.sum in the destination app. If the installed parser exposes New() and Parse() as inspected, initialize it once at boot, handle its initialization error, and retain its embedded rule definitions. Do not fetch rules during requests or mutate parser options after serving starts.
  3. Read r.Header.Get("User-Agent") from Laravel Go's web.Request. Treat missing, whitespace-only or over-4096-byte values as unknown before invoking the parser. The byte cap is application policy, not an upstream promise. Enforce the server's overall header limit as well.
  4. Project only browser and operating-system family names. Normalize "" and "Other" to JSON null. Keep parsed results request-local. The following is a fragment inside the handler after the input check, with parser supplied by boot configuration:
client := parser.Parse(ua)
labels := map[string]*string{"browser": nil, "platform": nil}
if value := client.UserAgent.Family; value != "" && value != "Other" {
    labels["browser"] = &value
}
if value := client.Os.Family; value != "" && value != "Other" {
    labels["platform"] = &value
}
  1. Render through escaping templates or JSON serialization. User-Agent is spoofable input, never identity, authentication, fraud proof or authorization. Do not log complete headers by default; truncate/redact diagnostic samples and apply retention limits.
  2. If labels affect response content, avoid public shared caching initially. A reviewed finite cache variant can be introduced later; varying on every raw User-Agent is a cache-cardinality and privacy risk.

Trial checklist

Use the source browser fixtures as licensed references, but explicitly approve expected new labels rather than asserting unspecified parity. Include an Edge UA containing both Chrome/ and Edg/, Opera with OPR/, desktop Safari, Android Chrome, iPhone Safari, and a bot UA. Compare browser and OS separately; no bot/device contract is claimed.

  • Missing, whitespace-only, arbitrary text and over-limit headers return null labels without becoming desktop or human.
  • Both sides of the byte cap behave as documented. HTML-looking input renders as text, not markup.
  • Parallel requests with different UAs do not contaminate each other's labels. Exercise the installed parser's actual concurrency path with go test -race ./....
  • Existing authorization and feature support decisions are unchanged; an unknown label never grants extra access.
  • Search the target app for remaining Agent calls before removing its old dependency/configuration.

Run gofmt, go test ./..., go test -race ./..., and go vet ./... in that app. Record Go/framework/parser versions, embedded rules provenance, fixture results and accepted label differences with the change.