| 1 | package tinyclient |
| 2 | |
| 3 | import ( |
| 4 | "encoding/binary" |
| 5 | "encoding/hex" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestNostrTargetPathDecodesNIP19Links(t *testing.T) { |
| 10 | pubkey := "5ac640e5df8f7945381c31f435288ba3f587fbd68efb27abaf941792f9c36369" |
| 11 | raw, _ := hex.DecodeString(pubkey) |
| 12 | id := "0f8fc45a6e3cd2d022cf68f3ea0f413665a6d92c3d783e3ab6c2bc324432b5af" |
| 13 | idRaw, _ := hex.DecodeString(id) |
| 14 | kind := make([]byte, 4) |
| 15 | binary.BigEndian.PutUint32(kind, 30023) |
| 16 | naddr := append(append(append([]byte{0, 5}, []byte("hello" )...), append([]byte{2, 32}, raw...)...), append([]byte{3, 4}, kind...)...) |
| 17 | cases := map[string]string{ |
| 18 | "web+nostr:npub1ttrypewl3au52wqux86r22yt506c077k3maj02a0jste97wrvd5sjfutc2" : "/search?author=" + pubkey, |
| 19 | "nostr:" + bech32("note" , idRaw): "/e/" + id, |
| 20 | bech32("nevent" , append([]byte{0, 32}, idRaw...)): "/e/" + id, |
| 21 | "web+nostr://" + bech32("nprofile" , append([]byte{0, 32}, raw...)): "/search?author=" + pubkey, |
| 22 | bech32("naddr" , naddr): "/a/30023:" + pubkey + ":hello" , |
| 23 | id: "/e/" + id, |
| 24 | } |
| 25 | for target, want := range cases { |
| 26 | if got, ok := nostrTargetPath(target); !ok || got != want { |
| 27 | t.Fatalf("%s: got %q ok=%v, want %q" , target, got, ok, want) |
| 28 | } |
| 29 | } |
| 30 | for _, bad := range []string{"" , "web+nostr:npub1ttrypewl3au52wqux86r22yt506c077k3maj02a0jste97wrvd5sjfutc3" , "nostr:javascript:alert(1)" , "nsec1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqs4g4gaj" } { |
| 31 | if _, ok := nostrTargetPath(bad); ok { |
| 32 | t.Fatalf("%q accepted" , bad) |
| 33 | } |
| 34 | } |
| 35 | } |