Copilot commented on code in PR #3743:
URL: https://github.com/apache/brpc/pull/3743#discussion_r4572107523
##########
src/brpc/builtin/common.cpp:
##########
@@ -122,27 +124,36 @@ void PrintRealDateTime(std::ostream& os, int64_t tm,
}
}
+static std::string EncodeUriPath(const char* path) {
+ std::string encoded;
+ PercentEncode(path, &encoded);
+ size_t pos = 0;
+ while ((pos = encoded.find("%2f", pos)) != std::string::npos) {
+ encoded.replace(pos, 3, "/");
+ ++pos;
+ }
+ return encoded;
+}
+
std::ostream& operator<<(std::ostream& os, const PrintedAsDateTime& d) {
PrintRealDateTime(os, d.realtime);
return os;
}
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(EncodeUriPath(link.uri));
Review Comment:
Percent-encoding the path here changes the path sent back to brpc, but the
URI parser and `FillUnresolvedPath` keep `%xx` sequences verbatim, so
`/dir/a%3fx%23fragment` reaches `dir_service` as a literal filename instead of
`/dir/a?x#fragment`. Directory entries containing `?`, `#`, `&`, or similar
characters therefore become inaccessible after this change. Please add a
compatible decode at the directory-service boundary (without decoding encoded
`/`) or otherwise preserve a path round trip, and cover that round trip in a
test.
##########
test/brpc_builtin_service_unittest.cpp:
##########
@@ -815,6 +819,76 @@ 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_EQ(std::string::npos, body.find(brpc::WebEscape(filename)));
Review Comment:
This regression test never exercises the new profiler-name escaping: the
filename contains characters rejected by `ValidProfilePath`, so it is skipped
before reaching the `<option>` rendering at lines 1103-1107. As a result,
removing the `WebEscape` calls would still make this test pass. Please either
make the test explicitly verify the intended filtering behavior or add a test
around the rendering path that can actually observe the escaping contract.
--
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]