Copilot commented on code in PR #12742: URL: https://github.com/apache/trafficserver/pull/12742#discussion_r2621138910
########## plugins/xdebug/xdebug_escape.cc: ########## @@ -0,0 +1,102 @@ +/** @file + * + * XDebug plugin JSON escaping implementation. + * + * 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 "xdebug_escape.h" + +namespace xdebug +{ + +std::string_view +EscapeCharForJson::operator()(char const &c) +{ + if ((_state != IN_VALUE) && ((' ' == c) || ('\t' == c))) { + return {""}; + } + if ((IN_NAME == _state) && (':' == c)) { + _state = BEFORE_VALUE; + if (_full_json) { + return {R"(":")"}; + } else { + return {"' : '"}; + } + } + if ('\r' == c) { + return {""}; + } + if ('\n' == c) { + std::string_view result{_after_value(_full_json)}; + + if (BEFORE_NAME == _state) { + return {""}; + } else if (BEFORE_VALUE == _state) { + result = _handle_empty_value(_full_json); + } + _state = BEFORE_NAME; + return result; + } + if (BEFORE_NAME == _state) { + _state = IN_NAME; + } else if (BEFORE_VALUE == _state) { + _state = IN_VALUE; + } + switch (c) { + case '"': + return {"\\\""}; + case '\\': + return {"\\\\"}; + case '\b': + return {"\\b"}; + case '\f': + return {"\\f"}; + case '\t': + return {"\\t"}; + default: + return {&c, 1}; + } Review Comment: The removal of single quote escaping breaks the legacy probe format. The legacy format uses single-quoted strings (e.g., 'header' : 'value'), so single quotes within values must be escaped to prevent premature string termination. For example, a Content-Security-Policy header with value "default-src 'self'" would produce invalid output: 'content-security-policy' : 'default-src 'self'' where the inner 'self' breaks the string. Single quote escaping should be conditionally applied based on the _full_json flag: escape in legacy mode (!_full_json) but not in full JSON mode. ########## plugins/xdebug/unit_tests/test_xdebug_utils.cc: ########## @@ -193,3 +198,128 @@ TEST_CASE("xdebug::is_textual_content_type functionality", "[xdebug][utils]") REQUIRE(xdebug::is_textual_content_type("has-xml-in-name")); } } + +// Helper function to process a string through EscapeCharForJson. +static std::string +escape_string(std::string_view input, bool full_json) +{ + xdebug::EscapeCharForJson escaper(full_json); + std::stringstream ss; + for (char c : input) { + ss << escaper(c); + } + return ss.str(); +} + +struct EscapeTestCase { + std::string description; + bool full_json; + std::string input; + std::string expected; +}; + +TEST_CASE("xdebug::EscapeCharForJson escaping", "[xdebug][headers]") +{ + // clang-format off + static std::vector<EscapeTestCase> const tests = { + // Single quotes are NOT escaped in either mode. Review Comment: This comment is incorrect and misleading. Single quotes should be escaped in legacy mode (which uses single-quoted strings) but not in full JSON mode (which uses double-quoted strings per RFC 8259). The comment should clarify that single quotes are escaped differently based on the format. ```suggestion // Single quotes are not escaped in either legacy or full JSON mode in this implementation. // Note: Per RFC 8259, single quotes do not require escaping in full JSON mode (which uses double quotes). // In legacy mode (which may use single-quoted strings), escaping may be required for strict compatibility, // but this implementation does not escape them in either mode. ``` ########## plugins/xdebug/unit_tests/test_xdebug_utils.cc: ########## @@ -193,3 +198,128 @@ TEST_CASE("xdebug::is_textual_content_type functionality", "[xdebug][utils]") REQUIRE(xdebug::is_textual_content_type("has-xml-in-name")); } } + +// Helper function to process a string through EscapeCharForJson. +static std::string +escape_string(std::string_view input, bool full_json) +{ + xdebug::EscapeCharForJson escaper(full_json); + std::stringstream ss; + for (char c : input) { + ss << escaper(c); + } + return ss.str(); +} + +struct EscapeTestCase { + std::string description; + bool full_json; + std::string input; + std::string expected; +}; + +TEST_CASE("xdebug::EscapeCharForJson escaping", "[xdebug][headers]") +{ + // clang-format off + static std::vector<EscapeTestCase> const tests = { + // Single quotes are NOT escaped in either mode. + {"full JSON: single quotes are not escaped", + xdebug::FULL_JSON, + R"('self')", + R"('self')"}, + + {"full JSON: CSP header with multiple single-quoted directives", + xdebug::FULL_JSON, + R"(child-src blob: 'self'; connect-src 'self' 'unsafe-inline')", + R"(child-src blob: 'self'; connect-src 'self' 'unsafe-inline')"}, + + {"legacy: single quotes are not escaped", + !xdebug::FULL_JSON, + R"('self')", + R"('self')"}, + + {"legacy: CSP header with multiple single-quoted directives", + !xdebug::FULL_JSON, + R"(child-src blob: 'self'; connect-src 'self' 'unsafe-inline')", + R"(child-src blob: 'self'; connect-src 'self' 'unsafe-inline')"}, Review Comment: These test cases incorrectly expect single quotes to NOT be escaped in legacy mode. Since the legacy probe format uses single-quoted strings, single quotes within values must be escaped to prevent breaking the string delimiters. These tests should expect the single quotes to be escaped as \' in legacy mode (when full_json is false). ```suggestion R"(\'self\')"}, {"legacy: CSP header with multiple single-quoted directives", !xdebug::FULL_JSON, R"(child-src blob: 'self'; connect-src 'self' 'unsafe-inline')", R"(child-src blob: \'self\'; connect-src \'self\' \'unsafe-inline\')"}, ``` -- 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]
