aicoded.dev

E-LINT-012: cookie not __Host-

Every app runs on a host of its own, beside other apps under the same domain. A cookie whose name starts with __Host- and that has Path=/, Secure and no Domain stays on the app's own host and on HTTPS: the browser refuses to let any other host set or change it, never sends it to another host, and drops a __Host- cookie that lacks one of these. Any other cookie can reach, or be planted by, the apps beside it. So app code sets only __Host- cookies with Path: "/", Secure: true and no Domain, and reads only __Host- cookies, which no other app can have set. A cookie that deletes one, with MaxAge: -1, follows the same rules.

Lint reads, in hand-written and generated files:

http.SetCookie is E-LINT-009. Lint cannot see a header key or a cookie name that is not a constant, a cookie value taken from the request or from decoded data and set again with .String(), the cookies that r.Cookies() or the Cookie header hand out, empty cookies inside containers (make([]http.Cookie, n), arrays, struct fields), or cookies that the page's JavaScript sets; on the platform the runner is the boundary. It takes Domain off every cookie an app sets and drops one without Secure.

c := &http.Cookie{Name: "__Host-theme", Value: theme, Path: "/", Secure: true, HttpOnly: true, SameSite: http.SameSiteLaxMode}
w.Header().Add("Set-Cookie", c.String())

Fix: name the cookie with a constant that starts with __Host-, set Path: "/" and Secure: true, leave out Domain, set it with w.Header().Add("Set-Cookie", c.String()), and read only cookies whose name starts with __Host-

Markdown · Source on GitHub · framework f46741f