|
| 1 | +using System; |
| 2 | +using ToSic.Razor.Internals; |
| 3 | + |
| 4 | +namespace ToSic.Razor.Wip |
| 5 | +{ |
| 6 | + /// <summary> |
| 7 | + /// TODO: WIP |
| 8 | + /// https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html |
| 9 | + /// https://cheatsheetseries.owasp.org/cheatsheets/DotNet_Security_Cheat_Sheet.html#a7-cross-site-scripting-xss |
| 10 | + /// </summary> |
| 11 | + public class XssPrevention |
| 12 | + { |
| 13 | + /// <summary> |
| 14 | + /// Output Encoding for "JSON-LD Contexts" |
| 15 | + /// https://w3c.github.io/json-ld-syntax/#restrictions-for-contents-of-json-ld-script-elements |
| 16 | + /// https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html#output-encoding-for-javascript-contexts |
| 17 | + /// Authors should avoid using character sequences in scripts embedded in HTML which may be confused with a |
| 18 | + /// comment-open, script-open, comment-close, or script-close. |
| 19 | + /// Partial encode < and > characters with the \uXXXX unicode encoding format (X = Integer). |
| 20 | + /// </summary> |
| 21 | + /// <param name="unsafeJsonLd"></param> |
| 22 | + /// <returns></returns> |
| 23 | + public static string JsonLdScriptEncoding(string unsafeJsonLd) => unsafeJsonLd |
| 24 | + .Replace("<!--", "<", @"\u003C", StringComparison.OrdinalIgnoreCase) |
| 25 | + .Replace("<script", "<", @"\u003C", StringComparison.OrdinalIgnoreCase) |
| 26 | + .Replace("-->", ">", @"\u003E", StringComparison.OrdinalIgnoreCase) |
| 27 | + .Replace("</script", "<", @"\u003C", StringComparison.OrdinalIgnoreCase); |
| 28 | + |
| 29 | + ///// <summary> |
| 30 | + ///// Output Encoding for "HTML Contexts" |
| 31 | + ///// https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html#output-encoding-for-html-contexts |
| 32 | + ///// Convert & to & |
| 33 | + ///// Convert < to < |
| 34 | + ///// Convert > to > |
| 35 | + ///// Convert " to " |
| 36 | + ///// Convert ' to ' |
| 37 | + ///// Convert / to / |
| 38 | + ///// </summary> |
| 39 | + ///// <param name="unsafeHtml"></param> |
| 40 | + ///// <returns></returns> |
| 41 | + //internal static string HtmlEntityEncoding(string unsafeHtml) => unsafeHtml |
| 42 | + // .Replace("&", "&") // TODO: encode & only in case that unsafeHtml is not already entity encoded |
| 43 | + // .Replace("<", "<") |
| 44 | + // .Replace(">", ">") |
| 45 | + // .Replace("\"", """) |
| 46 | + // .Replace("'", "'"); |
| 47 | + |
| 48 | + ///// <summary> |
| 49 | + ///// TODO: Output Encoding for "HTML Attribute Contexts" |
| 50 | + ///// https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html#output-encoding-for-html-attribute-contexts |
| 51 | + ///// Except for alphanumeric characters, encode all characters with the |
| 52 | + ///// HTML Entity &#xHH; format, including spaces. (HH = Hex Value) |
| 53 | + ///// </summary> |
| 54 | + ///// <param name="unsafeAttributeValue"></param> |
| 55 | + ///// <returns></returns> |
| 56 | + //internal static string HtmlAttributeEncoding(string unsafeAttributeValue) => unsafeAttributeValue; |
| 57 | + |
| 58 | + ///// <summary> |
| 59 | + ///// TODO: Output Encoding for "URL Contexts" |
| 60 | + ///// https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html#output-encoding-for-url-contexts |
| 61 | + ///// Standard percent encoding. URL encoding should only be used to encode parameter values, |
| 62 | + ///// not the entire URL or path fragments of a URL. |
| 63 | + ///// </summary> |
| 64 | + ///// <param name="unsafeParameterValue"></param> |
| 65 | + ///// <returns></returns> |
| 66 | + //internal static string UrlParameterEncoding(string unsafeParameterValue) => unsafeParameterValue; |
| 67 | + |
| 68 | + ///// <summary> |
| 69 | + ///// TODO: Output Encoding for "JavaScript Contexts" |
| 70 | + ///// https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html#output-encoding-for-javascript-contexts |
| 71 | + ///// Except for alphanumeric characters, encode all characters with the |
| 72 | + ///// \uXXXX unicode encoding format (X = Integer). |
| 73 | + ///// </summary> |
| 74 | + ///// <param name="unsafeJavaScript"></param> |
| 75 | + ///// <returns></returns> |
| 76 | + //internal static string JavaScriptEncoding(string unsafeJavaScript) => unsafeJavaScript; |
| 77 | + |
| 78 | + ///// <summary> |
| 79 | + ///// TODO: Output Encoding for "CSS Contexts" |
| 80 | + ///// https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html#output-encoding-for-css-contexts |
| 81 | + ///// CSS encoding supports \XX and \XXXXXX. Using a two character encode can cause problems if |
| 82 | + ///// the next character continues the encode sequence. There are two solutions: |
| 83 | + ///// (a) Add a space after the CSS encode (will be ignored by the CSS parser) |
| 84 | + ///// (b) use the full amount of CSS encoding possible by zero padding the value. |
| 85 | + ///// </summary> |
| 86 | + ///// <param name="unsafeCss"></param> |
| 87 | + ///// <returns></returns> |
| 88 | + //internal static string CssHexEncoding(string unsafeCss) => unsafeCss; |
| 89 | + } |
| 90 | +} |
0 commit comments