Skip to content

Conversation

@toontoet
Copy link
Contributor

This is an attempt to resolve bug #1106, whereby Unicode characters in JSON via an AMCP CG UPDATE command rendered the AMCP channel unusable.
This solution has been extensively tested with many different Unicode payloads, and the AMCP connection now continues to function.
A try/catch has also been added so that the template continues to work even if the JSON is incorrect and no error message is logged.

try {
if (browser_ != nullptr && browser_->GetMainFrame() != nullptr) {
// Safe UTF-8 conversion with error handling
std::string utf8_javascript;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused as to what this is solving too.

In what scenarios could u8(javascript) fail? We are using that in so many places, that it feels like it would be a bigger problem if there are strings that cause it to throw an exception

CASPAR_LOG(info) << L"Received message from " << client->address() << ": " << message << L"\\r\\n";
// Sanitize message for logging to prevent conversion errors
std::wstring sanitized_log_message = message;
caspar::log::replace_nonprintable(sanitized_log_message, L'?');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems redundant, we are already using replace_nonprintable at the point where the log gets written anywhere. Or at least that is the intention, so is there a case where this isnt happening?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there were problems when you did a console.log() with unicode chars from a template. ccg would abort/crash. Console messages are now cleaned up in OnConsoleMessage in the html_producer.cpp.

// Trim the data
std::wstring trimmed_data;
try {
trimmed_data = boost::algorithm::trim_copy_if(data, boost::is_any_of(" \""));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this actually throw in a meaningful way? Looking at the signature, I suspect the only throws is if it fails to allocate the output string. Which is not something worth handling here.

I am happy to be wrong though

// Now escape for JavaScript
std::wstring escaped_data;
try {
escaped_data = escape_javascript_string(json_safe_data);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, I dont see where/how this could throw an error, so this handling adds a lot of I assume unnecessary noise


// Robust error handling to prevent app crashes
std::wstring javascript_call;
try {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another try catch that seems unnecessary

}

// Pre-validate and clean the JSON to prevent crashes
// Remove any control characters that might cause JSON parsing issues
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering about this, whether it is a good idea for us to be mangling the json or if we should continue assuming that the client gave us valid json.

I am not sure, so am open to discussion on this.

Also, have you tested this with xml? that is likely less common to use these days, but in the past was the recommended/only supported format for this data

@toontoet
Copy link
Contributor Author

Thanks for your review. It was a bit messed up. I created a test scenario for a lot of cases. They all pass now.

The backup scenario's for the conversion were there for extra safety but not used in real world scenario's or test-cases. so they are removed. As well as the intensive try catches.

Test script and template to invoke and update various utf-8/unicode strings in json, xml and invoke commands.
emoji_utf8_test.js
debug.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants