Skip to content

Commit ecb4787

Browse files
MihaZupangewarren
andauthored
Add Uri Security Considerations section (#4232)
* Add Uri Security Considerations section Co-authored-by: Genevieve Warren <[email protected]>
1 parent f16d746 commit ecb4787

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

xml/System/Uri.xml

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,36 @@ Uri uri1 = new Uri("C:/test/path/file.txt") // Implicit file path.
188188
Uri uri2 = new Uri("file:///C:/test/path/file.txt") // Explicit file path.
189189
```
190190
These implicit file paths are not compliant with the URI specification and so should be avoided when possible. When using .NET Core on Unix-based systems, implicit file paths can be especially problematic, because an absolute implicit file path is *indistinguishable* from a relative path. When such ambiguity is present, <xref:System.Uri> default to interpreting the path as an absolute URI.
191+
192+
## Security Considerations
193+
194+
Because of security concerns, your application should use caution when accepting <xref:System.Uri> instances from untrusted sources and with `dontEscape` set to `true` in the [constructor](xref:System.Uri.%23ctor(System.String,System.Boolean)). You can check a URI string for validity by calling the <xref:System.Uri.IsWellFormedOriginalString%2A> method.
195+
196+
When dealing with untrusted user input, confirm assumptions about the newly created `Uri` instance before trusting its properties.
197+
This can be done in the following way:
198+
199+
```csharp
200+
string userInput = ...;
201+
202+
Uri baseUri = new Uri("https://myWebsite/files/");
203+
204+
if (!Uri.TryCreate(baseUri, userInput, out Uri newUri))
205+
{
206+
// Fail: invalid input.
207+
}
208+
209+
if (!baseUri.IsBaseOf(newUri))
210+
{
211+
// Fail: the Uri base has been modified - the created Uri is not rooted in the original directory.
212+
}
213+
```
214+
215+
This validation can be used in other cases, like when dealing with UNC paths, by simply changing the `baseUri`:
216+
217+
```csharp
218+
Uri baseUri = new Uri(@"\\host\share\some\directory\name\");
219+
```
220+
191221
192222
## Performance Considerations
193223
If you use a *Web.config *file that contains URIs to initialize your application, additional time is required to process the URIs if their scheme identifiers are nonstandard. In such a case, initialize the affected parts of your application when the URIs are needed, not at start time.
@@ -203,9 +233,6 @@ Uri uri2 = new Uri("file:///C:/test/path/file.txt") // Explicit file path.
203233
204234
]]></format>
205235
</remarks>
206-
<block subset="none" type="usage">
207-
<para>Because of security concerns, your application should use caution when accepting <see cref="T:System.Uri" /> instances from untrusted sources and with <paramref name="dontEscape" /> set to <see langword="true" />.You can check a URI string for validity by calling the <see cref="M:System.Uri.IsWellFormedOriginalString" /> method.</para>
208-
</block>
209236
<altmember cref="T:System.Configuration.IdnElement" />
210237
<altmember cref="T:System.Configuration.IriParsingElement" />
211238
<altmember cref="T:System.Configuration.UriSection" />

0 commit comments

Comments
 (0)