using RegexArray = std::vector<std::pair<std::string, std::regex>>;
// An AgentEventHandler that dumps requests information to stdout and blocks // any requests that have the keyword "block" in their data class Handler : public content_analysis::sdk::AgentEventHandler { public: using Event = content_analysis::sdk::ContentAnalysisEvent;
protected: // subclasses can override this // returns whether the response has been set virtualbool SetCustomResponse(AtomicCout& aout, std::unique_ptr<Event>& event) { returnfalse;
} // subclasses can override this // returns whether the response has been sent virtualbool SendCustomResponse(std::unique_ptr<Event>& event) { returnfalse;
} // Analyzes one request from Google Chrome and responds back to the browser // with either an allow or block verdict. void AnalyzeContent(AtomicCout& aout, std::unique_ptr<Event> event) { // An event represents one content analysis request and response triggered // by a user action in Google Chrome. The agent determines whether the // user is allowed to perform the action by examining event->GetRequest(). // The verdict, which can be "allow" or "block" is written into // event->GetResponse().
DumpEvent(aout.stream(), event.get());
bool success = true;
std::optional<content_analysis::sdk::ContentAnalysisResponse_Result_TriggeredRule_Action> caResponse; bool setResponse = SetCustomResponse(aout, event); if (!setResponse) {
caResponse = content_analysis::sdk::ContentAnalysisResponse_Result_TriggeredRule_Action_BLOCK; if (event->GetRequest().has_text_content()) {
caResponse = DecideCAResponse(
event->GetRequest().text_content(), aout.stream());
} elseif (event->GetRequest().has_file_path()) { // TODO: Fix downloads to store file *first* so we can check contents. // Until then, just check the file name:
caResponse = DecideCAResponse(
event->GetRequest().file_path(), aout.stream());
} elseif (event->GetRequest().has_print_data()) { // In the case of print request, normally the PDF bytes would be parsed // for sensitive data violations. To keep this class simple, only the // URL is checked for the word "block".
caResponse = DecideCAResponse(event->GetRequest().request_data().url(), aout.stream());
}
}
void OnAnalysisRequested(std::unique_ptr<Event> event) override { // If the agent is capable of analyzing content in the background, the // events may be handled in background threads. Having said that, a // event should not be assumed to be thread safe, that is, it should not // be accessed by more than one thread concurrently. // // In this example code, the event is handled synchronously.
AtomicCout aout;
aout.stream() << std::endl << "----------" << std::endl << std::endl;
AnalyzeContent(aout, std::move(event));
}
void OnResponseAcknowledged( const content_analysis::sdk::ContentAnalysisAcknowledgement&
ack) override { constchar* final_action = ""; if (ack.has_final_action()) { switch (ack.final_action()) { case content_analysis::sdk::ContentAnalysisAcknowledgement::ACTION_UNSPECIFIED:
final_action = ""; break; case content_analysis::sdk::ContentAnalysisAcknowledgement::ALLOW:
final_action = "Allow"; break; case content_analysis::sdk::ContentAnalysisAcknowledgement::REPORT_ONLY:
final_action = "Report only"; break; case content_analysis::sdk::ContentAnalysisAcknowledgement::WARN:
final_action = "Warn"; break; case content_analysis::sdk::ContentAnalysisAcknowledgement::BLOCK:
final_action = "Block"; break;
}
}
// Truncate the text past 50 bytes to keep it to a reasonable length in // the terminal window. if (text_content.size() > 50) {
prefix = " Pasted data (truncated): ";
text_content = text_content.substr(0, 50) + "...";
}
stream << prefix
<< text_content
<< std::endl;
stream << " Pasted data size (bytes): "
<< request.text_content().size()
<< std::endl;
}
if (request.has_print_data() && !print_data_file_path_.empty()) { if (request.request_data().has_print_metadata() &&
request.request_data().print_metadata().has_printer_name()) {
stream << " Printer name: "
<< request.request_data().print_metadata().printer_name()
<< std::endl;
} else {
stream << " No printer name in request" << std::endl;
}
stream << " Print data saved to: " << print_data_file_path_
<< std::endl; using content_analysis::sdk::ContentAnalysisEvent; auto print_data =
content_analysis::sdk::CreateScopedPrintHandle(event->GetRequest(),
event->GetBrowserInfo().pid);
std::ofstream file(print_data_file_path_,
std::ios::out | std::ios::trunc | std::ios::binary);
file.write(print_data->data(), print_data->size());
file.flush();
file.close();
}
}
// Get file size. This example does not handle files larger than 1MB. // Make sure content string can hold the contents of the file. int size = file.tellg(); if (size > 1024 * 1024) returnfalse;
// An AgentEventHandler that dumps requests information to stdout and blocks // any requests that have the keyword "block" in their data class QueuingHandler : public Handler { public:
QueuingHandler(unsignedlong threads, std::vector<unsignedlong>&& delays, const std::string& print_data_file_path,
RegexArray&& toBlock = RegexArray(),
RegexArray&& toWarn = RegexArray(),
RegexArray&& toReport = RegexArray())
: Handler(std::move(delays), print_data_file_path, std::move(toBlock), std::move(toWarn), std::move(toReport)) {
StartBackgroundThreads(threads);
}
~QueuingHandler() override { // Abort background process and wait for it to finish.
request_queue_.abort();
WaitForBackgroundThread();
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.