Skip to content

Commit

Permalink
Code review 11
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioaversa committed Feb 23, 2024
1 parent d4b4a41 commit 60ac7a3
Show file tree
Hide file tree
Showing 10 changed files with 221 additions and 51 deletions.
25 changes: 12 additions & 13 deletions rules/S6930/S6930.AspNetCore.cs
Original file line number Diff line number Diff line change
@@ -1,43 +1,42 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Routing;
using System.Diagnostics.CodeAnalysis;

[Route(@"A\[controller]")] // Noncompliant {{Replace this `\` with `/`.}}
// ^
[Route(@"A\[controller]")] // Noncompliant {{Replace `\` with `/`.}}
// ^^^^^^^^^^^^^^^^^
public class BackslashOnControllerUsingVerbatimString : Controller { }

[Route("A\\[controller]")] // Noncompliant
// ^^
[Route("A\\[controller]")] // Noncompliant {{Replace `\` with `/`.}}
// ^^^^^^^^^^^^^^^^^
public class BackslashOnControllerUsingEscapeCharacter : Controller { }

[Route("A\\[controller]\\B")] // Noncompliant
// ^^
// ^^@-1
[Route("A\\[controller]\\B")] // Noncompliant {{Replace `\` with `/`.}}
// ^^^^^^^^^^^^^^^^^^^^
public class MultipleBackslashesOnController : Controller { }

public class BackslashOnActionUsingVerbatimString : Controller
{
[Route(@"A\[action]")] // Noncompliant
// ^
[Route(@"A\[action]")] // Noncompliant {{Replace `\` with `/`.}}
// ^^^^^^^^^^^^^
public IActionResult Index() => View();
}

public class BackslashOnActionUsingEscapeCharacter : Controller
{
[Route("A\\[action]")] // Noncompliant
// ^^
// ^^^^^^^^^^^^^
public IActionResult Index() => View();
}

public class MultipleBackslashesOnAction : Controller
{
[Route("A\\[action]\\B")] // Noncompliant
// ^^
// ^^@-1
// ^^^^^^^^^^^^^^^^
public IActionResult Index() => View();
}

[Route("\\[controller]")] // Noncompliant
// ^^
// ^^^^^^^^^^^^^^^^
public class RouteOnControllerStartingWithBackslash : Controller { }

public class AController : Controller
Expand Down
112 changes: 112 additions & 0 deletions rules/S6930/S6930.AspNetCore.vb
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<Route("A\[controller]")> ' Noncompliant ^8#16 {{Replace `\` with `/`.}}
Public Class BackslashOnController : Inherits Controller : End Class

<Route("A\[controller]\B")> ' Noncompliant ^8#18 {{Replace `\` with `/`.}}
Public Class MultipleBackslashesOnController : Inherits Controller : End Class

Public Class BackslashOnAction
Inherits Controller

<Route("A\[action]")> ' Noncompliant ^12#12 {{Replace `\` with `/`.}}
Public Function Index() As IActionResult
Return View()
End Function
End Class

Public Class MultipleBackslashesOnAction
Inherits Controller

<Route("A\[action]\B")> ' Noncompliant ^12#14 {{Replace `\` with `/`.}}
Public Function Index() As IActionResult
Return View()
End Function
End Class

<Route("\[controller]")> ' Noncompliant
Public Class RouteOnControllerStartingWithBackslash
Inherits Controller
End Class

Public Class AController
Inherits Controller

' [Route("A\\[action]")] ' Compliant: commented out
Public Function WithoutRouteAttribute() As IActionResult
Return View()
End Function

<Route("A\[action]", Name:="a", Order:=3)> ' Noncompliant
Public Function WithOptionalAttributeParameters() As IActionResult
Return View()
End Function

<Route("A/[action]", Name:="a\b", Order:=3)> ' Compliant: backslash is on the name
Public Function WithBackslashInRouteName() As IActionResult
Return View()
End Function

<RouteAttribute("A\[action]")> ' Noncompliant
Public Function WithAttributeSuffix() As IActionResult
Return View()
End Function

<Microsoft.AspNetCore.Mvc.RouteAttribute("A\[action]")> ' Noncompliant
Public Function WithFullQualifiedName() As IActionResult
Return View()
End Function

<Route("A\[action]")> ' Noncompliant
<Route("B\[action]")> ' Noncompliant
<Route("C/[action]")> ' Compliant: forward slash is used
Public Function WithMultipleRoutes() As IActionResult
Return View()
End Function

<Route("A%5C[action]")> ' Compliant: URL-escaped backslash is used
Public Function WithUrlEscapedBackslash() As IActionResult
Return View()
End Function

