Copilot commented on code in PR #13807: URL: https://github.com/apache/trafficserver/pull/13807#discussion_r4200756162
########## plugins/experimental/jax_fingerprint/config.cc: ########## @@ -0,0 +1,212 @@ +/** @file + + @section license License + + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + */ + +#include "config.h" + +#ifdef ENABLE_JAX_METHOD_JA4 +#include "ja4/method.h" +#endif +#ifdef ENABLE_JAX_METHOD_JA4H +#include "ja4h/method.h" +#endif +#ifdef ENABLE_JAX_METHOD_JA3 +#include "ja3/method.h" +#endif + +#include <yaml-cpp/yaml.h> + +#include <algorithm> +#include <initializer_list> +#include <string_view> + +namespace +{ +constexpr Method const *METHODS[] = { +#ifdef ENABLE_JAX_METHOD_JA4 + &ja4::method, +#endif +#ifdef ENABLE_JAX_METHOD_JA4H + &ja4h::method, +#endif +#ifdef ENABLE_JAX_METHOD_JA3 + &ja3::method, +#endif +}; + +constexpr std::string_view ROOT_KEY = "jax_fingerprint"; +constexpr std::string_view FINGERPRINTS_KEY = "fingerprints"; + +bool +check_keys(const YAML::Node &node, std::initializer_list<std::string_view> allowed, std::string_view scope) +{ + for (auto const &entry : node) { + auto key = entry.first.as<std::string>(); + if (std::find(allowed.begin(), allowed.end(), key) == allowed.end()) { + TSError("[%s] Unknown key '%s' in %.*s", PLUGIN_NAME, key.c_str(), static_cast<int>(scope.size()), scope.data()); + return false; + } + } + return true; +} + +bool +read_string(const YAML::Node &node, const char *key, std::string &value) +{ + if (YAML::Node field = node[key]; field) { + if (!field.IsScalar() || field.Scalar().empty()) { + TSError("[%s] '%s' must be a non-empty string", PLUGIN_NAME, key); + return false; + } + value = field.Scalar(); + } + return true; +} + +bool +parse_fingerprint(const YAML::Node &node, PluginType plugin_type, PluginConfig &config) +{ + if (!node.IsMap()) { + TSError("[%s] Each '%.*s' entry must be a map", PLUGIN_NAME, static_cast<int>(FINGERPRINTS_KEY.size()), + FINGERPRINTS_KEY.data()); + return false; + } + + if (!check_keys(node, + {"method", "standalone", "mode", "header", "via_header", "log_filename", "log_field", "servernames", "export"}, + "fingerprint entry")) { + return false; + } + + config.plugin_type = plugin_type; + + std::string method_name; + if (!read_string(node, "method", method_name) || method_name.empty()) { + TSError("[%s] Each fingerprint entry requires a 'method'", PLUGIN_NAME); + return false; + } + auto method = std::find_if(std::begin(METHODS), std::end(METHODS), [&](Method const *m) { return m->name == method_name; }); + if (method == std::end(METHODS)) { + TSError("[%s] Unknown method: %s", PLUGIN_NAME, method_name.c_str()); + return false; + } + config.method = **method; + + if (YAML::Node standalone = node["standalone"]; standalone) { + config.standalone = standalone.as<bool>(); + } + + std::string mode; + if (!read_string(node, "mode", mode)) { + return false; + } + if (mode.empty() || mode == "overwrite") { + config.mode = Mode::OVERWRITE; + } else if (mode == "keep") { + config.mode = Mode::KEEP; + } else if (mode == "append") { + config.mode = Mode::APPEND; + } else { + TSError("[%s] Unknown mode: %s", PLUGIN_NAME, mode.c_str()); + return false; + } + + if (!read_string(node, "header", config.header_name) || !read_string(node, "via_header", config.via_header_name) || + !read_string(node, "log_filename", config.log_filename) || !read_string(node, "log_field", config.log_symbol) || + !read_string(node, "export", config.export_name)) { + return false; + } + + if (!config.log_symbol.empty() && plugin_type == PluginType::REMAP) { + TSError("[%s] 'log_field' is only supported for the plugin loaded via plugin.config", PLUGIN_NAME); + return false; + } + + if (YAML::Node servernames = node["servernames"]; servernames) { + if (!servernames.IsSequence()) { + TSError("[%s] 'servernames' must be a list of server names", PLUGIN_NAME); + return false; + } + for (auto const &servername : servernames) { + config.servernames.emplace(servername.as<std::string>()); Review Comment: `servernames: [""]` passes validation, but for JA3/JA4 it causes `handle_client_hello` to skip every connection: a nonempty SNI cannot match the empty entry, and missing SNI is also skipped. Reject empty or non-scalar server-name elements rather than accepting an allowlist that silently disables fingerprint generation. ########## plugins/experimental/jax_fingerprint/plugin.cc: ########## @@ -459,88 +346,79 @@ TSRemapInit(TSRemapInterface *api_info, char *errbuf, int errbuf_size) } TSReturnCode -TSRemapNewInstance(int argc, char *argv[], void **ih, char * /* errbuf ATS_UNUSED */, int /* errbuf_size ATS_UNUSED */) +TSRemapNewInstance(int argc, char *argv[], void **ih, char *errbuf, int errbuf_size) { - Dbg(dbg_ctl, "New instance for client matching %s to %s", argv[0], argv[1]); - auto owned_config = std::make_unique<PluginConfig>(); - owned_config->plugin_type = PluginType::REMAP; - - // Parse parameters - if (!read_config_option(argc - 1, const_cast<const char **>(argv + 1), *owned_config)) { - Dbg(dbg_ctl, "Bad arguments"); + if (argc != 3) { + snprintf(errbuf, errbuf_size, "[%s] Usage: @plugin=%s.so @pparam=<config.yaml>", PLUGIN_NAME, PLUGIN_NAME); return TS_ERROR; } + Dbg(dbg_ctl, "New instance for client matching %s to %s", argv[0], argv[1]); - if (!owned_config->log_symbol.empty()) { - TSError("[%s] --log-field is not supported in remap.config. Use it in plugin.config instead.", PLUGIN_NAME); + auto configs = std::make_unique<PluginConfigs>(); + if (!load_config_file(argv[2], PluginType::REMAP, *configs)) { + snprintf(errbuf, errbuf_size, "[%s] Failed to load configuration from %s", PLUGIN_NAME, argv[2]); return TS_ERROR; } - // Create a log file - if (!owned_config->log_filename.empty()) { - if (!create_log_file(owned_config->log_filename, owned_config->log_handle)) { - TSError("[%s] Failed to create log.", PLUGIN_NAME); + for (auto const &config : *configs) { + if (!prepare_config(*config)) { return TS_ERROR; Review Comment: If a later entry fails preparation, such as when its export name conflicts with an existing user argument, log objects created for earlier entries remain registered. `PluginConfig`'s destructor does not release `log_handle`, and a failed remap instance never reaches `TSRemapDeleteInstance`. Repeated failed reloads can therefore accumulate orphaned log objects and filename conflicts. The preparation loop in `TSPluginInit` has the same problem. Release every created log handle on both failure paths, including any handle created for the failing entry. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
