OpenVPN 3 Core Library
Loading...
Searching...
No Matches
headredact.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12#pragma once
13
14#include <string>
15#include <regex>
16
17namespace openvpn::HTTP {
18
19inline std::string headers_redact(const std::string &headers)
20{
21#ifdef OPENVPN_HTTP_HEADERS_NO_REDACT
22 return headers;
23#else
24 // Alternative regex implementation:
25 // static const std::regex re(R"((authorization[\s:=]+basic\s+)([^\s]+))", std::regex_constants::ECMAScript | std::regex_constants::icase);
26 // return std::regex_replace(headers, re, "$1[REDACTED]");
27 std::stringstream result;
28
29 std::istringstream iss(headers);
30
31 for (std::string line; std::getline(iss, line);)
32 {
33 if (auto authpos = line.find("Authorization: "); authpos != std::string::npos)
34 {
35 auto auth = line.substr(authpos);
36 auto argument = auth.substr(auth.find(' ') + 1);
37 std::string authtype;
38 if (auto arg1 = argument.find(' '); arg1 != std::string::npos)
39 {
40 authtype = argument.substr(0, arg1);
41 }
42 result << line.substr(0, authpos) << "Authorization: " << authtype << " [REDACTED]\r" << std::endl;
43 }
44 else if ((authpos = line.find("authorization=basic ")) != std::string::npos)
45 {
46 result << line.substr(0, authpos) << "authorization=basic [REDACTED]\r" << std::endl;
47 }
48 else
49 {
50 result << line << std::endl;
51 }
52 }
53 return result.str();
54#endif
55}
56
57} // namespace openvpn::HTTP
std::string headers_redact(const std::string &headers)