2022-11-11 21:39:37 -06:00
|
|
|
|
use crate::{
|
|
|
|
|
lints::{
|
|
|
|
|
HiddenUnicodeCodepointsDiag, HiddenUnicodeCodepointsDiagLabels,
|
|
|
|
|
HiddenUnicodeCodepointsDiagSub,
|
|
|
|
|
},
|
|
|
|
|
EarlyContext, EarlyLintPass, LintContext,
|
|
|
|
|
};
|
2021-11-04 17:31:42 -05:00
|
|
|
|
use ast::util::unicode::{contains_text_flow_control_chars, TEXT_FLOW_CONTROL_CHARS};
|
2021-08-19 13:40:00 -05:00
|
|
|
|
use rustc_ast as ast;
|
|
|
|
|
use rustc_span::{BytePos, Span, Symbol};
|
|
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
|
/// The `text_direction_codepoint_in_literal` lint detects Unicode codepoints that change the
|
|
|
|
|
/// visual representation of text on screen in a way that does not correspond to their on
|
|
|
|
|
/// memory representation.
|
|
|
|
|
///
|
|
|
|
|
/// ### Explanation
|
|
|
|
|
///
|
|
|
|
|
/// The unicode characters `\u{202A}`, `\u{202B}`, `\u{202D}`, `\u{202E}`, `\u{2066}`,
|
|
|
|
|
/// `\u{2067}`, `\u{2068}`, `\u{202C}` and `\u{2069}` make the flow of text on screen change
|
|
|
|
|
/// its direction on software that supports these codepoints. This makes the text "abc" display
|
|
|
|
|
/// as "cba" on screen. By leveraging software that supports these, people can write specially
|
|
|
|
|
/// crafted literals that make the surrounding code seem like it's performing one action, when
|
|
|
|
|
/// in reality it is performing another. Because of this, we proactively lint against their
|
|
|
|
|
/// presence to avoid surprises.
|
|
|
|
|
///
|
|
|
|
|
/// ### Example
|
|
|
|
|
///
|
|
|
|
|
/// ```rust,compile_fail
|
|
|
|
|
/// #![deny(text_direction_codepoint_in_literal)]
|
|
|
|
|
/// fn main() {
|
|
|
|
|
/// println!("{:?}", '');
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// {{produces}}
|
|
|
|
|
///
|
|
|
|
|
pub TEXT_DIRECTION_CODEPOINT_IN_LITERAL,
|
|
|
|
|
Deny,
|
|
|
|
|
"detect special Unicode codepoints that affect the visual representation of text on screen, \
|
|
|
|
|
changing the direction in which text flows",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
declare_lint_pass!(HiddenUnicodeCodepoints => [TEXT_DIRECTION_CODEPOINT_IN_LITERAL]);
|
|
|
|
|
|
|
|
|
|
impl HiddenUnicodeCodepoints {
|
|
|
|
|
fn lint_text_direction_codepoint(
|
|
|
|
|
&self,
|
|
|
|
|
cx: &EarlyContext<'_>,
|
|
|
|
|
text: Symbol,
|
|
|
|
|
span: Span,
|
|
|
|
|
padding: u32,
|
|
|
|
|
point_at_inner_spans: bool,
|
|
|
|
|
label: &str,
|
|
|
|
|
) {
|
|
|
|
|
// Obtain the `Span`s for each of the forbidden chars.
|
|
|
|
|
let spans: Vec<_> = text
|
|
|
|
|
.as_str()
|
|
|
|
|
.char_indices()
|
|
|
|
|
.filter_map(|(i, c)| {
|
2021-11-04 17:31:42 -05:00
|
|
|
|
TEXT_FLOW_CONTROL_CHARS.contains(&c).then(|| {
|
2021-08-19 13:40:00 -05:00
|
|
|
|
let lo = span.lo() + BytePos(i as u32 + padding);
|
|
|
|
|
(c, span.with_lo(lo).with_hi(lo + BytePos(c.len_utf8() as u32)))
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
|
2022-11-11 21:39:37 -06:00
|
|
|
|
let count = spans.len();
|
|
|
|
|
let labels = point_at_inner_spans
|
|
|
|
|
.then_some(HiddenUnicodeCodepointsDiagLabels { spans: spans.clone() });
|
|
|
|
|
let sub = if point_at_inner_spans && !spans.is_empty() {
|
|
|
|
|
HiddenUnicodeCodepointsDiagSub::Escape { spans }
|
|
|
|
|
} else {
|
|
|
|
|
HiddenUnicodeCodepointsDiagSub::NoEscape { spans }
|
|
|
|
|
};
|
|
|
|
|
|
2024-01-15 21:40:39 -06:00
|
|
|
|
cx.emit_span_lint(
|
2022-09-16 02:01:02 -05:00
|
|
|
|
TEXT_DIRECTION_CODEPOINT_IN_LITERAL,
|
|
|
|
|
span,
|
2022-11-11 21:39:37 -06:00
|
|
|
|
HiddenUnicodeCodepointsDiag { label, count, span_label: span, labels, sub },
|
2022-09-16 02:01:02 -05:00
|
|
|
|
);
|
2021-08-19 13:40:00 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
impl EarlyLintPass for HiddenUnicodeCodepoints {
|
|
|
|
|
fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &ast::Attribute) {
|
|
|
|
|
if let ast::AttrKind::DocComment(_, comment) = attr.kind {
|
2021-12-14 21:39:23 -06:00
|
|
|
|
if contains_text_flow_control_chars(comment.as_str()) {
|
2021-08-19 13:40:00 -05:00
|
|
|
|
self.lint_text_direction_codepoint(cx, comment, attr.span, 0, false, "doc comment");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-06 21:52:28 -06:00
|
|
|
|
#[inline]
|
2021-08-19 13:40:00 -05:00
|
|
|
|
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) {
|
|
|
|
|
// byte strings are already handled well enough by `EscapeError::NonAsciiCharInByteString`
|
2022-10-09 21:40:56 -05:00
|
|
|
|
match &expr.kind {
|
|
|
|
|
ast::ExprKind::Lit(token_lit) => {
|
2022-08-01 01:46:08 -05:00
|
|
|
|
let text = token_lit.symbol;
|
2021-12-14 21:39:23 -06:00
|
|
|
|
if !contains_text_flow_control_chars(text.as_str()) {
|
2021-08-19 13:40:00 -05:00
|
|
|
|
return;
|
|
|
|
|
}
|
2022-10-09 21:40:56 -05:00
|
|
|
|
let padding = match token_lit.kind {
|
2021-08-19 13:40:00 -05:00
|
|
|
|
// account for `"` or `'`
|
2022-10-09 21:40:56 -05:00
|
|
|
|
ast::token::LitKind::Str | ast::token::LitKind::Char => 1,
|
2021-08-19 13:40:00 -05:00
|
|
|
|
// account for `r###"`
|
2022-10-09 21:40:56 -05:00
|
|
|
|
ast::token::LitKind::StrRaw(n) => n as u32 + 2,
|
2021-08-19 13:40:00 -05:00
|
|
|
|
_ => return,
|
|
|
|
|
};
|
2022-10-09 21:40:56 -05:00
|
|
|
|
self.lint_text_direction_codepoint(cx, text, expr.span, padding, true, "literal");
|
2021-08-19 13:40:00 -05:00
|
|
|
|
}
|
2022-10-09 21:40:56 -05:00
|
|
|
|
_ => {}
|
2021-08-19 13:40:00 -05:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|