Skip to content
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

Improve Path Sanitization #161

Merged
merged 5 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/PathSanitizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Text.RegularExpressions;

static class PathSanitizer
{
static readonly Regex invalidCharsRegex = new(@"\W");
public static string Sanitize(string path, string parent)
{
var partStr = invalidCharsRegex.Replace(path, "_");
if (char.IsDigit(partStr[0]))
partStr = "_" + partStr;
if (partStr == parent)
partStr = "_" + partStr;
return partStr;
}
}
2 changes: 1 addition & 1 deletion src/ThisAssembly.Resources/CSharp.sbntxt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/// => @"{{ $0.Path }}"
{{~ end ~}}
/// </summary>
public static partial class {{ $0.Name | string.replace "-" "_" | string.replace " " "_" }}
public static partial class {{ $0.Name }}
{
{{~ if $0.IsText ~}}
private static string text;
Expand Down
25 changes: 10 additions & 15 deletions src/ThisAssembly.Resources/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,24 @@ public static Area Load(string basePath, List<Resource> resources, string rootAr
var parts = basePath.Split(new[] { "\\", "/" }, StringSplitOptions.RemoveEmptyEntries);
var end = resources.Count == 1 ? ^1 : ^0;

var parent = "Resources";
foreach (var part in parts.AsSpan()[..end])
{
var partStr = SanitizePart(part);
var partStr = PathSanitizer.Sanitize(part, parent);
parent = partStr;

area.NestedArea = new Area(partStr);
area = area.NestedArea;
}

area.Resources = resources;
area.Resources = resources
.Select(r => r with
{
Name = PathSanitizer.Sanitize(r.Name, parent),
});
return root;
}

static readonly Regex invalidCharsRegex = new(@"\W");
static string SanitizePart(string? part)
{
var partStr = invalidCharsRegex.Replace(part, "_");
if (char.IsDigit(partStr[0]))
partStr = "_" + partStr;
return partStr;
}
}

[DebuggerDisplay("{Name}")]
record Resource(string Name, string? Comment, bool IsText)
{
public string? Path { get; set; }
};
record Resource(string Name, string? Comment, bool IsText, string Path);
9 changes: 5 additions & 4 deletions src/ThisAssembly.Resources/ResourcesGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ static void GenerateSource(
var name = group.Count() == 1
? Path.GetFileNameWithoutExtension(f.resourceName)
: Path.GetExtension(f.resourceName)[1..];
return new Resource(name, f.comment, isText)
{
Path = f.resourceName,
};
return new Resource(
Name: name,
Comment: f.comment,
IsText: isText,
Path: f.resourceName);
})
.ToList();

Expand Down
4 changes: 4 additions & 0 deletions src/ThisAssembly.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,9 @@ public void CanUseByteResource()
[Fact]
public void CanUseSameNameDifferentExtensions()
=> Assert.NotNull(ThisAssembly.Resources.Content.Swagger.swagger_ui.css.GetBytes());

[Fact]
public void CanUseFileInvalidCharacters()
=> Assert.NotNull(ThisAssembly.Resources.webhook_data.Text);
}
}
4 changes: 1 addition & 3 deletions src/ThisAssembly.Tests/ThisAssembly.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
<Import Project="..\*\*.props" />

<ItemGroup>
<Compile Remove="..\EmbeddedResource.cs" />
<Compile Remove="..\GeneratorExtension.cs" />
<Compile Remove="..\IsExternalInit.cs" />
<Compile Remove="..\*.cs" />
</ItemGroup>

<ItemGroup>
Expand Down