Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.
C# audio-converteravaloniabatch-conversionconvertercsharpdocument-converterdocx-to-pdfdotnetdrag-and-dropfile-conversionfile-converterheic-converterhwphwpximage-converterlinuxmacospdf-convertervideo-converterwindows
Mail HTML keeps its old-style formatting and form text
KnifeLemon5fae4670dab4
diff --git a/src/Filee.Engines/Email/EmlConverter.cs b/src/Filee.Engines/Email/EmlConverter.csindex 8615857a..f309816f 100644--- a/src/Filee.Engines/Email/EmlConverter.cs+++ b/src/Filee.Engines/Email/EmlConverter.cs@@ -174,8 +174,18 @@ public sealed partial class EmlConverter : IConverterprivate static (string Body, string Styles) SafeHtml(string html){var document = new HtmlParser().ParseDocument(html);- foreach (var element in document.QuerySelectorAll("script, noscript, iframe, object, embed, applet, frame, frameset, portal, form, input, button, textarea, select, option, meta, link, base, svg, math, template, video, audio, source, track").ToArray())+ // Raw-text elements (noembed, xmp…) and comments go too: browsers read them as text, other HTML readers may not.+ foreach (var element in document.QuerySelectorAll("script, noscript, iframe, object, embed, applet, frame, frameset, portal, input, button, textarea, select, option, meta, link, base, svg, math, template, video, audio, source, track, noembed, noframes, xmp, plaintext").ToArray())element.Parent?.RemoveChild(element);+ foreach (var comment in document.Descendants<IComment>().ToArray())+ comment.Parent?.RemoveChild(comment);+ // Newsletters wrap whole bodies in forms: keep the content, the controls are gone already.+ foreach (var form in document.QuerySelectorAll("form").ToArray())+ {+ while (form.FirstChild is { } child)+ form.Parent!.InsertBefore(child, form);+ form.Parent!.RemoveChild(form);+ }foreach (var element in document.QuerySelectorAll("*").ToArray()){@@ -199,21 +209,16 @@ public sealed partial class EmlConverter : IConverterprivate static readonly HashSet<string> SafeHtmlAttributes = new(StringComparer.OrdinalIgnoreCase){- "class", "id", "title", "lang", "dir", "style", "align", "width", "height", "colspan", "rowspan",- "cellpadding", "cellspacing", "border",+ "class", "id", "title", "lang", "dir", "style", "align", "valign", "width", "height", "colspan", "rowspan",+ "cellpadding", "cellspacing", "border", "bgcolor", "color", "face", "size", "nowrap", "start", "type",+ "span", "scope", "headers", "abbr", "summary", "hspace", "vspace", "clear", "noshade", "frame", "rules", "open",};private static bool SafeLink(string value) => value.StartsWith('#')|| Uri.TryCreate(value, UriKind.Absolute, out var uri) && uri.Scheme is "http" or "https" or "mailto";- private static bool SafeInlineImage(string value) =>- value.StartsWith("data:image/png;base64,", StringComparison.OrdinalIgnoreCase)- || value.StartsWith("data:image/jpeg;base64,", StringComparison.OrdinalIgnoreCase)- || value.StartsWith("data:image/jpg;base64,", StringComparison.OrdinalIgnoreCase)- || value.StartsWith("data:image/gif;base64,", StringComparison.OrdinalIgnoreCase)- || value.StartsWith("data:image/webp;base64,", StringComparison.OrdinalIgnoreCase)- || value.StartsWith("data:image/bmp;base64,", StringComparison.OrdinalIgnoreCase)- || value.StartsWith("data:image/avif;base64,", StringComparison.OrdinalIgnoreCase);+ /// <summary>Any raster data: image (old Outlook writes image/x-png, image/pjpeg…), never SVG, which can carry script.</summary>+ private static bool SafeInlineImage(string value) => RasterDataImage().IsMatch(value);// ───────────────────────── Text ─────────────────────────@@ -314,6 +319,9 @@ public sealed partial class EmlConverter : IConverter[GeneratedRegex("<script[^>]*>.*?</script>", RegexOptions.IgnoreCase | RegexOptions.Singleline)]private static partial Regex ScriptBlock();+ [GeneratedRegex("^data:image/(?!svg)[a-z0-9.+-]+;base64,", RegexOptions.IgnoreCase)]+ private static partial Regex RasterDataImage();+[GeneratedRegex("cid:([^\"'\\s)>]+)", RegexOptions.IgnoreCase)]private static partial Regex ContentIdReference();diff --git a/tests/Filee.Engines.Tests/EmlTests.cs b/tests/Filee.Engines.Tests/EmlTests.csindex f1dbf7f3..51e67f4d 100644--- a/tests/Filee.Engines.Tests/EmlTests.cs+++ b/tests/Filee.Engines.Tests/EmlTests.cs@@ -52,7 +52,10 @@ public class EmlTests(EngineFixture fx) : IClassFixture<EngineFixture>const string Html = "<html><head><style>p { color: #C00000; }</style></head><body><p>안녕하세요. 자료를 보냅니다.</p>" +"<img src=\"cid:logo@filee\" alt=\"logo\"><script>alert('x')</script>" +"<img src=\"https://tracker.example/pixel\" onerror=\"alert('x')\">" +- "<a href=\"javascript:alert('x')\">bad link</a><svg onload=\"alert('x')\"></svg></body></html>";+ "<a href=\"javascript:alert('x')\">bad link</a><svg onload=\"alert('x')\"></svg>" ++ "<table bgcolor=\"#EEEEEE\"><tr><td valign=\"top\"><font color=\"#336699\">표 안</font></td></tr></table>" ++ "<form action=\"https://evil.example/\"><p>양식 안의 본문</p><input name=\"q\"></form>" ++ "<xmp><img src=x onerror=alert(1)></xmp><!-- <img src=x onerror=alert(2)> --></body></html>";var eml = string.Join("\r\n",$"From: {EncodedWord("보낸이", "EUC-KR")} <[email protected]>","To: [email protected], \"Second\" <[email protected]>",@@ -132,6 +135,14 @@ public class EmlTests(EngineFixture fx) : IClassFixture<EngineFixture>Assert.DoesNotContain("tracker.example", html);Assert.DoesNotContain("javascript:", html);Assert.DoesNotContain("<svg", html);+ Assert.Contains("bgcolor=\"#EEEEEE\"", html); // old-style mail formatting stays+ Assert.Contains("valign=\"top\"", html);+ Assert.Contains("<font color=\"#336699\">", html);+ Assert.Contains("<p>양식 안의 본문</p>", html); // the form's text stays, the form and its controls go+ Assert.DoesNotContain("<form", html);+ Assert.DoesNotContain("<input", html);+ Assert.DoesNotContain("<xmp", html);+ Assert.DoesNotContain("<!--", html);Assert.Contains("Content-Security-Policy", html);}