summary refs log tree commit diff
path: root/ReferenceClientProxyImplementation/Patches/Implementations/HTMLPatches/StripNoncesPatch.cs
blob: 6c5f312e21d8770540ff192c8e9d173b78fc3198 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System.Text;
using System.Text.RegularExpressions;
using ReferenceClientProxyImplementation.Configuration;

namespace ReferenceClientProxyImplementation.Patches.Implementations.HTMLPatches;

public partial class StripNoncesPatch(ProxyConfiguration config) : IPatch {
    public int GetOrder() => 0;

    public string GetName() => "Strip nonces/integrity from html";
    public bool Applies(string relativeName, byte[] content) => relativeName is "app.html" or "developers.html" or "popout.html";

    public async Task<byte[]> Execute(string _, byte[] content) {
        var stringContent = Encoding.UTF8.GetString(content);
        stringContent = HtmlScriptIntegrityRegex().Replace(
            HtmlScriptNonceRegex().Replace(
                JsElementNonceRegex().Replace(
                    stringContent,
                    ""
                ),
                ""
            ),
            ""
        );
        return Encoding.UTF8.GetBytes(stringContent);
    }

    [GeneratedRegex("\\snonce=\"[a-zA-Z0-9+/=]+\"")]
    private static partial Regex HtmlScriptNonceRegex();

    [GeneratedRegex("\\w.nonce='[a-zA-Z0-9+/=]+';")]
    private static partial Regex JsElementNonceRegex();

    [GeneratedRegex(@"\sintegrity=""[a-zA-Z0-9+/=\-\s]+""")]
    private static partial Regex HtmlScriptIntegrityRegex();
}