Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/internal/api/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (a *App) handleCallback(w http.ResponseWriter, r *http.Request) {
fail("store")
return
}
http.SetCookie(w, &http.Cookie{Name: auth.StateCookieName, Path: "/", MaxAge: -1})
http.SetCookie(w, a.Sessions.ClearStateCookie())

// CLI login: mint an API token and hand it back to the CLI's loopback
// listener instead of setting a browser session.
Expand Down
9 changes: 9 additions & 0 deletions backend/internal/auth/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,15 @@ func (s *Sessions) ClearCLICookie() *http.Cookie {
return &http.Cookie{Name: CLICookieName, Path: "/", MaxAge: -1, Secure: !s.devMode, HttpOnly: true}
}

// ClearStateCookie expires the OAuth state cookie with the same security flags
// it was set with (HttpOnly, Secure in prod, SameSite=Lax).
func (s *Sessions) ClearStateCookie() *http.Cookie {
return &http.Cookie{
Name: StateCookieName, Path: "/", MaxAge: -1,
Secure: !s.devMode, HttpOnly: true, SameSite: http.SameSiteLaxMode,
}
}

// VerifyState checks the OAuth state cookie against the state query parameter.
func (s *Sessions) VerifyState(r *http.Request, state string) bool {
c, err := r.Cookie(StateCookieName)
Expand Down
12 changes: 12 additions & 0 deletions backend/internal/email/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@
package email

import (
"errors"
"fmt"
"log"
"net/smtp"
"strings"
)

// errHeaderInjection is returned when a header field contains a newline, which
// could otherwise inject additional SMTP headers.
var errHeaderInjection = errors.New("email: header field contains a newline")

// Config holds the SMTP settings, all sourced from the environment.
type Config struct {
Host string
Expand Down Expand Up @@ -41,6 +47,12 @@ func (s *Sender) Send(to, subject, body string) (sent bool, err error) {
if from == "" {
from = s.cfg.User
}
// Guard against SMTP header injection: a CR/LF in any header field (notably
// the user-supplied recipient) would let an attacker inject extra headers.
if strings.ContainsAny(to, "\r\n") || strings.ContainsAny(subject, "\r\n") ||
strings.ContainsAny(from, "\r\n") {
return false, errHeaderInjection
}
msg := []byte(fmt.Sprintf(
"From: %s\r\nTo: %s\r\nSubject: %s\r\nMIME-Version: 1.0\r\n"+
"Content-Type: text/plain; charset=UTF-8\r\n\r\n%s\r\n",
Expand Down
3 changes: 3 additions & 0 deletions backend/internal/store/jsonstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,9 @@ func (s *JSONStore) InstallSeries(short string, sinceDays int) []InstallPoint {
if sinceDays <= 0 {
sinceDays = 90
}
if sinceDays > 366 {
sinceDays = 366 // cap so an untrusted ?days= can't force a huge allocation
}
now := time.Now().UTC()
out := make([]InstallPoint, 0, sinceDays)
days := s.doc.Installs[short]
Expand Down
3 changes: 3 additions & 0 deletions backend/internal/store/pebblestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,9 @@ func (s *PebbleStore) InstallSeries(short string, sinceDays int) []InstallPoint
if sinceDays <= 0 {
sinceDays = 90
}
if sinceDays > 366 {
sinceDays = 366 // cap so an untrusted ?days= can't force a huge allocation
}
now := time.Now().UTC()
out := make([]InstallPoint, 0, sinceDays)
days := s.doc.Installs[short]
Expand Down