Skip to content

Commit

Permalink
fix(core): json escaping of some characters
Browse files Browse the repository at this point in the history
  • Loading branch information
bezysoftware committed Oct 5, 2024
1 parent 44020e3 commit 9caff83
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text.Json;

namespace Netstr.Extensions
namespace Netstr.Json
{
public static class JsonExtensions
{
Expand Down
29 changes: 29 additions & 0 deletions src/Netstr/Json/NostrJsonEncoder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Text.Encodings.Web;

namespace Netstr.Json
{
/// <summary>
/// Json encoder for nostr events which follows NIP-01's character escaping rules.
/// </summary>
public class NostrJsonEncoder : JavaScriptEncoder
{
private static int[] EscapableCharacters = [0x0A, 0x22, 0x5C, 0x0D, 0x09, 0x08, 0x0C];

public override int MaxOutputCharactersPerInputCharacter => JavaScriptEncoder.Default.MaxOutputCharactersPerInputCharacter;

public override unsafe int FindFirstCharacterToEncode(char* text, int textLength)
{
return JavaScriptEncoder.UnsafeRelaxedJsonEscaping.FindFirstCharacterToEncode(text, textLength);
}

public override unsafe bool TryEncodeUnicodeScalar(int unicodeScalar, char* buffer, int bufferLength, out int numberOfCharactersWritten)
{
return JavaScriptEncoder.UnsafeRelaxedJsonEscaping.TryEncodeUnicodeScalar(unicodeScalar, buffer, bufferLength, out numberOfCharactersWritten);
}

public override bool WillEncode(int unicodeScalar)
{
return EscapableCharacters.Contains(unicodeScalar);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Text.Json.Serialization;
using System.Text.Json;

namespace Netstr.Converters
namespace Netstr.Json
{
/// <summary>
/// Converts Unix time to DateTimeOffset.
Expand Down
2 changes: 1 addition & 1 deletion src/Netstr/Messaging/Events/EventParser.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Netstr.Extensions;
using Netstr.Json;
using Netstr.Messaging.Models;
using System.Text.Json;

Expand Down
5 changes: 3 additions & 2 deletions src/Netstr/Messaging/Events/Validators/EventHashValidator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Netstr.Messaging.Models;
using Netstr.Json;
using Netstr.Messaging.Models;
using System.Security.Cryptography;
using System.Text.Encodings.Web;
using System.Text.Json;
Expand All @@ -12,7 +13,7 @@ public class EventHashValidator : IEventValidator
{
private static JsonSerializerOptions serializerOptions = new JsonSerializerOptions
{
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
Encoder = new NostrJsonEncoder()
};

public string? Validate(Event e, ClientContext context)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.Extensions.Options;
using Netstr.Data;
using Netstr.Extensions;
using Netstr.Json;
using Netstr.Messaging.Models;
using Netstr.Messaging.Subscriptions;
using Netstr.Messaging.Subscriptions.Validators;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Netstr.Extensions;
using Netstr.Json;
using Netstr.Messaging.Models;
using System.Text.Json;

Expand Down
2 changes: 1 addition & 1 deletion src/Netstr/Messaging/Models/Event.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Microsoft.EntityFrameworkCore.Diagnostics;
using Netstr.Converters;
using Netstr.Json;
using System.Numerics;
using System.Text.Json.Serialization;

Expand Down
2 changes: 1 addition & 1 deletion src/Netstr/Messaging/Models/SubscriptionFilter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Netstr.Converters;
using Netstr.Json;
using System.Text.Json;
using System.Text.Json.Serialization;

Expand Down
1 change: 1 addition & 0 deletions src/Netstr/Netstr.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<DockerfileFile>..\..\Dockerfile</DockerfileFile>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
Expand Down
16 changes: 8 additions & 8 deletions test/Netstr.Tests/NIPs/01.feature
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Feature: NIP-01
Feature: NIP-01
Defines the basic protocol that should be implemented by everybody.

Background:
Expand All @@ -20,21 +20,21 @@ Scenario: Invalid messages are discarded, valid ones accepted
| Kinds |
| 1 |
And Bob publishes events
| Id | Content | Kind | CreatedAt | Signature | Tags |
| ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff | Hello 1 | 1 | 1722337838 | | |
| a6d166e834e78827af0770f31f15b13a772f281ad880f43ce12c24d4e3d0e346 | Hello 1 | 1 | 1722337838 | Invalid | |
| 9a6b4cefcd17f3bf7fb03c02da044c628836a118c47d5b92503c1d2bdb796296 | Hi ' \" \b \t \r \n | 1 | 1722337838 | | |
| 50ed63c449df67d89e9964a27a26abbf214ca155b03915067a5a0f75618802bb | Hello | 1 | 1722337838 | | [[]] |
| Id | Content | Kind | CreatedAt | Signature | Tags |
| ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff | Hello 1 | 1 | 1722337838 | | |
| a6d166e834e78827af0770f31f15b13a772f281ad880f43ce12c24d4e3d0e346 | Hello 1 | 1 | 1722337838 | Invalid | |
| bb5d2fe5b2c16c676d87ef446fa38581b9fa45e2e50ba89568664abf4e1d1396 | Hi ' \" \b \t \r \n 🎉 #nostr | 1 | 1722337838 | | |
| 50ed63c449df67d89e9964a27a26abbf214ca155b03915067a5a0f75618802bb | Hello | 1 | 1722337838 | | [[]] |
Then Bob receives messages
| Type | Id | Success |
| OK | ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff | false |
| OK | a6d166e834e78827af0770f31f15b13a772f281ad880f43ce12c24d4e3d0e346 | false |
| OK | 9a6b4cefcd17f3bf7fb03c02da044c628836a118c47d5b92503c1d2bdb796296 | true |
| OK | bb5d2fe5b2c16c676d87ef446fa38581b9fa45e2e50ba89568664abf4e1d1396 | true |
| OK | 50ed63c449df67d89e9964a27a26abbf214ca155b03915067a5a0f75618802bb | false |
And Alice receives a message
| Type | Id | EventId |
| EOSE | abcd | |
| EVENT | abcd | 9a6b4cefcd17f3bf7fb03c02da044c628836a118c47d5b92503c1d2bdb796296 |
| EVENT | abcd | bb5d2fe5b2c16c676d87ef446fa38581b9fa45e2e50ba89568664abf4e1d1396 |

Scenario: Newly subscribed client receives matching events, EOSE and future events
Bob publishes events which are stored by the relay before any subscription exists.
Expand Down
8 changes: 4 additions & 4 deletions test/Netstr.Tests/NIPs/01.feature.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/Netstr.Tests/NIPs/Types.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Netstr.Extensions;
using Netstr.Json;
using Netstr.Messaging.Models;
using System.Net.WebSockets;
using System.Text.Json;
Expand Down

0 comments on commit 9caff83

Please sign in to comment.