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


##########
test/brpc_builtin_service_unittest.cpp:
##########
@@ -815,6 +819,60 @@ TEST_F(BuiltinServiceTest, hotspots_query_escaping) {
     }
 }
 
+TEST_F(BuiltinServiceTest, hotspots_profile_filename_escaping) {
+    GFLAGS_NAMESPACE::FlagSaver flag_saver;
+    butil::ScopedTempDir temp_dir;
+    ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
+    brpc::FLAGS_rpc_profiling_dir = temp_dir.path().value();
+    const butil::FilePath profile_dir = temp_dir.path().Append(
+        brpc::GetProgramChecksum());
+    ASSERT_TRUE(butil::CreateDirectory(profile_dir));
+
+    const std::string filename = "profile'><img src=x onerror=alert(1)>.cpu";
+    const butil::FilePath profile_path = profile_dir.Append(filename);
+    ASSERT_EQ(0, butil::WriteFile(profile_path, "", 0));
+
+    ClosureChecker done;
+    brpc::Controller cntl;
+    brpc::HotspotsRequest req;
+    brpc::HotspotsResponse res;
+    brpc::HotspotsService service;
+    SetUpController(&cntl, true);
+    service.cpu(&cntl, &req, &res, &done);
+
+    const std::string& body = cntl.response_attachment().to_string();
+    EXPECT_EQ(std::string::npos, body.find(filename))
+        << "unescaped profile filename in html: " << body;
+    EXPECT_NE(std::string::npos, body.find(brpc::WebEscape(filename)));
+
+    const butil::FilePath selected_profile_path =
+        profile_dir.Append("selected.cpu");
+    ASSERT_EQ(0, butil::WriteFile(selected_profile_path, "", 0));
+    std::string percent_encoded_path;
+    brpc::PercentEncode(selected_profile_path.value(), &percent_encoded_path);
+    ClosureChecker encoded_done;
+    brpc::Controller encoded_cntl;
+    brpc::HotspotsRequest encoded_req;
+    brpc::HotspotsResponse encoded_res;
+    SetUpController(&encoded_cntl, true);
+    encoded_cntl.http_request().uri().SetQuery(
+        "view", "brpc_pct:" + percent_encoded_path);
+    service.cpu(&encoded_cntl, &encoded_req, &encoded_res, &encoded_done);
+    EXPECT_FALSE(encoded_cntl.Failed()) << encoded_cntl.ErrorText();

Review Comment:
   [replied by brpc-oncall robot] This test only exercises the HTML page path 
(`StartProfiling`); the actual consumer `/hotspots/<type>_non_responsive` 
(`DoProfiling`/`DisplayResult`) is not covered, which is exactly why the 
current mismatch (`base` encoded into the URL but never decoded before 
validation) goes unnoticed. Please add a case that calls `cpu_non_responsive` 
(or `heap_non_responsive`) with both an encoded `view` and an encoded `base`, 
plus a case with raw (non-prefixed) `view`/`base` to pin the documented 
backward compatibility.



##########
src/brpc/builtin/hotspots_service.cpp:
##########
@@ -956,10 +1009,10 @@ static void StartProfiling(ProfilingType type,
     os << "  var targetURL = '/hotspots/" << type_str << "';\n"
         "  targetURL += '?display_type=' + display_type;\n"
         "  if (past_prof != '') {\n"
-        "    targetURL += '&view=' + past_prof;\n"
+        "    targetURL += '&view=brpc_pct:' + encodeURIComponent(past_prof);\n"

Review Comment:
   [replied by brpc-oncall robot] The encoding prefix is hard-coded here in the 
inline JavaScript while the server side uses `PROFILE_PATH_ENCODING_PREFIX`; 
the two literals can silently drift apart. Emit the prefix from C++ into the 
script instead of repeating it. More generally, `ValidProfilePath` already 
restricts `view`/`base` to `[A-Za-z0-9_-/.]`, so percent-encoding these paths 
adds no protection and forces a decode step in every consumer (forgetting one 
is what breaks the diff view). If supporting names outside that character set 
is not intended, dropping the encode/decode layer would keep the fix smaller 
and harder to get wrong.



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