-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A malicious user may be able to forge new log entries or may access unexpected resources.
- Loading branch information
Showing
5 changed files
with
79 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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$"); | ||
} | ||
|
||
private static void AssertSanitizeFileName(string expected, string fileName) | ||
=> Assert.AreEqual(expected, fileName.SanitizeFileName()); | ||
} |