Repository navigation
Accept only Ecency app codes and posting tokens as private-api sessions #109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| using System.Text.Json.Nodes; | ||
| using EcencyApi.Handlers; | ||
| using EcencyApi.Infrastructure; | ||
| using NBitcoin.Secp256k1; | ||
| using Xunit; | ||
|
|
||
| namespace EcencyApi.Tests; | ||
|
|
||
| /// <summary> | ||
| /// ValidateCode end to end, with real signatures: only a code or posting token | ||
| /// issued to the Ecency app is a session, whether the user's own key signed it | ||
| /// or @hivesigner did when it exchanged a code, and the rest is refused before | ||
| /// any account is read. | ||
| /// </summary> | ||
| [Collection("session-token")] | ||
| public class SessionTokenTests : IDisposable | ||
| { | ||
| private static readonly ECPrivKey Key = HiveCrypto.FromLogin("alice", "test-password"); | ||
| private static readonly string Pub = HiveCrypto.PublicKeyFromLogin("alice", "test-password"); | ||
| // The key @hivesigner signs the tokens it exchanges codes for. | ||
| private static readonly ECPrivKey HsKey = HiveCrypto.FromLogin("hivesigner", "test-password"); | ||
| private static readonly string HsPub = HiveCrypto.PublicKeyFromLogin("hivesigner", "test-password"); | ||
| private readonly List<string?> _reads = new(); | ||
|
|
||
| public SessionTokenTests() | ||
| { | ||
| PrivateApi.ResetHivesignerCache(); | ||
| PrivateApi.ValidationAccounts = names => | ||
| { | ||
| var name = names.Single(); | ||
| _reads.Add(name); | ||
| var account = new JsonObject | ||
| { | ||
| ["name"] = name, | ||
| ["posting"] = new JsonObject | ||
| { | ||
| ["key_auths"] = new JsonArray(new JsonArray(name == "hivesigner" ? HsPub : Pub, 1)), | ||
| }, | ||
| }; | ||
| return Task.FromResult<JsonArray?>(new JsonArray(account)); | ||
| }; | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| PrivateApi.ValidationAccounts = names => HiveClients.Default.GetAccounts(names); | ||
| PrivateApi.ResetHivesignerCache(); | ||
| } | ||
|
|
||
| /// <summary>A request body carrying `signedMessage` for alice, signed by `key`.</summary> | ||
| private static JsonObject Body(string signedMessage, ECPrivKey? key = null) | ||
| { | ||
| var message = new JsonObject | ||
| { | ||
| ["signed_message"] = JsonNode.Parse(signedMessage), | ||
| ["authors"] = new JsonArray("alice"), | ||
| ["timestamp"] = 1726650000, | ||
| }; | ||
| var digest = HiveCrypto.Sha256Utf8(JsJson.Stringify(message)); | ||
| message["signatures"] = new JsonArray(HiveCrypto.Sign(key ?? Key, digest)); | ||
| return new JsonObject { ["code"] = B64u.Encode(JsJson.Stringify(message)) }; | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("""{"type":"posting","app":"ecency.app"}""")] | ||
| [InlineData("""{"type":"code","app":"ecency.app"}""")] | ||
| public async Task EcencyTokensAreSessions(string signedMessage) | ||
| { | ||
| Assert.Equal("alice", await PrivateApi.ValidateCode(Body(signedMessage))); | ||
| Assert.Equal(new[] { "alice" }, _reads); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task APostingTokenHivesignerExchangedIsASession() | ||
| { | ||
| // What web and mobile hold: HiveSigner's posting token for the Ecency | ||
| // app, signed by @hivesigner rather than by the user's own key. | ||
| var body = Body("""{"type":"posting","app":"ecency.app"}""", HsKey); | ||
| Assert.Equal("alice", await PrivateApi.ValidateCode(body)); | ||
| Assert.Equal(new[] { "alice", "hivesigner" }, _reads); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task HivesignerSignedTokensForOtherAppsAreNot() | ||
| { | ||
| var body = Body("""{"type":"posting","app":"another.app"}""", HsKey); | ||
| Assert.Null(await PrivateApi.ValidateCode(body)); | ||
| Assert.Empty(_reads); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("""{"message":"hello"}""")] | ||
| [InlineData("""{"type":"login","app":"ecency.app"}""")] | ||
| [InlineData("""{"type":"posting","app":"another.app"}""")] | ||
| public async Task OtherSignedMessagesAreNot(string signedMessage) | ||
| { | ||
| Assert.Null(await PrivateApi.ValidateCode(Body(signedMessage))); | ||
| Assert.Empty(_reads); | ||
| } | ||
| } | ||
|
|
||
| [CollectionDefinition("session-token", DisableParallelization = true)] | ||
| public class SessionTokenCollection | ||
| { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2. Session denials lack endpoint coverage
📘 Rule violation▣ TestabilityAgent Prompt
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation toolsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No parity entry needed. The harness only sends invalid codes (
invalid,badcode), and those are refused identically before and after this change. No catalogued case sends a correctly signed token, so no response diverges. The new rule is covered bySessionTokenTestswith real signatures.