Copilot commented on code in PR #3743:
URL: https://github.com/apache/brpc/pull/3743#discussion_r4571669369


##########
src/brpc/builtin/common.cpp:
##########
@@ -128,21 +129,19 @@ std::ostream& operator<<(std::ostream& os, const 
PrintedAsDateTime& d) {
 }
 
 std::ostream& operator<<(std::ostream& os, const Path& link) {
-    if (link.html_addr) {
-        if (link.html_addr != Path::LOCAL) {
-            os << "<a href=\"http://"; << *link.html_addr << link.uri << "\">";
-        } else {
-            os << "<a href=\"" << link.uri << "\">";
-        }
+    const char* const text = (link.text ? link.text : link.uri);
+    if (!link.html_addr) {
+        os << text;
+        return os;
     }
-    if (link.text) {
-        os << link.text;
+
+    const std::string escaped_uri = WebEscape(link.uri);
+    if (link.html_addr != Path::LOCAL) {
+        os << "<a href=\"http://"; << *link.html_addr << escaped_uri << "\">";
     } else {
-        os << link.uri;
-    }
-    if (link.html_addr) {
-        os << "</a>";
+        os << "<a href=\"" << escaped_uri << "\">";

Review Comment:
   `WebEscape` only performs HTML entity escaping; it does not encode URI path 
syntax. `/dir` passes raw filesystem names here, so a valid entry such as `a?x` 
or `a#x` is rendered as `/dir/.../a?x` or `/dir/.../a#x`, causing the browser 
to treat the suffix as a query or fragment instead of part of the filename. 
Encode path components (while preserving the surrounding URI structure) before 
HTML-escaping the href, and cover reserved filename characters in the 
regression test.



##########
src/brpc/builtin/hotspots_service.cpp:
##########
@@ -1095,11 +1097,11 @@ static void StartProfiling(ProfilingType type,
         "<select id='view_prof' onchange='onSelectProf()'>";
     os << "<option value=''>&lt;new profile&gt;</option>";
     for (size_t i = 0; i < past_profs.size(); ++i) {
-        os << "<option value='" << past_profs[i] << "' ";
+        os << "<option value='" << WebEscape(past_profs[i]) << "' ";
         if (view != nullptr && past_profs[i] == *view) {
             os << "selected";
         }
-        os << '>' << GetBaseName(&past_profs[i]);
+        os << '>' << WebEscape(GetBaseName(&past_profs[i]));

Review Comment:
   Escaping these option values only protects the HTML; the browser decodes the 
entities back to the original path, while `generateURL()` and the AJAX URL 
below still concatenate `view`/`base` without percent-encoding. The handlers 
likewise read the query values without decoding before `ValidProfilePath`, so 
the values produced by these options cannot be selected reliably (and reserved 
characters can be parsed as query syntax). Percent-encode both query values 
when constructing the URLs and `PercentDecode` them before validation/use in 
both handlers; the raw-path regression test does not exercise the 
browser-generated form.



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to