<Route("A/{s:regex(^(?!index\b)[[a-zA-Z0-9-]]+$)}.html")> ' Compliant: backslash is in regex
Public Function WithRegexContainingBackslashInLookahead(ByVal s As String) As IActionResult
Return View()
End Function

<Route("A/{s:datetime:regex(\d{{4}}-\d{{2}}-\d{{4}})}/B")> ' Compliant: backslash is in regex
Public Function WithRegexContainingBackslashInMetaEscape(ByVal s As String) As IActionResult
Return View()
End Function
End Class

Public Class WithAllTypesOfStrings
Inherits Controller

Private Const AConstStringIncludingABackslash As String = "A\"
Private Const AConstStringNotIncludingABackslash As String = "A/"

<Route(AConstStringIncludingABackslash)> ' Noncompliant
Public Function WithConstStringIncludingABackslash() As IActionResult
Return View()
End Function

<Route(AConstStringNotIncludingABackslash)> ' Compliant
Public Function WithConstStringNotIncludingABackslash() As IActionResult
Return View()
End Function
End Class

Class WithAllControllerEndpointRouteBuilderExtensionsMethods
Private Sub MapControllerRoute(ByVal app As WebApplication)
app.MapControllerRoute(name:="default", pattern:="{controller=Home}\{action=Index}/{id?}") ' Noncompliant
app.MapControllerRoute("default", "{controller=Home}\{action=Index}/{id?}") ' Noncompliant
app.MapAreaControllerRoute("default", "area", "{controller=Home}\{action=Index}/{id?}") ' Noncompliant
app.MapFallbackToAreaController("{controller=Home}\{action=Index}", "action", "controller", "area") ' Noncompliant
app.MapFallbackToAreaController("\action", "\controller", "\area") ' Compliant
app.MapFallbackToController("\action", "\controller") ' Compliant
app.MapFallbackToController("{controller=Home}\{action=Index}", "\action", "\controller") ' Noncompliant
app.MapFallbackToPage("\action") ' Compliant
app.MapFallbackToPage("{controller=Home}\{action=Index}", "\page") ' Noncompliant

ControllerEndpointRouteBuilderExtensions.MapControllerRoute(app, "default", "{controller=Home}\{action=Index}/{id?}") ' Noncompliant
End Sub
End Class
28 changes: 13 additions & 15 deletions rules/S6930/S6930.AspNetFramework.cs
Original file line number Diff line number Diff line change
@@ -1,43 +1,41 @@
using System.Web.Mvc;
using System;

[Route(@"A\[controller]")] // Noncompliant {{Replace this `\` with `/`.}}
// ^
[Route(@"A\[controller]")] // Noncompliant {{Replace `\` with `/`.}}
// ^^^^^^^^^^^^^^^^^
public class BackslashOnControllerUsingVerbatimString : Controller { }

[Route("A\\[controller]")] // Noncompliant
// ^^
[Route("A\\[controller]")] // Noncompliant {{Replace `\` with `/`.}}
// ^^^^^^^^^^^^^^^^^
public class BackslashOnControllerUsingEscapeCharacter : Controller { }

[Route("A\\[controller]\\B")] // Noncompliant
// ^^
// ^^@-1
[Route("A\\[controller]\\B")] // Noncompliant {{Replace `\` with `/`.}}
// ^^^^^^^^^^^^^^^^^^^^
public class MultipleBackslashesOnController : Controller { }

public class BackslashOnActionUsingVerbatimString : Controller
{
[Route(@"A\[action]")] // Noncompliant
// ^
[Route(@"A\[action]")] // Noncompliant {{Replace `\` with `/`.}}
// ^^^^^^^^^^^^^
public ActionResult Index() => View();
}

public class BackslashOnActionUsingEscapeCharacter : Controller
{
[Route("A\\[action]")] // Noncompliant
// ^^
// ^^^^^^^^^^^^^
public ActionResult Index() => View();
}

public class MultipleBackslashesOnAction : Controller
{
[Route("A\\[action]\\B")] // Noncompliant
// ^^
// ^^@-1
// ^^^^^^^^^^^^^^^^
public ActionResult Index() => View();
}

[Route("\\[controller]")] // Noncompliant
// ^^
// ^^^^^^^^^^^^^^^^
public class RouteOnControllerStartingWithBackslash : Controller { }

public class AController : Controller
Expand All @@ -63,10 +61,10 @@ public class AController : Controller
public ActionResult WithUrlEscapedBackslash() => View();

[Route("A/{s:regex(^(?!index\\b)[[a-zA-Z0-9-]]+$)}.html")]
public ActionResult WithRegexContainingBackslashInLookahead(string s) => View(); // Compliant: backslash is in regex
public ActionResult WithRegexContainingBackslashInLookahead() => View(); // Compliant: backslash is in regex

