Skip to content

Commit d1ad494

Browse files
committed
associate capabilities discovered through incoming requests with gateway, not DID
1 parent 4e94fe8 commit d1ad494

3 files changed

Lines changed: 35 additions & 24 deletions

File tree

ap/id.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ var (
2626
// KeyRegex matches a base58-encoded Ed25519 public key.
2727
KeyRegex = regexp.MustCompile(`\b(z6Mk[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]+)\b`)
2828

29-
// didKeyRegex matches a portable object ID, without the ap:// prefix.
30-
didKeyRegex = regexp.MustCompile(`^did:key:(z6Mk[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]+)(?:[\/#?].*){0,1}`)
31-
3229
// apURLRegex matches an ap:// URL.
3330
apURLRegex = regexp.MustCompile(`^ap:\/\/did:key:(z6Mk[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]+)((?:[\/#?].*){0,1})`)
3431

@@ -67,23 +64,26 @@ func Gateway(gw, id string) string {
6764
return id
6865
}
6966

70-
// Origin returns the origin of an ActivityPub ID.
71-
func Origin(id string) (string, error) {
67+
// Origins returns the origin and the host of an ActivityPub ID.
68+
func Origins(id string) (string, string, error) {
69+
u, err := url.Parse(id)
70+
if err != nil {
71+
return "", "", err
72+
}
73+
7274
if m := apURLRegex.FindStringSubmatch(id); m != nil {
73-
return "did:key:" + m[1], nil
75+
return "did:key:" + m[1], u.Host, nil
7476
}
7577

7678
if m := GatewayURLRegex.FindStringSubmatch(id); m != nil {
77-
return "did:key:" + m[1], nil
79+
return "did:key:" + m[1], u.Host, nil
7880
}
7981

80-
if m := didKeyRegex.FindStringSubmatch(id); m != nil {
81-
return "did:key:" + m[1], nil
82-
}
82+
return u.Host, u.Host, nil
83+
}
8384

84-
if u, err := url.Parse(id); err != nil {
85-
return "", err
86-
} else {
87-
return u.Host, nil
88-
}
85+
// Origin returns the origin of an ActivityPub ID.
86+
func Origin(id string) (string, error) {
87+
origin, _, err := Origins(id)
88+
return origin, err
8989
}

fed/inbox.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,26 +43,26 @@ var unsupportedActivityTypes = map[ap.ActivityType]struct{}{
4343
ap.Move: {},
4444
}
4545

46-
func (l *Listener) getActivityOrigin(activity *ap.Activity, sender *ap.Actor) (string, string, error) {
46+
func (l *Listener) getActivityOrigin(activity *ap.Activity, sender *ap.Actor) (string, string, string, error) {
4747
if activity.ID == "" {
48-
return "", "", errors.New("unspecified activity ID")
48+
return "", "", "", errors.New("unspecified activity ID")
4949
}
5050

5151
activityOrigin, err := ap.Origin(activity.ID)
5252
if err != nil {
53-
return "", "", err
53+
return "", "", "", err
5454
}
5555

5656
if sender.ID == "" {
57-
return "", "", errors.New("unspecified sender ID")
57+
return "", "", "", errors.New("unspecified sender ID")
5858
}
5959

60-
senderOrigin, err := ap.Origin(sender.ID)
60+
senderOrigin, senderHost, err := ap.Origins(sender.ID)
6161
if err != nil {
62-
return "", "", err
62+
return "", "", "", err
6363
}
6464

65-
return activityOrigin, senderOrigin, nil
65+
return activityOrigin, senderOrigin, senderHost, nil
6666
}
6767

6868
func (l *Listener) validateActivity(activity *ap.Activity, origin string, depth uint) error {
@@ -434,7 +434,7 @@ func (l *Listener) doHandleInbox(w http.ResponseWriter, r *http.Request, keys [2
434434
if an activity wasn't sent by an actor on the same server, we must fetch the activity from its origin instead
435435
of trusting the sender to pass it as-is
436436
*/
437-
origin, senderOrigin, err := l.getActivityOrigin(queued, sender)
437+
origin, senderOrigin, senderHost, err := l.getActivityOrigin(queued, sender)
438438
if err != nil {
439439
slog.Warn("Failed to determine whether or not activity is forwarded", "activity", &activity, "sender", sender.ID, "error", err)
440440
w.WriteHeader(http.StatusInternalServerError)
@@ -596,7 +596,7 @@ func (l *Listener) doHandleInbox(w http.ResponseWriter, r *http.Request, keys [2
596596
if _, err = l.DB.ExecContext(
597597
r.Context(),
598598
`INSERT INTO servers (host, capabilities) VALUES ($1, $2) ON CONFLICT(host) DO UPDATE SET capabilities = capabilities | $2, updated = UNIXEPOCH()`,
599-
senderOrigin,
599+
senderHost,
600600
capabilities,
601601
); err != nil {
602602
slog.Error("Failed to record server capabilities", "server", senderOrigin, "error", err)

migrations/050_didhost.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package migrations
2+
3+
import (
4+
"context"
5+
"database/sql"
6+
)
7+
8+
func didhost(ctx context.Context, domain string, tx *sql.Tx) error {
9+
_, err := tx.ExecContext(ctx, `DELETE FROM servers WHERE host LIKE 'did:key:%'`)
10+
return err
11+
}

0 commit comments

Comments
 (0)