Skip to content

Commit

Permalink
nip11: catch invalid/small urls.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Feb 25, 2025
1 parent c98b2ea commit 7e06354
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions nip11/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,25 @@ import (
// relay or if it doesn't have a NIP-11 handler -- although in that case it will also return
// an error.
func Fetch(ctx context.Context, u string) (info RelayInformationDocument, err error) {
if _, ok := ctx.Deadline(); !ok {
// if no timeout is set, force it to 7 seconds
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, 7*time.Second)
defer cancel()
}

// normalize URL to start with http://, https:// or without protocol
u = nostr.NormalizeURL(u)
if len(u) < 8 {
return info, fmt.Errorf("invalid url %s", u)
}

info = RelayInformationDocument{
URL: u,
}

if _, ok := ctx.Deadline(); !ok {
// if no timeout is set, force it to 7 seconds
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, 7*time.Second)
defer cancel()
}

// make request
req, err := http.NewRequestWithContext(ctx, "GET", "http"+u[2:], nil)
req, _ := http.NewRequestWithContext(ctx, "GET", "http"+u[2:], nil)

// add the NIP-11 header
req.Header.Add("Accept", "application/nostr+json")
Expand Down

0 comments on commit 7e06354

Please sign in to comment.