Skip to content

Commit

Permalink
Make path handling more robust since path is user defined
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahLerner committed Jan 30, 2020
1 parent 962824f commit 9bc737f
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/WireMock.Net/Handlers/LocalFileSystemHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void WriteMappingFile(string path, string text)
public byte[] ReadResponseBodyAsFile(string path)
{
Check.NotNullOrEmpty(path, nameof(path));

path = CleanPath(path);
// If the file exists at the given path relative to the MappingsFolder, then return that.
// Else the path will just be as-is.
return File.ReadAllBytes(File.Exists(Path.Combine(GetMappingFolder(), path)) ? Path.Combine(GetMappingFolder(), path) : path);
Expand All @@ -90,7 +90,7 @@ public byte[] ReadResponseBodyAsFile(string path)
public string ReadResponseBodyAsString(string path)
{
Check.NotNullOrEmpty(path, nameof(path));

path = CleanPath(path);
// In case the path is a filename, the path will be adjusted to the MappingFolder.
// Else the path will just be as-is.
return File.ReadAllText(File.Exists(Path.Combine(GetMappingFolder(), path)) ? Path.Combine(GetMappingFolder(), path) : path);
Expand Down Expand Up @@ -138,5 +138,20 @@ private string AdjustPath(string filename)
{
return Path.Combine(GetMappingFolder(), filename);
}

/// <summary>
/// Robust handling of the user defined path.
/// Gets the path string ready for Path.Combine method.
/// </summary>
/// <param name="path">Path to clean</param>
/// <returns></returns>
private string CleanPath(string path)
{
path = path.Replace("/", @"\");
// remove leading \ character which would break Path.Combine
path = path.StartsWith(@"\") ? path.Substring(1, path.Length - 1) : path;

return path;
}
}
}

0 comments on commit 9bc737f

Please sign in to comment.