-
Notifications
You must be signed in to change notification settings - Fork 0
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
Sanitize user input #187
Sanitize user input #187
Changes from 3 commits
d9072d1
2d44e45
832dca1
83a4424
42d4fb2
6dfacf2
098f573
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
version: "3.4" | ||
|
||
services: | ||
stac-browser: | ||
image: ghcr.io/geowerkstatt/stac-browser:latest | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System.Globalization; | ||
|
||
namespace Geopilot.Api; | ||
|
||
/// <summary> | ||
/// GeoPilot API extensions. | ||
/// </summary> | ||
public static class Extensions | ||
{ | ||
/// <summary> | ||
/// Sanitizes a file name by removing invalid characters. | ||
/// </summary> | ||
/// <param name="fileName">The file name to sanitize.</param> | ||
/// <returns>The sanitized file name.</returns> | ||
public static string SanitizeFileName(this string fileName) | ||
{ | ||
fileName = fileName.Trim().ReplaceLineEndings(string.Empty); | ||
|
||
// Get invalid characters for file names and add some platform-specific ones. | ||
var invalidFileNameChars = Path.GetInvalidFileNameChars() | ||
.Concat(new[] { '?', '$', '*', '|', '<', '>', '"', ':' }).ToArray(); | ||
|
||
foreach (var invalidFileNameChar in invalidFileNameChars) | ||
{ | ||
fileName = fileName.Replace(invalidFileNameChar.ToString(CultureInfo.InvariantCulture), string.Empty); | ||
} | ||
|
||
return fileName; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
namespace Geopilot.Api.Controllers; | ||
|
||
[TestClass] | ||
public class Extensions | ||
{ | ||
[TestMethod] | ||
public void SanitizeFileNameForValidFileNames() | ||
{ | ||
AssertSanitizeFileName("SQUIRRELGENESIS", "SQUIRRELGENESIS"); | ||
AssertSanitizeFileName("JUNIORARK.xyz", "JUNIORARK.xyz"); | ||
AssertSanitizeFileName("PEEVEDBEAM-ANT.MESS.abc", "PEEVEDBEAM-ANT.MESS.abc"); | ||
AssertSanitizeFileName("WEIRD WATER.example", "WEIRD WATER.example"); | ||
AssertSanitizeFileName("AUTOFIRE123.doc", "AUTOFIRE123.doc"); | ||
AssertSanitizeFileName("SUNNY(1).doc", "SUNNY(1).doc"); | ||
AssertSanitizeFileName("ODD_MONKEY.doc", "ODD_MONKEY.doc"); | ||
AssertSanitizeFileName("SILLY,MONKEY.docx", "SILLY,MONKEY.docx"); | ||
AssertSanitizeFileName("CamelCase.bat", "CamelCase.bat"); | ||
AssertSanitizeFileName("SLICKER-CHIPMUNK.bat", "SLICKER-CHIPMUNK.bat"); | ||
} | ||
|
||
[TestMethod] | ||
public void SanitizeFileNameForInvalidFileNames() | ||
{ | ||
AssertSanitizeFileName("CHIPMUNKWALK", " CHIPMUNKWALK "); | ||
AssertSanitizeFileName("SLEEPYBOUNCE", "SLEEPYBOUNCE\n"); | ||
AssertSanitizeFileName("PLOWARK", "PLOWARK\r"); | ||
AssertSanitizeFileName("JUNIORGLEE", "JUNIORGLEE\t"); | ||
AssertSanitizeFileName("LATENTROUTE34", "LATENTROUTE?34"); | ||
AssertSanitizeFileName("TRAWLSOUFFLE", "TRAWLSOUFFLE*"); | ||
AssertSanitizeFileName("VIOLENTIRON", "><VIOLENTIRON\"|"); | ||
AssertSanitizeFileName("YELLOWBAGEL", "YELLOWBAGEL://"); | ||
AssertSanitizeFileName("ZANYWATER", "ZANYWATER$"); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. REQ: Tests als DataRows umsetzten. So werden gleich alle tests ausgeführt auch wenn einer davon fehlschlägt. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PP: Die gefixten Alerts mit Nr. Verlinken There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wart noch kurz mit dem Review... Bin nur CodeQL am Testen 😅 |
||
|
||
private static void AssertSanitizeFileName(string expected, string fileName) | ||
=> Assert.AreEqual(expected, fileName.SanitizeFileName()); | ||
} |
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.
PP: Umbenenn zu
StringExtensions
, da diese Klasse genau das beinhaltet.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.
Danke! Wartet bitte noch mit dem Review... ich schliesse diesen hier wieder 😅