aboutsummaryrefslogtreecommitdiff
path: root/plugins/SessionProvider/src/Security/WebSessionSecMiddleware.cs
blob: e8578a8ff6260d1cf3fa3f7c1afdcf00a86f8ca0 (plain)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* Copyright (c) 2024 Vaughn Nugent
* 
* Library: VNLib
* Package: SessionProvider
* File: WebSessionSecMiddleware.cs 
*
* WebSessionSecMiddleware.cs is part of SessionProvider which is part of the larger 
* VNLib collection of libraries and utilities.
*
* SessionProvider is free software: you can redistribute it and/or modify 
* it under the terms of the GNU Affero General Public License as 
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* SessionProvider is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program.  If not, see https://www.gnu.org/licenses/.
*/

using System;
using System.Threading.Tasks;
using System.Security.Authentication;
using System.Text.Json.Serialization;

using VNLib.Utils.Logging;
using VNLib.Plugins.Essentials.Extensions;
using VNLib.Plugins.Extensions.Loading;
using VNLib.Plugins.Essentials.Middleware;

namespace VNLib.Plugins.Essentials.Sessions
{
    [ConfigurationName("web")]
    [MiddlewareImpl(MiddlewareImplOptions.SecurityCritical)]
    internal sealed class WebSessionSecMiddleware(PluginBase plugin, IConfigScope config) : IHttpMiddleware
    {
        private readonly ILogProvider _log = plugin.Log.CreateScope("Session-Sec");
        private readonly SecConfig _secConfig = config.Deserialze<SecConfig>();

        ///<inheritdoc/>
        public ValueTask<FileProcessArgs> ProcessAsync(HttpEntity entity)
        {
            ref readonly SessionInfo session = ref entity.Session;

            if (session.IsSet)
            {
                /*
                * Check if the session was established over a secure connection, 
                * and if the current connection is insecure, redirect them to a 
                * secure connection.
                */
                if (session.SecurityProcol > SslProtocols.None && !entity.IsSecure)
                {
                    //Redirect the client to https
                    UriBuilder ub = new(entity.Server.RequestUri)
                    {
                        Scheme = Uri.UriSchemeHttps
                    };

                    _log.Debug("Possbile session TLS downgrade detected, redirecting {con} to secure endpoint", entity.TrustedRemoteIp);

                    //Redirect
                    entity.Redirect(RedirectType.Moved, ub.Uri);
                    return ValueTask.FromResult(FileProcessArgs.VirtualSkip);
                }

                //If session is not new, then verify it matches stored credentials
                if (!session.IsNew && session.SessionType == SessionType.Web)
                {
                    if (_secConfig.EnfoceStrictTlsProtocol)
                    {
                        //Try to prevent security downgrade attacks
                        if (!(session.IPMatch && session.SecurityProcol <= entity.Server.GetSslProtocol()))
                        {
                            _log.Debug("Possible TLS downgrade attack stopeed from connection {con}", entity.TrustedRemoteIp);
                            return ValueTask.FromResult(FileProcessArgs.Deny);
                        }
                    }
                }
            }

            return ValueTask.FromResult(FileProcessArgs.Continue);
        }
      
        sealed class SecConfig
        {

            [JsonPropertyName("strict_tls_protocol")]
            public bool EnfoceStrictTlsProtocol { get; set; } = true;
        }
    }
}