Skip to content

Conversation

@piotrbartman
Copy link
Member

@piotrbartman piotrbartman commented Feb 4, 2026

A simple and efficient log sanitizer, ready to work with rsyslog

first step in: QubesOS/qubes-issues/issues/830

requires: QubesOS/qubes-linux-utils/pull/136

Comment on lines 60 to 93
/**
* Implements strict ASCII filtering and replaces non-printable control characters.
* All bytes outside the standard ASCII range (0x00 - 0x7F) are replaced with '?'.
* Control characters (0x00-0x1F) and DEL (0x7F) are replaced with ' '.
*
* @param str Input string (read-only)
* @param result Buffer to store the sanitized output (must be large enough)
* @param max_len Maximum length of the input string to process
* @return The length of the sanitized string written to result
*/
size_t filter_to_ascii_and_sanitize(const char *str, char *result, size_t max_len) {
size_t i;
size_t j = 0;

for (i = 0; i < max_len && str[i] != '\0'; i++) {
unsigned char byte = (unsigned char)str[i];

if (byte > 0x7F) {
result[j++] = '?';
continue;
}

// Replace harmful control characters (0x00-0x1F) and DEL (0x7F)
if ((byte < 0x20) || byte == 0x7F) {
result[j++] = ' ';
}
// Keep printable ASCII characters
else {
result[j++] = str[i];
}
}
result[j] = '\0';
return j;
}
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, maybe limiting chars to only ASCII here was a bit paranoid. I'll move this function to qubes-linux-utils and extend the restrictions for better handling of utf.

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