Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add copy_data processor to extract request data into derivatives #361

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ perf/test_files/breakdown.numbers
.vscode
*.swp
*.swo
*.swn
11 changes: 11 additions & 0 deletions src/builder/processor_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "indexer.hpp"
#include "parser/specification.hpp"
#include "processor/base.hpp"
#include "processor/copy_data.hpp"
#include "processor/extract_schema.hpp"
#include "processor/fingerprint.hpp"
#include "scanner.hpp"
Expand Down Expand Up @@ -81,6 +82,14 @@ template <> struct typed_processor_builder<session_fingerprint> {
}
};

template <> struct typed_processor_builder<copy_data_processor> {
std::shared_ptr<base_processor> build(const auto &spec)
{
return std::make_shared<copy_data_processor>(
spec.id, spec.expr, spec.mappings, spec.evaluate, spec.output);
}
};

template <typename T, typename Spec, typename Scanners>
concept has_build_with_scanners =
requires(typed_processor_builder<T> b, Spec spec, Scanners scanners) {
Expand Down Expand Up @@ -117,6 +126,8 @@ template <typename T>
return build_with_type<http_network_fingerprint>(*this, scanners);
case processor_type::session_fingerprint:
return build_with_type<session_fingerprint>(*this, scanners);
case processor_type::copy_data:
return build_with_type<copy_data_processor>(*this, scanners);
default:
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/builder/processor_builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ namespace ddwaf {

enum class processor_type : unsigned {
extract_schema,
// Reserved
http_endpoint_fingerprint,
http_network_fingerprint,
http_header_fingerprint,
session_fingerprint,
copy_data,
};

struct processor_builder {
Expand Down
8 changes: 7 additions & 1 deletion src/parser/processor_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "parser/parser.hpp"
#include "parser/specification.hpp"
#include "processor/base.hpp"
#include "processor/copy_data.hpp"
#include "processor/extract_schema.hpp"
#include "processor/fingerprint.hpp"
#include "semver.hpp"
Expand Down Expand Up @@ -106,6 +107,8 @@ processor_container parse_processors(
type = processor_type::http_header_fingerprint;
} else if (generator_id == "session_fingerprint") {
type = processor_type::session_fingerprint;
} else if (generator_id == "copy_data") {
type = processor_type::copy_data;
} else {
DDWAF_WARN("Unknown generator: {}", generator_id);
info.add_failed(id, "unknown generator '" + generator_id + "'");
Expand All @@ -130,9 +133,12 @@ processor_container parse_processors(
} else if (type == processor_type::http_network_fingerprint) {
mappings = parse_processor_mappings(
mappings_vec, addresses, http_network_fingerprint::param_names);
} else {
} else if (type == processor_type::session_fingerprint) {
mappings = parse_processor_mappings(
mappings_vec, addresses, session_fingerprint::param_names);
} else {
mappings = parse_processor_mappings(
mappings_vec, addresses, copy_data_processor::param_names);
}

std::vector<reference_spec> scanners;
Expand Down
41 changes: 41 additions & 0 deletions src/processor/copy_data.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Unless explicitly stated otherwise all files in this repository are
// dual-licensed under the Apache-2.0 License or BSD-3-Clause License.
//
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2021 Datadog, Inc.

#include <cstdlib>
#include <cstring>
#include <string_view>

#include "processor/base.hpp"

namespace ddwaf {

class copy_data_processor : public structured_processor<copy_data_processor> {
public:
static constexpr std::array<std::string_view, 1> param_names{"input"};

copy_data_processor(std::string id, std::shared_ptr<expression> expr,
std::vector<processor_mapping> mappings, bool evaluate, bool output)
: structured_processor(
std::move(id), std::move(expr), std::move(mappings), evaluate, output)
{}

// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
std::pair<ddwaf_object, object_store::attribute> eval_impl(
const unary_argument<const ddwaf_object *> &input, processor_cache & /*cache*/,
ddwaf::timer & /*deadline*/) const
{
if (input.value == nullptr) {
return {};
}

// Objects generated by this post-processors must always be non-ephemeral
// as there would be no way to distinguish between derivatives generated
// across multiple calls.
return {ddwaf::object::clone(input.value), object_store::attribute::none};
}
};

} // namespace ddwaf
9 changes: 5 additions & 4 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Copyright 2021 Datadog, Inc.

#include <cstdint>
#include <list>
#include <deque>
#include <utility>

#include "ddwaf.h"
Expand Down Expand Up @@ -46,17 +46,18 @@ void clone_helper(const ddwaf_object &source, ddwaf_object &destination)
}
}

ddwaf_object clone(ddwaf_object *input)
ddwaf_object clone(const ddwaf_object *input)
{
ddwaf_object tmp;
ddwaf_object_invalid(&tmp);

ddwaf_object copy;
std::list<std::pair<ddwaf_object *, ddwaf_object *>> queue;
std::deque<std::pair<ddwaf_object *, ddwaf_object *>> queue;

clone_helper(*input, copy);
if (is_container(input)) {
queue.emplace_front(input, &copy);
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
queue.emplace_front(const_cast<ddwaf_object *>(input), &copy);
}

while (!queue.empty()) {
Expand Down
2 changes: 1 addition & 1 deletion src/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ inline bool is_invalid_or_null(const ddwaf_object *obj)
return obj != nullptr && (obj->type == DDWAF_OBJ_INVALID || obj->type == DDWAF_OBJ_NULL);
}

ddwaf_object clone(ddwaf_object *input);
ddwaf_object clone(const ddwaf_object *input);
} // namespace object

// NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
Expand Down