forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ClickHouse#51038 from evillique/headers-blacklist
Add HTTP header filtering
- Loading branch information
Showing
19 changed files
with
177 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#include <Common/HTTPHeaderFilter.h> | ||
#include <Common/StringUtils/StringUtils.h> | ||
#include <Common/Exception.h> | ||
|
||
#include <re2/re2.h> | ||
|
||
namespace DB | ||
{ | ||
|
||
namespace ErrorCodes | ||
{ | ||
extern const int BAD_ARGUMENTS; | ||
} | ||
|
||
void HTTPHeaderFilter::checkHeaders(const HTTPHeaderEntries & entries) const | ||
{ | ||
std::lock_guard guard(mutex); | ||
|
||
for (const auto & entry : entries) | ||
{ | ||
if (forbidden_headers.contains(entry.name)) | ||
throw Exception(ErrorCodes::BAD_ARGUMENTS, "HTTP header \"{}\" is forbidden in configuration file, " | ||
"see <http_forbid_headers>", entry.name); | ||
|
||
for (const auto & header_regex : forbidden_headers_regexp) | ||
if (re2::RE2::FullMatch(entry.name, header_regex)) | ||
throw Exception(ErrorCodes::BAD_ARGUMENTS, "HTTP header \"{}\" is forbidden in configuration file, " | ||
"see <http_forbid_headers>", entry.name); | ||
} | ||
} | ||
|
||
void HTTPHeaderFilter::setValuesFromConfig(const Poco::Util::AbstractConfiguration & config) | ||
{ | ||
std::lock_guard guard(mutex); | ||
|
||
if (config.has("http_forbid_headers")) | ||
{ | ||
std::vector<std::string> keys; | ||
config.keys("http_forbid_headers", keys); | ||
|
||
for (const auto & key : keys) | ||
{ | ||
if (startsWith(key, "header_regexp")) | ||
forbidden_headers_regexp.push_back(config.getString("http_forbid_headers." + key)); | ||
else if (startsWith(key, "header")) | ||
forbidden_headers.insert(config.getString("http_forbid_headers." + key)); | ||
} | ||
} | ||
else | ||
{ | ||
forbidden_headers.clear(); | ||
forbidden_headers_regexp.clear(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#pragma once | ||
|
||
#include <IO/HTTPHeaderEntries.h> | ||
#include <Poco/Util/AbstractConfiguration.h> | ||
#include <vector> | ||
#include <unordered_set> | ||
#include <mutex> | ||
|
||
|
||
namespace DB | ||
{ | ||
|
||
class HTTPHeaderFilter | ||
{ | ||
public: | ||
|
||
void setValuesFromConfig(const Poco::Util::AbstractConfiguration & config); | ||
void checkHeaders(const HTTPHeaderEntries & entries) const; | ||
|
||
private: | ||
std::unordered_set<std::string> forbidden_headers; | ||
std::vector<std::string> forbidden_headers_regexp; | ||
|
||
mutable std::mutex mutex; | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<clickhouse> | ||
<http_forbid_headers> | ||
<header>exact_header</header> | ||
<header_regexp>(?i)(case_insensitive_header)</header_regexp> | ||
</http_forbid_headers> | ||
</clickhouse> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
-- Tags: no-fasttest | ||
-- Tag no-fasttest: Depends on AWS | ||
|
||
SELECT * FROM url('https://localhost:8123/', LineAsString, headers('exact_header' = 'value')); -- { serverError BAD_ARGUMENTS } | ||
SELECT * FROM url('https://localhost:8123/', LineAsString, headers('cAsE_INSENSITIVE_header' = 'value')); -- { serverError BAD_ARGUMENTS } | ||
SELECT * FROM url('https://localhost:8123/', LineAsString, headers('random_header' = 'value')) FORMAT Null; | ||
|
||
SELECT * FROM urlCluster('test_cluster_two_shards_localhost', 'https://localhost:8123/', LineAsString, headers('exact_header' = 'value')); -- { serverError BAD_ARGUMENTS } | ||
SELECT * FROM urlCluster('test_cluster_two_shards_localhost', 'https://localhost:8123/', LineAsString, headers('cAsE_INSENSITIVE_header' = 'value')); -- { serverError BAD_ARGUMENTS } | ||
SELECT * FROM urlCluster('test_cluster_two_shards_localhost', 'https://localhost:8123/', LineAsString, headers('random_header' = 'value')) FORMAT Null; | ||
|
||
SELECT * FROM s3('https://localhost:8123/123/4', LineAsString, headers('exact_header' = 'value')); -- { serverError BAD_ARGUMENTS } | ||
SELECT * FROM s3('https://localhost:8123/123/4', LineAsString, headers('cAsE_INSENSITIVE_header' = 'value')); -- { serverError BAD_ARGUMENTS } | ||
SELECT * FROM s3('https://localhost:8123/123/4', LineAsString, headers('random_header' = 'value')); -- { serverError S3_ERROR } | ||
|
||
SELECT * FROM s3Cluster('test_cluster_two_shards_localhost', 'https://localhost:8123/123/4', LineAsString, headers('exact_header' = 'value')); -- { serverError BAD_ARGUMENTS } | ||
SELECT * FROM s3Cluster('test_cluster_two_shards_localhost', 'https://localhost:8123/123/4', LineAsString, headers('cAsE_INSENSITIVE_header' = 'value')); -- { serverError BAD_ARGUMENTS } | ||
SELECT * FROM s3Cluster('test_cluster_two_shards_localhost', 'https://localhost:8123/123/4', LineAsString, headers('random_header' = 'value')); -- { serverError S3_ERROR } |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
-- Tags: no-fasttest | ||
-- Tag no-fasttest: Depends on AWS | ||
|
||
SELECT * FROM s3(headers('random_header' = 'value')); -- { serverError NUMBER_OF_ARGUMENTS_DOESNT_MATCH } | ||
SELECT * FROM s3Cluster('test_cluster_two_shards_localhost', headers('random_header' = 'value')); -- { serverError NUMBER_OF_ARGUMENTS_DOESNT_MATCH } |