runs: scope by account; app: the PKCE half of signing in
Three phones on one account now produce one history, which is the main reason to have accounts beyond upload permission. GET /v1/runs returns the account's runs and says how many devices contributed; fetching and deleting resolve a run id against the caller's own devices, so an id from another account is not found rather than fetched from wherever it happens to live. The rule that needed stating: the empty account is never a group. Devices nobody has signed in on are unrelated devices that share the absence of an owner, and matching on "" would let any anonymous device read every other one's runs. Tested, along with sibling-device access working and cross-account access not. App side: authorization code with PKCE. The app is a public client - anything compiled into an APK can be read out with unzip and strings - and the redirect returns through a custom URI scheme that any app on the device may register, so an intercepted code is a real risk. PKCE makes a stolen code worthless: it can only be exchanged by presenting a verifier that never left the process. A callback whose state does not match is refused before the code is spent and before any network call, since that is exactly how someone gets a victim to complete the attacker's sign-in. Nothing from the IdP is retained. The ID token is used once to prove who is signing in and then discarded; the device credential authenticates everything afterwards. No access tokens to store, no refresh tokens to rotate. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
parent
0eaba6150b
commit
4e6f2da3fb
@@ -755,11 +755,19 @@ func (s *Server) listRuns(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusUnauthorized, map[string]string{"error": "unknown credential"})
|
||||
return
|
||||
}
|
||||
list := s.Runs.List(dev.ID)
|
||||
list := s.Runs.ListFor(s.visibleDevices(dev))
|
||||
if list == nil {
|
||||
list = []runs.Meta{}
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{"runs": list})
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"runs": list,
|
||||
// Says whose history this is, so a client can show "3 devices" rather than leaving the
|
||||
// user to wonder why runs from another phone appeared.
|
||||
"scope": map[string]any{
|
||||
"account_id": dev.AccountID,
|
||||
"devices": len(s.visibleDevices(dev)),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) getRun(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -768,9 +776,14 @@ func (s *Server) getRun(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusUnauthorized, map[string]string{"error": "unknown credential"})
|
||||
return
|
||||
}
|
||||
// Scoped to the calling device's own directory: one device cannot read another's runs by
|
||||
// guessing a run id.
|
||||
b, err := s.Runs.Get(dev.ID, r.PathValue("id"))
|
||||
// Resolved against the caller's own devices only, so a run id from another account is not
|
||||
// found rather than being fetched from wherever it happens to live.
|
||||
owner, ok := s.Runs.OwnerOf(s.visibleDevices(dev), r.PathValue("id"))
|
||||
if !ok {
|
||||
writeJSON(w, http.StatusNotFound, map[string]string{"error": "no such run"})
|
||||
return
|
||||
}
|
||||
b, err := s.Runs.Get(owner, r.PathValue("id"))
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusNotFound, map[string]string{"error": "no such run"})
|
||||
return
|
||||
@@ -785,7 +798,12 @@ func (s *Server) deleteRun(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusUnauthorized, map[string]string{"error": "unknown credential"})
|
||||
return
|
||||
}
|
||||
if err := s.Runs.Delete(dev.ID, r.PathValue("id")); err != nil {
|
||||
owner, ok := s.Runs.OwnerOf(s.visibleDevices(dev), r.PathValue("id"))
|
||||
if !ok {
|
||||
w.WriteHeader(http.StatusNoContent) // delete is idempotent; absent is the desired state
|
||||
return
|
||||
}
|
||||
if err := s.Runs.Delete(owner, r.PathValue("id")); err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
@@ -928,3 +946,17 @@ func (s *Server) accountStatus(w http.ResponseWriter, r *http.Request) {
|
||||
"device_id": dev.ID,
|
||||
})
|
||||
}
|
||||
|
||||
// visibleDevices is the set of devices whose runs the caller may read.
|
||||
//
|
||||
// Signed in: every device on the same account, which is what an account is for. Not signed in:
|
||||
// only itself — anonymous devices are not a group, and treating the absent account as a shared
|
||||
// one would let any of them read all the others.
|
||||
func (s *Server) visibleDevices(dev *store.Device) []string {
|
||||
if dev.LinkedToAccount() {
|
||||
if ids := s.Store.DeviceIDsForAccount(dev.AccountID); len(ids) > 0 {
|
||||
return ids
|
||||
}
|
||||
}
|
||||
return []string{dev.ID}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user