Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

feat(rome_console): redact Unicode control characters #2384

Merged
merged 2 commits into from
Apr 12, 2022

Conversation

leops
Copy link
Contributor

@leops leops commented Apr 11, 2022

Summary

This change intercepts the writing logic of rome_console to replace on the fly any Unicode character with a width of zero (with the exception of code points with the White_Space property) with the REPLACEMENT CHARACTER code point. This should cover most code points in the Cc and Cf categories, and ensure they are explicitly visible in the terminal output (for instance when printing code spans)

io::Write::write_all(writer, content.as_bytes())
SanitizeAdapter(writer)
.write_str(content)
.map_err(|_| io::Error::new(io::ErrorKind::Other, "formatter error"))
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Would it be possible to use some more descriptive error messages. Maybe also something that helps us find the cause of the error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the same error message that would be emitted by the Rust standard library when converting an fmt::Error to io::Error (https://github.com/rust-lang/rust/blob/master/library/std/src/io/mod.rs#L1662), I copied it to keep the errors coherent in case there's another place where we use io::Write::write_fmt (although here the ErrorKind is Other because Uncategorized is private to the standard library)

Copy link
Contributor

@ematipico ematipico Apr 12, 2022

Choose a reason for hiding this comment

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

Either way, is there a way to put a more descriptive message and how to fix the error? If so, then we should put such information regardless: https://rome.tools/#technical

First and second bullet points

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reading back the code in std it also includes a bit of logic to forward the original I/O error if it exists, I missed that when I originally searched for the error message but now I'm also going to include that.
This means the only case a "formatter error" could be returned is when an implementation of fmt::Display returned an error that didn't originate from the underlying writer, unfortunately since all displayable types are type-erased it's not really possible to extract more information from the error.
I can probably reword the message to make the exact error that happened more clear, then as the console printing is designed to panic in case of error down the line this error message is going to be printed to the user as part of the panic hook telling the user Rome encountered a bug and asking them to file a bug report, so hopefully all of that should make the error more approachable if it's ever encountered in a production build

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, that makes sense. At the end it's our bug, so asking the user to open in issue seems to be a good messagging.

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Apr 12, 2022

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 3faa07e
Status: ✅  Deploy successful!
Preview URL: https://1c8c4bd6.tools-8rn.pages.dev

View logs

@leops leops merged commit 316fbfc into main Apr 12, 2022
@leops leops deleted the feature/console-sanitize branch April 12, 2022 10:01
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants