rspec/rules/S4005/rule.adoc

60 lines
1.7 KiB
Plaintext
Raw Permalink Normal View History

== Why is this an issue?
2021-01-27 13:42:22 +01:00
String representations of URIs or URLs are prone to parsing and encoding errors which can lead to vulnerabilities. The ``++System.Uri++`` class is a safe alternative and should be preferred.
2021-02-02 15:02:10 +01:00
2021-01-27 13:42:22 +01:00
This rule raises an issue when a called method has a string parameter with a name containing "uri", "Uri", "urn", "Urn", "url" or "Url" and the declaring type contains a corresponding overload that takes a ``++System.Uri++`` as a parameter.
2020-06-30 12:49:37 +02:00
2021-02-02 15:02:10 +01:00
2021-01-27 13:42:22 +01:00
When there is a choice between two overloads that differ only regarding the representation of a URI, the user should choose the overload that takes a ``++System.Uri++`` argument.
2020-06-30 12:49:37 +02:00
=== Noncompliant code example
2020-06-30 12:49:37 +02:00
2022-02-04 17:28:24 +01:00
[source,text]
2020-06-30 12:49:37 +02:00
----
using System;
namespace MyLibrary
{
public class Foo
{
public void FetchResource(string uriString) { }
public void FetchResource(Uri uri) { }
public string ReadResource(string uriString, string name, bool isLocal) { }
public string ReadResource(Uri uri, string name, bool isLocal) { }
public void Main() {
FetchResource("http://www.mysite.com"); // Noncompliant
ReadResource("http://www.mysite.com", "foo-resource", true); // Noncompliant
}
}
}
----
=== Compliant solution
2020-06-30 12:49:37 +02:00
2022-02-04 17:28:24 +01:00
[source,text]
2020-06-30 12:49:37 +02:00
----
using System;
namespace MyLibrary
{
public class Foo
{
public void FetchResource(string uriString) { }
public void FetchResource(Uri uri) { }
public string ReadResource(string uriString, string name, bool isLocal) { }
public string ReadResource(Uri uri, string name, bool isLocal) { }
public void Main() {
FetchResource(new Uri("http://www.mysite.com"));
ReadResource(new Uri("http://www.mysite.com"), "foo-resource", true);
}
}
}
----