Keep showing a guest link's URL so it can be copied again
The URL was shown once, in a box under the create button, and then gone: only a hash of the token was stored, so the app genuinely could not produce it a second time. Lose the message you sent the sitter and the only way back was to mint a new link — which strands whoever is already holding the old one. Settings now lists every live link with its URL and a Copy button, so re-sending one is just copying it again. That means keeping the token rather than only its hash, and it is worth being plain about the trade. It is not the trade you would make for a password, which the user has probably reused, or a session token, which grants everything indefinitely. A guest link grants a strict subset of what the same database already holds in plaintext, expires on a date the owner picked, and can be revoked in one tap — so an attacker who can read puppy.db gains very little by also being able to open it as a guest. The lookup column stays a hash and remains the key redeem matches against; the secret sits in a new column beside it, which also keeps the migration additive. Links created before this have an empty secret. They keep working and stay revocable — the migration touches nothing but the new column — and the list says why their URL is missing rather than rendering a broken one. The two tests that asserted the old contract now assert the new one: a listing hands back a secret that really opens the link, and the lookup column is still a hash. Added one for the legacy row, since "still works, just cannot be shown" is the part a future change is most likely to break quietly.
This commit is contained in:
+56
-8
@@ -230,13 +230,36 @@ func TestListSharesHidesRevokedAndExpired(t *testing.T) {
|
||||
if len(links) != 1 || links[0].ID != live.ID {
|
||||
t.Fatalf("listed %d link(s), want only the live one", len(links))
|
||||
}
|
||||
if links[0].Token != "" {
|
||||
t.Error("listing leaked a raw token")
|
||||
}
|
||||
|
||||
// Settings shows every live link's URL so it can be re-sent, which means a
|
||||
// listing has to carry the same secret the link was created with — and that
|
||||
// secret has to still work.
|
||||
func TestListSharesReturnsAWorkingURL(t *testing.T) {
|
||||
a := testAuth(t)
|
||||
ownerID := testOwner(t, a)
|
||||
link, err := a.createShare(ownerID, "Anna", hoursAhead(24))
|
||||
if err != nil {
|
||||
t.Fatalf("create share: %v", err)
|
||||
}
|
||||
links, err := a.listShares(ownerID)
|
||||
if err != nil {
|
||||
t.Fatalf("list: %v", err)
|
||||
}
|
||||
if len(links) != 1 {
|
||||
t.Fatalf("listed %d link(s), want 1", len(links))
|
||||
}
|
||||
if links[0].Token != link.Token {
|
||||
t.Fatalf("listing returned %q, want the issued secret %q", links[0].Token, link.Token)
|
||||
}
|
||||
if _, _, ok := a.redeemShare(links[0].Token); !ok {
|
||||
t.Error("the secret handed back by the listing does not open the link")
|
||||
}
|
||||
}
|
||||
|
||||
// The raw token is only ever handed back once, at creation.
|
||||
func TestOnlyTheTokenHashIsStored(t *testing.T) {
|
||||
// The lookup column stays a hash even though the secret is kept beside it, so
|
||||
// the token in a URL is never what is matched against directly.
|
||||
func TestLookupIsStillByHash(t *testing.T) {
|
||||
a := testAuth(t)
|
||||
ownerID := testOwner(t, a)
|
||||
link, err := a.createShare(ownerID, "Anna", hoursAhead(24))
|
||||
@@ -247,11 +270,36 @@ func TestOnlyTheTokenHashIsStored(t *testing.T) {
|
||||
if err := a.db.QueryRow(`SELECT token FROM share_links WHERE id = ?`, link.ID).Scan(&stored); err != nil {
|
||||
t.Fatalf("read back: %v", err)
|
||||
}
|
||||
if stored == link.Token {
|
||||
t.Error("the raw token is stored in the clear")
|
||||
}
|
||||
if stored != hashToken(link.Token) {
|
||||
t.Error("stored token is not the hash of the issued one")
|
||||
t.Error("the lookup column is not the hash of the issued token")
|
||||
}
|
||||
}
|
||||
|
||||
// A link made before secrets were kept has no URL to show. It must still work
|
||||
// and still be revocable — only the copy-again affordance is unavailable.
|
||||
func TestLinkWithoutAStoredSecretStillWorks(t *testing.T) {
|
||||
a := testAuth(t)
|
||||
ownerID := testOwner(t, a)
|
||||
link, err := a.createShare(ownerID, "Legacy", hoursAhead(24))
|
||||
if err != nil {
|
||||
t.Fatalf("create share: %v", err)
|
||||
}
|
||||
// What the migration leaves behind for a pre-existing row.
|
||||
if _, err := a.db.Exec(`UPDATE share_links SET secret = '' WHERE id = ?`, link.ID); err != nil {
|
||||
t.Fatalf("clear secret: %v", err)
|
||||
}
|
||||
links, err := a.listShares(ownerID)
|
||||
if err != nil {
|
||||
t.Fatalf("list: %v", err)
|
||||
}
|
||||
if len(links) != 1 || links[0].Token != "" {
|
||||
t.Fatalf("want the link listed with an empty token, got %+v", links)
|
||||
}
|
||||
if _, _, ok := a.redeemShare(link.Token); !ok {
|
||||
t.Error("a link whose secret was never stored stopped working")
|
||||
}
|
||||
if err := a.revokeShare(ownerID, link.ID); err != nil {
|
||||
t.Errorf("could not revoke it: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user