Skip to content

Commit 2843ade

Browse files
authored
Merge back changes 04122023 (#2404)
1 parent 3166775 commit 2843ade

File tree

23 files changed

+366
-94
lines changed

23 files changed

+366
-94
lines changed

appsec/src/helper/client.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
//
44
// This product includes software developed at Datadog
55
// (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.
6+
67
#include <chrono>
78
#include <spdlog/spdlog.h>
89
#include <stdexcept>
910
#include <string>
1011
#include <thread>
1112

13+
#include "base64.h"
1214
#include "client.hpp"
15+
#include "compression.hpp"
1316
#include "exception.hpp"
1417
#include "network/broker.hpp"
1518
#include "network/proto.hpp"
@@ -150,7 +153,7 @@ bool client::handle_command(const network::client_init::request &command)
150153
auto &&eng_settings = command.engine_settings;
151154
DD_STDLOG(DD_STDLOG_STARTUP);
152155

153-
std::map<std::string_view, std::string> meta;
156+
std::map<std::string, std::string> meta;
154157
std::map<std::string_view, double> metrics;
155158

156159
std::vector<std::string> errors;
@@ -433,6 +436,7 @@ bool client::handle_command(network::request_shutdown::request &command)
433436
auto free_ctx = defer([this]() { this->context_.reset(); });
434437

435438
auto response = std::make_shared<network::request_shutdown::response>();
439+
436440
try {
437441
auto sampler = service_->get_schema_sampler();
438442
std::optional<sampler::scope> scope;
@@ -467,6 +471,16 @@ bool client::handle_command(network::request_shutdown::request &command)
467471

468472
response->triggers = std::move(res->events);
469473
response->force_keep = res->force_keep;
474+
for (const auto &[key, value] : res->schemas) {
475+
std::string schema = value;
476+
if (value.length() > max_plain_schema_allowed) {
477+
auto encoded = compress(schema);
478+
if (encoded) {
479+
schema = base64_encode(encoded.value(), false);
480+
}
481+
}
482+
response->meta.emplace(key, std::move(schema));
483+
}
470484

471485
DD_STDLOG(DD_STDLOG_ATTACK_DETECTED);
472486
} else {

appsec/src/helper/client.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ namespace dds {
2020

2121
class client {
2222
public:
23+
// Below this limit the encoding+compression might result on a longer string
24+
static constexpr int max_plain_schema_allowed = 260;
2325
client(std::shared_ptr<service_manager> service_manager,
2426
network::base_broker::ptr &&broker)
2527
: service_manager_(std::move(service_manager)),

appsec/src/helper/engine.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void engine::subscribe(const subscriber::ptr &sub)
2626
}
2727

2828
void engine::update(engine_ruleset &ruleset,
29-
std::map<std::string_view, std::string> &meta,
29+
std::map<std::string, std::string> &meta,
3030
std::map<std::string_view, double> &metrics)
3131
{
3232
auto new_actions =
@@ -75,6 +75,7 @@ std::optional<engine::result> engine::context::publish(parameter &&param)
7575

7676
std::vector<std::string> event_data;
7777
std::unordered_set<std::string> event_actions;
78+
std::map<std::string, std::string> schemas;
7879

7980
for (auto &sub : common_->subscribers) {
8081
auto it = listeners_.find(sub);
@@ -88,6 +89,7 @@ std::optional<engine::result> engine::context::publish(parameter &&param)
8889
std::make_move_iterator(event->data.begin()),
8990
std::make_move_iterator(event->data.end()));
9091
event_actions.merge(event->actions);
92+
schemas.merge(event->schemas);
9193
}
9294
} catch (std::exception &e) {
9395
SPDLOG_ERROR("subscriber failed: {}", e.what());
@@ -98,7 +100,8 @@ std::optional<engine::result> engine::context::publish(parameter &&param)
98100
return std::nullopt;
99101
}
100102

101-
dds::engine::result res{action_type::record, {}, std::move(event_data)};
103+
dds::engine::result res{
104+
action_type::record, {}, std::move(event_data), std::move(schemas)};
102105
// Currently the only action the extension can perform is block
103106
if (!event_actions.empty()) {
104107
// The extension can only handle one action, so we pick the first one
@@ -119,7 +122,7 @@ std::optional<engine::result> engine::context::publish(parameter &&param)
119122
}
120123

121124
void engine::context::get_meta_and_metrics(
122-
std::map<std::string_view, std::string> &meta,
125+
std::map<std::string, std::string> &meta,
123126
std::map<std::string_view, double> &metrics)
124127
{
125128
for (const auto &[subscriber, listener] : listeners_) {
@@ -231,7 +234,7 @@ engine::action_map engine::parse_actions(
231234
}
232235

233236
engine::ptr engine::from_settings(const dds::engine_settings &eng_settings,
234-
std::map<std::string_view, std::string> &meta,
237+
std::map<std::string, std::string> &meta,
235238
std::map<std::string_view, double> &metrics)
236239

237240
{

appsec/src/helper/engine.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class engine {
5050
action_type type;
5151
std::unordered_map<std::string, std::string> parameters;
5252
std::vector<std::string> events;
53+
std::map<std::string, std::string> schemas;
5354
bool force_keep;
5455
};
5556

@@ -77,7 +78,7 @@ class engine {
7778

7879
std::optional<result> publish(parameter &&param);
7980
// NOLINTNEXTLINE(google-runtime-references)
80-
void get_meta_and_metrics(std::map<std::string_view, std::string> &meta,
81+
void get_meta_and_metrics(std::map<std::string, std::string> &meta,
8182
std::map<std::string_view, double> &metrics);
8283

8384
protected:
@@ -94,7 +95,7 @@ class engine {
9495
virtual ~engine() = default;
9596

9697
static engine::ptr from_settings(const dds::engine_settings &eng_settings,
97-
std::map<std::string_view, std::string> &meta,
98+
std::map<std::string, std::string> &meta,
9899
std::map<std::string_view, double> &metrics);
99100

100101
static auto create(
@@ -111,7 +112,7 @@ class engine {
111112
// Update is not thread-safe, although only one remote config client should
112113
// be able to update it so in practice it should not be a problem.
113114
virtual void update(engine_ruleset &ruleset,
114-
std::map<std::string_view, std::string> &meta,
115+
std::map<std::string, std::string> &meta,
115116
std::map<std::string_view, double> &metrics);
116117

117118
// Only exposed for testing purposes

appsec/src/helper/network/proto.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ struct client_init {
126126
std::string version{dds::php_ddappsec_version};
127127
std::vector<std::string> errors;
128128

129-
std::map<std::string_view, std::string> meta;
129+
std::map<std::string, std::string> meta;
130130
std::map<std::string_view, double> metrics;
131131

132132
MSGPACK_DEFINE(status, version, errors, meta, metrics);
@@ -278,7 +278,7 @@ struct request_shutdown {
278278

279279
bool force_keep;
280280

281-
std::map<std::string_view, std::string> meta;
281+
std::map<std::string, std::string> meta;
282282
std::map<std::string_view, double> metrics;
283283
std::map<std::string_view, std::string> schemas;
284284

appsec/src/helper/remote_config/listeners/engine_listener.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void engine_listener::commit()
8080
}
8181

8282
// TODO find a way to provide this information to the service
83-
std::map<std::string_view, std::string> meta;
83+
std::map<std::string, std::string> meta;
8484
std::map<std::string_view, double> metrics;
8585

8686
engine_ruleset ruleset = dds::engine_ruleset(std::move(ruleset_));

appsec/src/helper/sampler.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class sampler {
3333
public:
3434
explicit scope(std::atomic<bool> &concurrent) : concurrent_(&concurrent)
3535
{
36-
*concurrent_ = true;
36+
concurrent_->store(true, std::memory_order_relaxed);
3737
}
3838

3939
scope(const scope &) = delete;
@@ -54,7 +54,7 @@ class sampler {
5454
~scope()
5555
{
5656
if (concurrent_ != nullptr) {
57-
*concurrent_ = false;
57+
concurrent_->store(false, std::memory_order_relaxed);
5858
}
5959
}
6060

@@ -73,7 +73,7 @@ class sampler {
7373
result = {scope{concurrent_}};
7474
}
7575

76-
if (request_ < UINT_MAX) {
76+
if (request_ < std::numeric_limits<unsigned>::max()) {
7777
request_++;
7878
} else {
7979
request_ = 1;

appsec/src/helper/service.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ service::service(std::shared_ptr<engine> engine,
3636
service::ptr service::from_settings(service_identifier &&id,
3737
const dds::engine_settings &eng_settings,
3838
const remote_config::settings &rc_settings,
39-
std::map<std::string_view, std::string> &meta,
39+
std::map<std::string, std::string> &meta,
4040
std::map<std::string_view, double> &metrics, bool dynamic_enablement)
4141
{
4242
auto engine_ptr = engine::from_settings(eng_settings, meta, metrics);

appsec/src/helper/service.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class service {
4141
static service::ptr from_settings(service_identifier &&id,
4242
const dds::engine_settings &eng_settings,
4343
const remote_config::settings &rc_settings,
44-
std::map<std::string_view, std::string> &meta,
44+
std::map<std::string, std::string> &meta,
4545
std::map<std::string_view, double> &metrics, bool dynamic_enablement);
4646

4747
virtual void register_runtime_id(const std::string &id)
@@ -70,7 +70,10 @@ class service {
7070
return service_config_;
7171
}
7272

73-
std::shared_ptr<sampler> get_schema_sampler() { return schema_sampler_; }
73+
[[nodiscard]] std::shared_ptr<sampler> get_schema_sampler()
74+
{
75+
return schema_sampler_;
76+
}
7477

7578
protected:
7679
std::shared_ptr<engine> engine_{};

appsec/src/helper/service_manager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace dds {
1010
std::shared_ptr<service> service_manager::create_service(
1111
service_identifier &&id, const engine_settings &settings,
1212
const remote_config::settings &rc_settings,
13-
std::map<std::string_view, std::string> &meta,
13+
std::map<std::string, std::string> &meta,
1414
std::map<std::string_view, double> &metrics, bool dynamic_enablement)
1515
{
1616
const std::lock_guard guard{mutex_};

0 commit comments

Comments
 (0)