-
Notifications
You must be signed in to change notification settings - Fork 762
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
Enhance path validation logic #4495
Conversation
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.
I'm not sure what we're trying to achieve here. This generator is producing a client library that calls an endpoint. Are we trying to protect the endpoint by preventing the client from sending specific strings? Is this really appropriate? The endpoint is the one that should defend against this, right?
What if the client legitimately needs to send those funny strings?
It's not to protect then endpoint, but to protect the application using the generated code from accessing endpoints that it's not supposed to. The code path it's changing uses method arguments to create the destination endpoint. If this argument is not sanitized then it could be formed in a way that the application would call other endpoints that could potentially be abused, leaking other information from the request, or altering another part of the system. Simple example: your application intends to send a request to delete a product using |
For context, this is something that came up while threat modeling the AutoClient functionality. When it was presented in the threat modeling session, the scenario provided was that it was trying to simplify the client's ability to invoke these endpoints by allowing the client to perform the equivalent of format string building. Threat modeling asked if it was reasonable to expect that these inputs might come directly from untrusted user input (e.g., provided directly by a remote caller) and thus might be hostile, and the answer was yes. So we end up with a path traversal-style attack. A malicious client tricks an innocent frontend into making unintended privileged requests to an access-controlled backend. Sebastien talked about this earlier at #4495 (comment). There are two mitigations recommended for this risk. First, the arguments need to be properly escaped when the URI string is being built. (Sebastien already got to this in some previous PRs.) Second, certain patterns - even after escaping - may still be dangerous. It's fairly common for web servers to collapse The security review team was fine with either of the two approaches: defense-in-depth or document warnings for consumers. It's primarily a function of what audience you see consuming this library. Looks like this PR opts for the former. |
return false; | ||
} | ||
|
||
if (value.IndexOfAny({{staticsName}}.Slashes) > -1) |
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.
If we assume that 99% of the times, the strings will not contain slashes or dots, can we make a fast trivial exit paths to reduce computation?
if (value.IndexOfAny(SlahesOrDots) < 0)
return true;
Or something like it.
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.
Done
Waiting for approvals or feedback |
src/Libraries/Microsoft.Extensions.Http.AutoClient/AutoClientUtilities.cs
Outdated
Show resolved
Hide resolved
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.
Changes look good to me. Thanks for following up on this @sebastienros
src/Libraries/Microsoft.Extensions.Http.AutoClient/AutoClientUtilities.cs
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.Http.AutoClient/AutoClientUtilities.cs
Outdated
Show resolved
Hide resolved
7efa9b8
to
2fc2bac
Compare
2fc2bac
to
dda8053
Compare
Adding query arguments tests for the chars
?
,/
,%
,#
,Adding these rules for path segment arguments:
This is because we don't think it's legitimate for users to be able to use these as valid path arguments since it could be provided by user data (tenant name, username, ...) and escaping could be tricky as servers might un-encode them.
I am not sure
ArgumentException
is the best here so please send suggestions.Microsoft Reviewers: Open in CodeFlow