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 b2738c3
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions nip11/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,23 @@ 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)

Expand Down

0 comments on commit b2738c3

Please sign in to comment.