[Route("A/{s:datetime:regex(\\d{{4}}-\\d{{2}}-\\d{{4}})}/B")]
public ActionResult WithRegexContainingBackslashInMetaEscape(string s) => View(); // Compliant: backslash is in regex
public ActionResult WithRegexContainingBackslashInMetaEscape() => View(); // Compliant: backslash is in regex
}

namespace WithAliases
Expand Down
78 changes: 78 additions & 0 deletions rules/S6930/S6930.AspNetFramework.vb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<Route("A\[controller]")> ' Noncompliant ^8#16 {{Replace `\` with `/`.}}
Public Class BackslashOnController
Inherits Controller
End Class

<Route("A\[controller]\B")> ' Noncompliant ^8#18 {{Replace `\` with `/`.}}
Public Class MultipleBackslashesOnController
Inherits Controller
End Class

Public Class BackslashOnAction
Inherits Controller

<Route("A\[action]")> ' Noncompliant ^12#11 {{Replace `\` with `/`.}}
Public Function Index() As ActionResult
Return View()
End Function
End Class

Public Class MultipleBackslashesOnAction
Inherits Controller

<Route("A\[action]\B")> ' Noncompliant ^12#13 {{Replace `\` with `/`.}}
Public Function Index() As ActionResult
Return View()
End Function
End Class

<Route("\[controller]")>
Public Class RouteOnControllerStartingWithBackslash
Inherits Controller
End Class

Public Class AController
Inherits Controller

Public Function WithoutRouteAttribute() As ActionResult ' Compliant
Return View()
End Function

<Route("A\[action]", Name:="a", Order:=3)> ' Noncompliant
Public Function WithOptionalAttributeParameters() As ActionResult
Return View()
End Function

<RouteAttribute("A\[action]")> ' Noncompliant
Public Function WithAttributeSuffix() As ActionResult
Return View()
End Function

<System.Web.Mvc.RouteAttribute("A\[action]")> ' Noncompliant
Public Function WithFullQualifiedName() As ActionResult
Return View()
End Function

<Route("A\[action]")> ' Noncompliant
<Route("B\[action]")> ' Noncompliant
<Route("C/[action]")> ' Compliant: forward slash is used
Public Function WithMultipleRoutes() As ActionResult
Return View()
End Function

<Route("A%5C[action]")> ' Compliant: URL-escaped backslash is used
Public Function WithUrlEscapedBackslash() As ActionResult
Return View()
End Function

<Route("A/{s:regex(^(?!index\b)[[a-zA-Z0-9-]]+$)}.html")> ' Compliant: backslash is in regex
Public Function WithRegexContainingBackslashInLookahead() As ActionResult
Return View()
End Function

<Route("A/{s:datetime:regex(\d{{4}}-\d{{2}}-\d{{4}})}/B")> ' Compliant: backslash is in regex
Public Function WithRegexContainingBackslashInMetaEscape() As ActionResult
Return View()
End Function
End Class

17 changes: 0 additions & 17 deletions rules/S6930/S6930.vb

This file was deleted.

2 changes: 1 addition & 1 deletion rules/S6930/csharp/noncompliant-code-attribute.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[source,csharp,diff-id=1,diff-type=noncompliant]
----
[Route(@"Something\[controller]")] // Noncompliant: Replace this `\` with `/`
[Route(@"Something\[controller]")] // Noncompliant: Replace `\` with `/`.
public class HomeController : Controller
{
[HttpGet]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
----
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}\\{action=Index}"); // Noncompliant: Replace this `\` with `/`
pattern: "{controller=Home}\\{action=Index}"); // Noncompliant: Replace `\` with `/`.
----
4 changes: 2 additions & 2 deletions rules/S6930/rspecator.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ ifdef::env-github,rspecator-view[]

=== Message

Replace this `\` with `/`.
Replace `\` with `/`.


=== Highlighting

The '\' character.
The entire string which contains the '\' character(s).

'''
== Comments And Links
Expand Down
2 changes: 1 addition & 1 deletion rules/S6930/vbnet/noncompliant-code-attribute.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[source,vbnet,diff-id=1,diff-type=noncompliant]
----
<Route("Something\[controller]")> ' Noncompliant: Replace this `\` with `/`.
<Route("Something\[controller]")> ' Noncompliant: Replace `\` with `/`.
Public Class HomeController
Inherits Controller
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
----
app.MapControllerRoute(
name:="default",
pattern:="{controller=Home}\{action=Index}") ' Noncompliant: Replace this `\` with `/`
pattern:="{controller=Home}\{action=Index}") ' Noncompliant: Replace `\` with `/`.
----

0 comments on commit 60ac7a3

Please sign in to comment.