Skip to content

Commit

Permalink
Object store name check fix (#327)
Browse files Browse the repository at this point in the history
* Object store name check fix

* Check empty obj store names
  • Loading branch information
mtmk authored Jan 12, 2024
1 parent cfd01c3 commit ff8a8f0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
7 changes: 0 additions & 7 deletions src/NATS.Client.ObjectStore/NatsObjStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ public class NatsObjStore : INatsObjStore

private static readonly NatsHeaders NatsRollupHeaders = new() { { NatsRollup, RollupSubject } };

private static readonly Regex ValidObjectRegex = new(pattern: @"\A[-/_=\.a-zA-Z0-9]+\z", RegexOptions.Compiled);

private readonly NatsObjContext _objContext;
private readonly NatsJSContext _context;
private readonly INatsJSStream _stream;
Expand Down Expand Up @@ -658,11 +656,6 @@ private void ValidateObjectName(string name)
{
throw new NatsObjException("Object name can't be empty");
}

if (!ValidObjectRegex.IsMatch(name))
{
throw new NatsObjException("Object name can only contain alphanumeric characters, dashes, underscores, forward slash, equals sign, and periods");
}
}
}

Expand Down
20 changes: 20 additions & 0 deletions tests/NATS.Client.ObjectStore.Tests/ObjectStoreTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,26 @@ public async Task Put_chunks()
Assert.Equal(chunks, data.Chunks);
Assert.Equal(size, data.Size);
}

// Object name checks
{
var anyNameIsFine = "any name is fine '~#!$()*/\\,.?<>|{}[]`'\"";
await store.PutAsync(anyNameIsFine, new byte[] { 42 }, cancellationToken: cancellationToken);
var value = await store.GetBytesAsync(anyNameIsFine, cancellationToken);
Assert.Single(value);
Assert.Equal(42, value[0]);

// can't be empty
{
var exception = await Assert.ThrowsAsync<NatsObjException>(async () => await store.PutAsync(string.Empty, new byte[] { 42 }, cancellationToken: cancellationToken));
Assert.Matches("Object name can't be empty", exception.Message);
}

{
var exception = await Assert.ThrowsAsync<NatsObjException>(async () => await store.PutAsync(null!, new byte[] { 42 }, cancellationToken: cancellationToken));
Assert.Matches("Object name can't be empty", exception.Message);
}
}
}

[Fact]
Expand Down

0 comments on commit ff8a8f0

Please sign in to comment.