Page MenuHomeMusing Studio

No OneTemporary

diff --git a/oauth_signup.go b/oauth_signup.go
index b5f881b..1608de7 100644
--- a/oauth_signup.go
+++ b/oauth_signup.go
@@ -1,232 +1,239 @@
/*
* Copyright © 2020-2021 Musing Studio LLC.
*
* This file is part of WriteFreely.
*
* WriteFreely is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, included
* in the LICENSE file in this source code package.
*/
package writefreely
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"html/template"
"net/http"
"strings"
"time"
"github.com/writeas/impart"
"github.com/writeas/web-core/auth"
"github.com/writeas/web-core/log"
+ "github.com/writefreely/writefreely/author"
"github.com/writefreely/writefreely/page"
)
type viewOauthSignupVars struct {
page.StaticPage
To string
Message template.HTML
Flashes []template.HTML
AccessToken string
TokenUsername string
TokenAlias string // TODO: rename this to match the data it represents: the collection title
TokenEmail string
TokenRemoteUser string
Provider string
ClientID string
TokenHash string
InviteCode string
LoginUsername string
Alias string // TODO: rename this to match the data it represents: the collection title
Email string
}
const (
oauthParamAccessToken = "access_token"
oauthParamTokenUsername = "token_username"
oauthParamTokenAlias = "token_alias"
oauthParamTokenEmail = "token_email"
oauthParamTokenRemoteUserID = "token_remote_user"
oauthParamClientID = "client_id"
oauthParamProvider = "provider"
oauthParamHash = "signature"
oauthParamUsername = "username"
oauthParamAlias = "alias"
oauthParamEmail = "email"
oauthParamPassword = "password"
oauthParamInviteCode = "invite_code"
)
type oauthSignupPageParams struct {
AccessToken string
TokenUsername string
TokenAlias string // TODO: rename this to match the data it represents: the collection title
TokenEmail string
TokenRemoteUser string
ClientID string
Provider string
TokenHash string
InviteCode string
}
func (p oauthSignupPageParams) HashTokenParams(key string) string {
hasher := sha256.New()
hasher.Write([]byte(key))
hasher.Write([]byte(p.AccessToken))
hasher.Write([]byte(p.TokenUsername))
hasher.Write([]byte(p.TokenAlias))
hasher.Write([]byte(p.TokenEmail))
hasher.Write([]byte(p.TokenRemoteUser))
hasher.Write([]byte(p.ClientID))
hasher.Write([]byte(p.Provider))
return hex.EncodeToString(hasher.Sum(nil))
}
func (h oauthHandler) viewOauthSignup(app *App, w http.ResponseWriter, r *http.Request) error {
tp := &oauthSignupPageParams{
AccessToken: r.FormValue(oauthParamAccessToken),
TokenUsername: r.FormValue(oauthParamTokenUsername),
TokenAlias: r.FormValue(oauthParamTokenAlias),
TokenEmail: r.FormValue(oauthParamTokenEmail),
TokenRemoteUser: r.FormValue(oauthParamTokenRemoteUserID),
ClientID: r.FormValue(oauthParamClientID),
Provider: r.FormValue(oauthParamProvider),
InviteCode: r.FormValue(oauthParamInviteCode),
}
if tp.HashTokenParams(h.Config.Server.HashSeed) != r.FormValue(oauthParamHash) {
return impart.HTTPError{Status: http.StatusBadRequest, Message: "Request has been tampered with."}
}
tp.TokenHash = tp.HashTokenParams(h.Config.Server.HashSeed)
if err := h.validateOauthSignup(r); err != nil {
return h.showOauthSignupPage(app, w, r, tp, err)
}
var err error
hashedPass := []byte{}
clearPass := r.FormValue(oauthParamPassword)
hasPass := clearPass != ""
if hasPass {
hashedPass, err = auth.HashPass([]byte(clearPass))
if err != nil {
return h.showOauthSignupPage(app, w, r, tp, fmt.Errorf("unable to hash password"))
}
}
newUser := &User{
- Username: r.FormValue(oauthParamUsername),
+ // Normalize the username into a valid slug, just like normal registration, so the collection alias is always
+ // lowercase
+ Username: getSlug(r.FormValue(oauthParamUsername), ""),
HashedPass: hashedPass,
HasPass: hasPass,
Email: prepareUserEmail(r.FormValue(oauthParamEmail), h.EmailKey),
Created: time.Now().Truncate(time.Second).UTC(),
}
displayName := r.FormValue(oauthParamAlias)
if len(displayName) == 0 {
displayName = r.FormValue(oauthParamUsername)
}
err = h.DB.CreateUser(h.Config, newUser, displayName, "")
if err != nil {
return h.showOauthSignupPage(app, w, r, tp, err)
}
// Log invite if needed
if tp.InviteCode != "" {
err = app.db.CreateInvitedUser(tp.InviteCode, newUser.ID)
if err != nil {
return err
}
}
err = h.DB.RecordRemoteUserID(r.Context(), newUser.ID, r.FormValue(oauthParamTokenRemoteUserID), r.FormValue(oauthParamProvider), r.FormValue(oauthParamClientID), r.FormValue(oauthParamAccessToken))
if err != nil {
return h.showOauthSignupPage(app, w, r, tp, err)
}
if err := loginOrFail(h.Store, w, r, newUser); err != nil {
return h.showOauthSignupPage(app, w, r, tp, err)
}
return nil
}
func (h oauthHandler) validateOauthSignup(r *http.Request) error {
- username := r.FormValue(oauthParamUsername)
+ // Validate the normalized username, since that's what we'll actually store as the user's name and collection alias
+ username := getSlug(r.FormValue(oauthParamUsername), "")
if len(username) < h.Config.App.MinUsernameLen {
return impart.HTTPError{Status: http.StatusBadRequest, Message: "Username is too short."}
}
if len(username) > 100 {
return impart.HTTPError{Status: http.StatusBadRequest, Message: "Username is too long."}
}
+ if !author.IsValidUsername(h.Config, username) {
+ return impart.HTTPError{Status: http.StatusPreconditionFailed, Message: "Username is reserved or isn't valid. It must be at least 3 characters long, and can only include letters, numbers, and hyphens."}
+ }
collTitle := r.FormValue(oauthParamAlias)
if len(collTitle) == 0 {
collTitle = username
}
email := r.FormValue(oauthParamEmail)
if len(email) > 0 {
parts := strings.Split(email, "@")
if len(parts) != 2 || (len(parts[0]) < 1 || len(parts[1]) < 1) {
return impart.HTTPError{Status: http.StatusBadRequest, Message: "Invalid email address"}
}
}
return nil
}
func (h oauthHandler) showOauthSignupPage(app *App, w http.ResponseWriter, r *http.Request, tp *oauthSignupPageParams, errMsg error) error {
username := tp.TokenUsername
collTitle := tp.TokenAlias
email := tp.TokenEmail
session, err := app.sessionStore.Get(r, cookieName)
if err != nil {
// Ignore this
log.Error("Unable to get session; ignoring: %v", err)
}
if tmpValue := r.FormValue(oauthParamUsername); len(tmpValue) > 0 {
username = tmpValue
}
if tmpValue := r.FormValue(oauthParamAlias); len(tmpValue) > 0 {
collTitle = tmpValue
}
if tmpValue := r.FormValue(oauthParamEmail); len(tmpValue) > 0 {
email = tmpValue
}
p := &viewOauthSignupVars{
StaticPage: pageForReq(app, r),
To: r.FormValue("to"),
Flashes: []template.HTML{},
AccessToken: tp.AccessToken,
TokenUsername: tp.TokenUsername,
TokenAlias: tp.TokenAlias,
TokenEmail: tp.TokenEmail,
TokenRemoteUser: tp.TokenRemoteUser,
Provider: tp.Provider,
ClientID: tp.ClientID,
TokenHash: tp.TokenHash,
InviteCode: tp.InviteCode,
LoginUsername: username,
Alias: collTitle,
Email: email,
}
// Display any error messages
flashes, _ := getSessionFlashes(app, w, r, session)
for _, flash := range flashes {
p.Flashes = append(p.Flashes, template.HTML(flash))
}
if errMsg != nil {
p.Flashes = append(p.Flashes, template.HTML(errMsg.Error()))
}
err = pages["signup-oauth.tmpl"].ExecuteTemplate(w, "base", p)
if err != nil {
log.Error("Unable to render signup-oauth: %v", err)
return err
}
return nil
}
diff --git a/signup_test.go b/signup_test.go
index 2ee9032..ab7d223 100644
--- a/signup_test.go
+++ b/signup_test.go
@@ -1,388 +1,440 @@
//go:build sqlite
/*
* Copyright © 2026 Musing Studio LLC.
*
* This file is part of WriteFreely.
*
* WriteFreely is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, included
* in the LICENSE file in this source code package.
*/
package writefreely
import (
"database/sql"
"net/http"
"net/http/httptest"
"net/url"
"path/filepath"
"strings"
"sync"
"testing"
"time"
"github.com/gorilla/mux"
"github.com/gorilla/schema"
"github.com/stretchr/testify/assert"
"github.com/writeas/impart"
"github.com/writefreely/writefreely/config"
"github.com/writefreely/writefreely/key"
)
var initTemplatesOnce sync.Once
// newSignupTestApp builds a real, sqlite-backed App suitable for exercising
// the HTTP signup handlers end-to-end. It's intentionally minimal: only the
// pieces those handlers actually touch (db, cfg, keys, session store, form
// decoder, and the global page/template caches) are initialized.
func newSignupTestApp(t *testing.T) *App {
t.Helper()
dbPath := filepath.Join(t.TempDir(), "writefreely.db")
db, err := sql.Open("sqlite3_with_regex", dbPath+"?parseTime=true&cached=shared")
if err != nil {
t.Fatalf("open test db: %v", err)
}
t.Cleanup(func() { db.Close() })
cfg := config.New()
cfg.UseSQLite(true)
cfg.Database.FileName = dbPath
cfg.App.Host = "http://localhost:0"
cfg.App.SingleUser = false
cfg.App.OpenRegistration = false
cfg.App.MinUsernameLen = 3
cfg.Server.HashSeed = "test-hash-seed"
keys := &key.Keychain{}
if err := keys.GenerateKeys(); err != nil {
t.Fatalf("generate keys: %v", err)
}
app := &App{
db: &datastore{DB: db, driverName: driverSQLite},
cfg: cfg,
keys: keys,
}
app.formDecoder = schema.NewDecoder()
app.InitSession()
if err := adminInitDatabase(app); err != nil {
t.Fatalf("init schema: %v", err)
}
var tmplErr error
initTemplatesOnce.Do(func() {
tmplErr = InitTemplates(cfg)
})
if tmplErr != nil {
t.Fatalf("init templates: %v", tmplErr)
}
return app
}
// seedInvite inserts a directly-usable invite code owned by ownerID, with no
// expiration or use limit.
func seedInvite(t *testing.T, app *App, code string, ownerID int64) {
t.Helper()
_, err := app.db.Exec("INSERT INTO userinvites (id, owner_id, max_uses, created, expires, inactive) VALUES (?, ?, NULL, ?, NULL, 0)",
code, ownerID, time.Now().UTC().Format("2006-01-02 15:04:05"))
if err != nil {
t.Fatalf("seed invite: %v", err)
}
}
func userExists(t *testing.T, app *App, username string) bool {
t.Helper()
var id int64
err := app.db.QueryRow("SELECT id FROM users WHERE username = ?", username).Scan(&id)
if err == sql.ErrNoRows {
return false
}
if err != nil {
t.Fatalf("query user: %v", err)
}
return true
}
// --- Regression coverage for #1722: POST /auth/signup (handleWebSignup) ---
// No test previously covered this path even though the fix landed there.
func TestWebSignupClosedRegistration(t *testing.T) {
cases := []struct {
name string
invite string
wantStatus int
wantUser bool
}{
{"no invite code", "", http.StatusForbidden, false},
{"bogus invite code", "not-a-real-code", http.StatusForbidden, false},
{"valid invite code", "webvalid1", http.StatusFound, true},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
app := newSignupTestApp(t)
app.cfg.App.OpenRegistration = false
if tc.invite == "webvalid1" {
seedInvite(t, app, "webvalid1", 0)
}
username := "websignup-" + strings.ReplaceAll(tc.name, " ", "-")
form := url.Values{}
form.Set("alias", username)
form.Set("pass", "sup3rSecret!")
form.Set("invite_code", tc.invite)
req := httptest.NewRequest("POST", "/auth/signup", strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
w := httptest.NewRecorder()
err := handleWebSignup(app, w, req)
// handleWebSignup always returns an impart.HTTPError: a 302 to
// "to" on success, or the rejection status on failure.
httpErr, ok := err.(impart.HTTPError)
if !ok {
t.Fatalf("expected impart.HTTPError, got %T (%v)", err, err)
}
if httpErr.Status != tc.wantStatus {
t.Errorf("status = %d, want %d", httpErr.Status, tc.wantStatus)
}
assert.Equal(t, tc.wantUser, userExists(t, app, username),
"unexpected account-creation outcome for case %q", tc.name)
})
}
}
// --- Finding A: POST /api/auth/signup (apiSignup -> signup) ---
func TestAPISignupClosedRegistration(t *testing.T) {
cases := []struct {
name string
invite string
wantStatus int
wantUser bool
}{
{"no invite code", "", http.StatusForbidden, false},
{"bogus invite code", "not-a-real-code", http.StatusForbidden, false},
{"valid invite code", "valid1", http.StatusFound /* signupWithRegistration succeeds */, true},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
app := newSignupTestApp(t)
app.cfg.App.OpenRegistration = false
if tc.invite == "valid1" {
seedInvite(t, app, "valid1", 0)
}
username := "apisignup-" + strings.ReplaceAll(tc.name, " ", "-")
form := url.Values{}
form.Set("alias", username)
form.Set("pass", "sup3rSecret!")
form.Set("invite_code", tc.invite)
req := httptest.NewRequest("POST", "/api/auth/signup", strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
w := httptest.NewRecorder()
err := apiSignup(app, w, req)
if !tc.wantUser {
httpErr, ok := err.(impart.HTTPError)
if !ok {
t.Fatalf("expected impart.HTTPError, got %T (%v)", err, err)
}
if httpErr.Status != tc.wantStatus {
t.Errorf("status = %d, want %d", httpErr.Status, tc.wantStatus)
}
} else if err != nil {
t.Fatalf("expected signup to succeed, got error: %v", err)
}
assert.Equal(t, tc.wantUser, userExists(t, app, username),
"unexpected account-creation outcome for case %q", tc.name)
})
}
}
func TestAPISignupOpenRegistrationStillWorks(t *testing.T) {
app := newSignupTestApp(t)
app.cfg.App.OpenRegistration = true
form := url.Values{}
form.Set("alias", "apisignup-open")
form.Set("pass", "sup3rSecret!")
req := httptest.NewRequest("POST", "/api/auth/signup", strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
w := httptest.NewRecorder()
if err := apiSignup(app, w, req); err != nil {
t.Fatalf("expected signup to succeed with open registration, got: %v", err)
}
assert.True(t, userExists(t, app, "apisignup-open"))
}
// TestInitRoutesAlwaysRegistersAPISignup guards against the routing-time
// bypass in Finding A: /api/auth/signup used to only be registered when
// OpenRegistration was true *at boot*, so toggling the setting off at
// runtime (via the admin panel) left the route live with no invite check
// at all. It must now always resolve, regardless of the setting.
func TestInitRoutesAlwaysRegistersAPISignup(t *testing.T) {
app := newSignupTestApp(t)
app.cfg.App.OpenRegistration = false
router := InitRoutes(app, mux.NewRouter())
req := httptest.NewRequest("POST", "/api/auth/signup", nil)
var match mux.RouteMatch
if !router.Match(req, &match) {
t.Fatal("expected /api/auth/signup to match a route even when OpenRegistration is false")
}
}
// --- Finding B: POST /oauth/signup (viewOauthSignup) ---
func newTestOauthHandler(app *App) oauthHandler {
return oauthHandler{
Config: app.cfg,
DB: app.db,
Store: app.sessionStore,
EmailKey: app.keys.EmailKey,
}
}
func signOauthParams(hashSeed string, p oauthSignupPageParams) string {
return p.HashTokenParams(hashSeed)
}
func TestOAuthSignupClosedRegistration(t *testing.T) {
cases := []struct {
name string
invite string
wantUser bool
}{
{"no invite code", "", false},
{"bogus invite code", "not-a-real-code", false},
{"valid invite code", "oauthok", true},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
app := newSignupTestApp(t)
app.cfg.App.OpenRegistration = false
h := newTestOauthHandler(app)
if tc.invite == "oauthok" {
seedInvite(t, app, "oauthok", 0)
}
username := "oauthsignup-" + strings.ReplaceAll(tc.name, " ", "-")
tp := oauthSignupPageParams{
AccessToken: "tok-" + username,
TokenUsername: username,
TokenAlias: username,
TokenEmail: "",
TokenRemoteUser: "remote-" + username,
ClientID: "client1",
Provider: "generic",
InviteCode: tc.invite,
}
sig := signOauthParams(app.cfg.Server.HashSeed, tp)
form := url.Values{}
form.Set(oauthParamAccessToken, tp.AccessToken)
form.Set(oauthParamTokenUsername, tp.TokenUsername)
form.Set(oauthParamTokenAlias, tp.TokenAlias)
form.Set(oauthParamTokenEmail, tp.TokenEmail)
form.Set(oauthParamTokenRemoteUserID, tp.TokenRemoteUser)
form.Set(oauthParamClientID, tp.ClientID)
form.Set(oauthParamProvider, tp.Provider)
form.Set(oauthParamInviteCode, tp.InviteCode)
form.Set(oauthParamHash, sig)
form.Set(oauthParamUsername, username)
req := httptest.NewRequest("POST", "/oauth/signup", strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
w := httptest.NewRecorder()
err := h.viewOauthSignup(app, w, req)
// On rejection, viewOauthSignup re-renders the signup-oauth page
// inline with a flash message rather than returning an
// impart.HTTPError (mirroring showOauthSignupPage's contract), so
// the account-creation outcome in the DB is the real signal here.
if !tc.wantUser {
if err != nil {
t.Fatalf("expected inline re-render (nil error), got: %v", err)
}
if !strings.Contains(w.Body.String(), "Registration is closed") {
t.Errorf("expected rejection message in response body, got: %s", w.Body.String())
}
} else if err != nil {
t.Fatalf("expected oauth signup to succeed, got error: %v", err)
}
assert.Equal(t, tc.wantUser, userExists(t, app, username),
"unexpected account-creation outcome for case %q", tc.name)
})
}
}
// TestOAuthSignupCannotSwapInviteCodeWithoutInvalidatingSignature proves the
// replay bypass from Finding B is closed: a signature minted for one
// invite_code (or none) can no longer be reused with a different invite_code
// value, since InviteCode is now part of the signed payload.
func TestOAuthSignupCannotSwapInviteCodeWithoutInvalidatingSignature(t *testing.T) {
app := newSignupTestApp(t)
app.cfg.App.OpenRegistration = true // signature minted while registration was open
h := newTestOauthHandler(app)
username := "oauthsignup-swap"
tp := oauthSignupPageParams{
AccessToken: "tok-swap",
TokenUsername: username,
TokenAlias: username,
TokenRemoteUser: "remote-swap",
ClientID: "client1",
Provider: "generic",
InviteCode: "", // signed with an empty invite code
}
sig := signOauthParams(app.cfg.Server.HashSeed, tp)
// Registration is closed by the time the (replayed) form is submitted.
app.cfg.App.OpenRegistration = false
form := url.Values{}
form.Set(oauthParamAccessToken, tp.AccessToken)
form.Set(oauthParamTokenUsername, tp.TokenUsername)
form.Set(oauthParamTokenAlias, tp.TokenAlias)
form.Set(oauthParamTokenRemoteUserID, tp.TokenRemoteUser)
form.Set(oauthParamClientID, tp.ClientID)
form.Set(oauthParamProvider, tp.Provider)
form.Set(oauthParamInviteCode, "swapped-in-bogus-code") // tampered after signing
form.Set(oauthParamHash, sig)
form.Set(oauthParamUsername, username)
req := httptest.NewRequest("POST", "/oauth/signup", strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
w := httptest.NewRecorder()
err := h.viewOauthSignup(app, w, req)
httpErr, ok := err.(impart.HTTPError)
if !ok {
t.Fatalf("expected impart.HTTPError, got %T (%v)", err, err)
}
if httpErr.Status != http.StatusBadRequest {
t.Errorf("status = %d, want %d (tampered request)", httpErr.Status, http.StatusBadRequest)
}
assert.False(t, userExists(t, app, username))
}
+
+// TestOAuthSignupNormalizesUsername is regression coverage for #648 and #844:
+// usernames created via OAuth must be run through the same slug normalization as
+// normal registration, so the resulting collection alias is always lowercase.
+// Otherwise the blog 404s, since collection URLs are redirected to their
+// lowercase form on request but no matching (lowercase) alias exists.
+func TestOAuthSignupNormalizesUsername(t *testing.T) {
+ app := newSignupTestApp(t)
+ app.cfg.App.OpenRegistration = true
+ h := newTestOauthHandler(app)
+
+ const submitted = "MixedCaseUser"
+ const normalized = "mixedcaseuser"
+
+ tp := oauthSignupPageParams{
+ AccessToken: "tok-mixed",
+ TokenUsername: submitted,
+ TokenAlias: submitted,
+ TokenRemoteUser: "remote-mixed",
+ ClientID: "client1",
+ Provider: "generic",
+ }
+ sig := signOauthParams(app.cfg.Server.HashSeed, tp)
+
+ form := url.Values{}
+ form.Set(oauthParamAccessToken, tp.AccessToken)
+ form.Set(oauthParamTokenUsername, tp.TokenUsername)
+ form.Set(oauthParamTokenAlias, tp.TokenAlias)
+ form.Set(oauthParamTokenRemoteUserID, tp.TokenRemoteUser)
+ form.Set(oauthParamClientID, tp.ClientID)
+ form.Set(oauthParamProvider, tp.Provider)
+ form.Set(oauthParamHash, sig)
+ form.Set(oauthParamUsername, submitted)
+
+ req := httptest.NewRequest("POST", "/oauth/signup", strings.NewReader(form.Encode()))
+ req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
+ w := httptest.NewRecorder()
+
+ if err := h.viewOauthSignup(app, w, req); err != nil {
+ t.Fatalf("expected oauth signup to succeed, got error: %v", err)
+ }
+
+ assert.True(t, userExists(t, app, normalized), "user should be stored with a lowercased username")
+ assert.False(t, userExists(t, app, submitted), "user should not be stored with the raw mixed-case username")
+
+ var alias string
+ err := app.db.QueryRow("SELECT alias FROM collections WHERE alias = ?", normalized).Scan(&alias)
+ if err != nil {
+ t.Fatalf("expected collection with lowercased alias %q: %v", normalized, err)
+ }
+ assert.Equal(t, normalized, alias, "collection alias must be lowercase to be reachable")
+}

File Metadata

Mime Type
text/x-diff
Expires
Mon, Nov 30, 6:31 PM (14 h, 23 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3830271

Event Timeline