Piped login: tolerate duplicate nonce query param (array values), idempotent replay of the just-issued login

This commit is contained in:
spaghet committed 2026-11-23 22:36:07 -05:00
1 parent a79e820f2d
commit da85f182bb
3 files changed
+46 -6

No files matched your search

@@ -145,6 +145,34 @@ namespace Http
m.loginNonceSlot = (m.loginNonceSlot + 1) mod 4
end function
' The gate script appends token+nonce to the cb URL while the cb
' template already carries a nonce, so the callback can arrive with
' the key twice; Playlet's query parser turns duplicate keys into an
' ARRAY and ValidString(array) is "" -> the nonce looked unknown.
function FirstStr(v as dynamic) as string
if GetInterface(v, "ifArray") <> invalid
if v.Count() > 0
return ValidString(v[0])
end if
return ""
end if
return ValidString(v)
end function
function ReadLastSession() as string
reg = CreateObject("roRegistrySection", "piped_login")
if reg.Exists("current")
return reg.Read("current")
end if
return ""
end function
function WriteLastSession(sessionUuid as string) as void
reg = CreateObject("roRegistrySection", "piped_login")
reg.Write("current", sessionUuid)
reg.Flush()
end function
function SetLoggedInProfile(username as string, sessionUuid as string) as void
instance = m.GetInstance()
port = Str(m.server.task.port).Replace(" ", "")
@@ -201,7 +229,7 @@ namespace Http
request = context.request
response = context.response
reqNonce = ValidString(request.query.nonce)
reqNonce = m.FirstStr(request.query.nonce)
if reqNonce <> ""
' The phone clicked the link the login page handed out; keep
' that nonce valid for the callback this redirect starts,
@@ -248,7 +276,7 @@ namespace Http
fields = m.ParseFormBody(ValidString(request.BodyText()))
username = ValidString(fields.username)
password = ValidString(fields.password)
nonce = ValidString(fields.nonce)
nonce = m.FirstStr(fields.nonce)
if nonce <> "" and m.KnownNonceIdx(nonce) < 0
response.Default(400, m.ErrorPage(`Stale login page. <a href="/piped/login">Start again</a>`))
@@ -313,8 +341,8 @@ namespace Http
request = context.request
response = context.response
token = ValidString(request.query.token)
nonce = ValidString(request.query.nonce)
token = m.FirstStr(request.query.token)
nonce = m.FirstStr(request.query.nonce)
if StringUtils.IsNullOrEmpty(token)
response.Default(400, m.ErrorPage("Missing token."))
@@ -324,6 +352,17 @@ namespace Http
' rolling window above). Consume it so each link is single-use.
idx = m.KnownNonceIdx(nonce)
if idx < 0
' A consumed nonce arriving with the SAME session we just
' installed is a retry/replay of a login that already
' succeeded (the gate script can bounce the phone twice) -
' show success, not an error the user cannot act on.
if nonce <> "" and token = m.ReadLastSession()
response.http_code = 200
response.headers["Cache-Control"] = "no-store, max-age=0"
response.ContentType("text/html")
response.SetBodyDataString(m.PageShell("Signed in", m.SuccessBody(m.LookupUsername(token))))
return true
end if
response.Default(403, m.ErrorPage(`Login link expired or already used. <a href="/piped/login">Start again</a>`))
return true
end if
@@ -335,6 +374,7 @@ namespace Http
username = m.LookupUsername(token)
m.SetLoggedInProfile(username, token)
m.WriteLastSession(token)
response.http_code = 200
response.headers["Cache-Control"] = "no-store, max-age=0"