-
Notifications
You must be signed in to change notification settings - Fork 2
Private API: favorite-tags routes #93
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| using System.Text.Json.Nodes; | ||
| using EcencyApi.Handlers; | ||
| using Xunit; | ||
|
|
||
| namespace EcencyApi.Tests; | ||
|
|
||
| /// <summary> | ||
| /// The favorite-tag check and delete handlers interpolate a body-supplied tag into | ||
| /// an upstream path. A tag is an arbitrary string, so anything structural left | ||
| /// unescaped is re-parsed when the string becomes a Uri, and the upstream call | ||
| /// carries this service's credentials. Same reasoning as NotificationsPathTests. | ||
| /// </summary> | ||
| public class FavoriteTagsPathTests | ||
| { | ||
| [Fact] | ||
| public void RealRequestsAreUnchanged() | ||
| { | ||
| // Hive names and tags are unreserved characters; escaping must be a no-op for | ||
| // them or this would change every live request. | ||
| Assert.Equal( | ||
| "isfavoritetag/good-karma/photography", | ||
| PrivateApi.FavoriteTagPath("isfavoritetag", "good-karma", "photography")); | ||
| Assert.Equal( | ||
| "favoriteTag/user.name/contest-2026", | ||
| PrivateApi.FavoriteTagPath("favoriteTag", "user.name", "contest-2026")); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void LeadingHashIsEscapedNotDropped() | ||
| { | ||
| // Raw, `#` would truncate the path at a fragment. Escaped, it reaches the | ||
| // upstream, which normalises the tag and strips one leading hash itself. | ||
| Assert.Equal( | ||
| "isfavoritetag/good-karma/%23photography", | ||
| PrivateApi.FavoriteTagPath("isfavoritetag", "good-karma", "#photography")); | ||
| } | ||
|
|
||
| [Theory] | ||
| // A missing tag must not become the literal "undefined" the way a missing | ||
| // account does in the favorites handlers: "undefined", "null", "true" and "123" | ||
| // are all valid tag names upstream, so templating would check or delete a follow | ||
| // the caller never named. | ||
| [InlineData("{}")] | ||
| [InlineData("{\"tag\": null}")] | ||
| [InlineData("{\"tag\": true}")] | ||
| [InlineData("{\"tag\": 123}")] | ||
| [InlineData("{\"tag\": \"\"}")] | ||
| [InlineData("{\"tag\": [\"photography\"]}")] | ||
| [InlineData("{\"tag\": {\"name\": \"photography\"}}")] | ||
| public void AbsentOrNonStringTagIsRejected(string json) | ||
| { | ||
| var body = (JsonObject)JsonNode.Parse(json)!; | ||
|
|
||
| Assert.Null(PrivateApi.FavoriteTagField(body)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void StringTagIsReadAsIs() | ||
| { | ||
| // Read leniently and passed through untouched: normalising is the upstream's job. | ||
| var body = (JsonObject)JsonNode.Parse("{\"tag\": \"#Photography\"}")!; | ||
| Assert.Equal("#Photography", PrivateApi.FavoriteTagField(body)); | ||
|
|
||
| // Lone surrogates are valid JSON string content and must not be dropped. | ||
| var lone = (JsonObject)JsonNode.Parse("{\"tag\": \"\\ud800\"}")!; | ||
| Assert.Equal("\ud800", PrivateApi.FavoriteTagField(lone)); | ||
| } | ||
|
|
||
| [Theory] | ||
| // A slash would add path segments and address a different resource. | ||
| [InlineData("a", "b/c")] | ||
| [InlineData("a/b", "c")] | ||
| // A question mark would truncate the path and turn the rest into a query. | ||
| [InlineData("a", "b?x=1")] | ||
| // A hash would truncate the path at a fragment. | ||
| [InlineData("a", "b#f")] | ||
| // Whitespace and non-ASCII must not leak into the request line either. | ||
| [InlineData("a", "b c")] | ||
| [InlineData("a", "caf\u00e9")] | ||
| public void StructuralCharactersCannotEscapeTheirSegment(string username, string tag) | ||
| { | ||
| var path = PrivateApi.FavoriteTagPath("isfavoritetag", username, tag); | ||
|
|
||
| Assert.NotNull(path); | ||
| Assert.DoesNotContain("?x=1", path); | ||
| Assert.DoesNotContain("#f", path); | ||
| Assert.DoesNotContain(" ", path); | ||
| Assert.True(path!.All(c => c < 128)); | ||
| // The only separators left are the two this builder wrote itself. | ||
| Assert.Equal(2, path.Split('/').Length - 1); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void LoneSurrogateIsEncodedNotThrown() | ||
| { | ||
| // JSON strings are arbitrary UTF-16, so a lone surrogate reaches the helper as | ||
| // a real value (see TryGetStringLenient). On the runtime this service targets, | ||
| // EscapeDataString encodes it as U+FFFD rather than throwing, so the request | ||
| // stays on the handler's own path instead of falling through to the 500 page. | ||
| Assert.Equal( | ||
| "isfavoritetag/good-karma/%EF%BF%BD", | ||
| PrivateApi.FavoriteTagPath("isfavoritetag", "good-karma", "\ud800")); | ||
| Assert.Equal( | ||
| "favoriteTag/good-karma/a%EF%BF%BDb", | ||
| PrivateApi.FavoriteTagPath("favoriteTag", "good-karma", "a\udc00b")); | ||
| } | ||
|
|
||
| [Theory] | ||
| // Dot segments cannot be fixed by escaping: Uri decodes %2E back to `.` before it | ||
| // removes dot segments, so they have to be rejected outright. | ||
| [InlineData(".", "photography")] | ||
| [InlineData("..", "photography")] | ||
| [InlineData("good-karma", ".")] | ||
| [InlineData("good-karma", "..")] | ||
| public void DotSegmentsAreRejected(string username, string tag) | ||
| { | ||
| Assert.Null(PrivateApi.FavoriteTagPath("isfavoritetag", username, tag)); | ||
| } | ||
| } |
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
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.
When an authenticated client supplies a valid JSON string containing a lone surrogate such as
"\ud800",TemplateFielddeliberately materializes that UTF-16 value, butUri.EscapeDataString(tag)throwsUriFormatException. The check and delete handlers therefore fall through to the global 500 response instead of reaching their intended invalid-tag 400 branch. Validate Unicode scalar sequences or catch the escaping exception and returnnullfrom this helper.Useful? React with 👍 / 👎.
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.
Checked on the runtime this service targets (
net10.0):Uri.EscapeDataString("\ud800")does not throw. It encodes the lone surrogate as U+FFFD, giving%EF%BF%BD, and"a\udc00b"givesa%EF%BF%BDb. So the check and delete handlers stay on their own path and never reach the 500 page on this input.NotificationsPathandPostTipsPathrely on the same behaviour.Pinned it as
LoneSurrogateIsEncodedNotThrowninFavoriteTagsPathTests, so a runtime change would show up in CI rather than in production.