1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// Copyright Kitware, Inc.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use derive_builder::Builder;
use git_checks_core::impl_prelude::*;

use std::char::REPLACEMENT_CHARACTER;

const UNICODE_BIDI_CHARS: &[char] = &[
    '\u{202A}', '\u{202B}', '\u{202D}', '\u{202E}', '\u{2066}', '\u{2067}', '\u{2068}', '\u{202C}',
    '\u{2069}',
];

/// A check which denies commits which add text lines containing bidirectional control characters.
///
/// Files may be marked as binary by unsetting the `text` attribute. All content is assumed to be
/// UTF-8 encoded.
#[derive(Builder, Debug, Default, Clone, Copy)]
#[builder(field(private))]
pub struct RejectBiDi {
    /// Whether bidirectional control characters are allowed at all.
    ///
    /// Configuration: Optional
    /// Default: false
    #[builder(default = "false")]
    allow: bool,
}

impl RejectBiDi {
    /// Create a new builder.
    pub fn builder() -> RejectBiDiBuilder {
        RejectBiDiBuilder::default()
    }
}

impl ContentCheck for RejectBiDi {
    fn name(&self) -> &str {
        "reject-bidi"
    }

    fn check(
        &self,
        ctx: &CheckGitContext,
        content: &dyn Content,
    ) -> Result<CheckResult, Box<dyn Error>> {
        let mut result = CheckResult::new();

        for diff in content.diffs() {
            match diff.status {
                StatusChange::Added | StatusChange::Modified(_) => (),
                _ => continue,
            }

            let diff_attr = ctx.check_attr("diff", diff.name.as_path())?;
            if let AttributeState::Unset = diff_attr {
                // Binary files should not be handled here.
                continue;
            }

            let patch = match content.path_diff(&diff.name) {
                Ok(s) => s,
                Err(err) => {
                    result.add_alert(
                        format!(
                            "{}failed to get the diff for file `{}`: {}.",
                            commit_prefix(content),
                            diff.name,
                            err,
                        ),
                        true,
                    );
                    continue;
                },
            };

            for line in patch.lines().filter(|line| line.starts_with('+')) {
                let line_bidi_free: String = line
                    .chars()
                    .map(|c| {
                        if UNICODE_BIDI_CHARS.contains(&c) {
                            REPLACEMENT_CHARACTER
                        } else {
                            c
                        }
                    })
                    .collect();
                if line_bidi_free != line {
                    let safe_line = line_bidi_free[1..]
                        .replace('\\', "\\\\")
                        .replace('`', "\\`");
                    if self.allow {
                        result.add_warning(format!(
                            "{}Unicode bidirectional control character(s) added in `{}`: `{}`.",
                            commit_prefix_str(content, "needs checked;"),
                            diff.name,
                            safe_line,
                        ));
                    } else {
                        result.add_error(format!(
                            "{}Unicode bidirectional control character(s) added in `{}`: `{}`.",
                            commit_prefix_str(content, "not allowed;"),
                            diff.name,
                            safe_line,
                        ));
                    }
                }
            }
        }

        Ok(result)
    }
}

#[cfg(feature = "config")]
pub(crate) mod config {
    use git_checks_config::{register_checks, CommitCheckConfig, IntoCheck, TopicCheckConfig};
    use serde::Deserialize;
    #[cfg(test)]
    use serde_json::json;

    use crate::RejectBiDi;

    /// Configuration for the `RejectBiDi` check.
    ///
    /// The `allow` key is a boolean as to whether the check should raise an error rather than a
    /// warning. Defaults to `false`.
    ///
    /// This check is registered as a commit check with the name `"reject_bidi"` and a topic check
    /// with the name `"reject_bidi/topic"`.
    ///
    /// # Example
    ///
    /// ```json
    /// {
    ///     "allow": true
    /// }
    /// ```
    #[derive(Deserialize, Debug)]
    pub struct RejectBiDiConfig {
        #[serde(default)]
        allow: bool,
    }

    impl IntoCheck for RejectBiDiConfig {
        type Check = RejectBiDi;

        fn into_check(self) -> Self::Check {
            let mut builder = RejectBiDi::builder();

            builder.allow(self.allow);

            builder
                .build()
                .expect("configuration mismatch for `RejectBiDi`")
        }
    }

    register_checks! {
        RejectBiDiConfig {
            "reject_bidi" => CommitCheckConfig,
            "reject_bidi/topic" => TopicCheckConfig,
        },
    }

    #[test]
    fn test_reject_bidi_config_empty() {
        let json = json!({});
        let check: RejectBiDiConfig = serde_json::from_value(json).unwrap();

        assert!(!check.allow);

        let check = check.into_check();

        assert!(!check.allow);
    }

    #[test]
    fn test_reject_bidi_config_all_fields() {
        let json = json!({
            "allow": true,
        });
        let check: RejectBiDiConfig = serde_json::from_value(json).unwrap();

        assert!(check.allow);

        let check = check.into_check();

        assert!(check.allow);
    }
}

#[cfg(test)]
mod tests {
    use git_checks_core::{Check, TopicCheck};

    use crate::test::*;
    use crate::RejectBiDi;

    const BAD_TOPIC: &str = "678c1deeade619d52c5b0990bb05af79017f2787";
    const DELETE_TOPIC: &str = "0a5b9308fcedec797d70ba78bb1b92a7b7943828";
    const FIX_TOPIC: &str = "0a5b9308fcedec797d70ba78bb1b92a7b7943828";

    #[test]
    fn test_reject_bidi_builder_default() {
        assert!(RejectBiDi::builder().build().is_ok());
    }

    #[test]
    fn test_reject_bidi_name_commit() {
        let check = RejectBiDi::default();
        assert_eq!(Check::name(&check), "reject-bidi");
    }

    #[test]
    fn test_reject_bidi_name_topic() {
        let check = RejectBiDi::default();
        assert_eq!(TopicCheck::name(&check), "reject-bidi");
    }

    #[test]
    fn test_reject_bidi() {
        let check = RejectBiDi::default();
        let result = run_check("test_reject_bidi", BAD_TOPIC, check);
        test_result_errors(result, &[
            "commit 678c1deeade619d52c5b0990bb05af79017f2787 not allowed; Unicode bidirectional \
             control character(s) added in `has-bidi`: `bidi character: \u{fffd}`.",
            "commit 678c1deeade619d52c5b0990bb05af79017f2787 not allowed; Unicode bidirectional \
             control character(s) added in `has-bidi`: `bidi character: \u{fffd}`.",
            "commit 678c1deeade619d52c5b0990bb05af79017f2787 not allowed; Unicode bidirectional \
             control character(s) added in `has-bidi`: `bidi character: \u{fffd}`.",
            "commit 678c1deeade619d52c5b0990bb05af79017f2787 not allowed; Unicode bidirectional \
             control character(s) added in `has-bidi`: `bidi character: \u{fffd}`.",
            "commit 678c1deeade619d52c5b0990bb05af79017f2787 not allowed; Unicode bidirectional \
             control character(s) added in `has-bidi`: `bidi character: \u{fffd}`.",
            "commit 678c1deeade619d52c5b0990bb05af79017f2787 not allowed; Unicode bidirectional \
             control character(s) added in `has-bidi`: `bidi character: \u{fffd}`.",
            "commit 678c1deeade619d52c5b0990bb05af79017f2787 not allowed; Unicode bidirectional \
             control character(s) added in `has-bidi`: `bidi character: \u{fffd}`.",
            "commit 678c1deeade619d52c5b0990bb05af79017f2787 not allowed; Unicode bidirectional \
             control character(s) added in `has-bidi`: `bidi character: \u{fffd}`.",
        ]);
    }

    #[test]
    fn test_reject_bidi_allow() {
        let check = RejectBiDi::builder().allow(true).build().unwrap();
        let result = run_check("test_reject_bidi_allow", BAD_TOPIC, check);
        test_result_warnings(result, &[
            "commit 678c1deeade619d52c5b0990bb05af79017f2787 needs checked; Unicode bidirectional \
             control character(s) added in `has-bidi`: `bidi character: \u{fffd}`.",
            "commit 678c1deeade619d52c5b0990bb05af79017f2787 needs checked; Unicode bidirectional \
             control character(s) added in `has-bidi`: `bidi character: \u{fffd}`.",
            "commit 678c1deeade619d52c5b0990bb05af79017f2787 needs checked; Unicode bidirectional \
             control character(s) added in `has-bidi`: `bidi character: \u{fffd}`.",
            "commit 678c1deeade619d52c5b0990bb05af79017f2787 needs checked; Unicode bidirectional \
             control character(s) added in `has-bidi`: `bidi character: \u{fffd}`.",
            "commit 678c1deeade619d52c5b0990bb05af79017f2787 needs checked; Unicode bidirectional \
             control character(s) added in `has-bidi`: `bidi character: \u{fffd}`.",
            "commit 678c1deeade619d52c5b0990bb05af79017f2787 needs checked; Unicode bidirectional \
             control character(s) added in `has-bidi`: `bidi character: \u{fffd}`.",
            "commit 678c1deeade619d52c5b0990bb05af79017f2787 needs checked; Unicode bidirectional \
             control character(s) added in `has-bidi`: `bidi character: \u{fffd}`.",
            "commit 678c1deeade619d52c5b0990bb05af79017f2787 needs checked; Unicode bidirectional \
             control character(s) added in `has-bidi`: `bidi character: \u{fffd}`.",
        ]);
    }

    #[test]
    fn test_reject_bidi_topic() {
        let check = RejectBiDi::default();
        let result = run_topic_check("test_reject_bidi_topic", BAD_TOPIC, check);
        test_result_errors(
            result,
            &[
                "Unicode bidirectional control character(s) added in `has-bidi`: `bidi character: \
                 \u{fffd}`.",
                "Unicode bidirectional control character(s) added in `has-bidi`: `bidi character: \
                 \u{fffd}`.",
                "Unicode bidirectional control character(s) added in `has-bidi`: `bidi character: \
                 \u{fffd}`.",
                "Unicode bidirectional control character(s) added in `has-bidi`: `bidi character: \
                 \u{fffd}`.",
                "Unicode bidirectional control character(s) added in `has-bidi`: `bidi character: \
                 \u{fffd}`.",
                "Unicode bidirectional control character(s) added in `has-bidi`: `bidi character: \
                 \u{fffd}`.",
                "Unicode bidirectional control character(s) added in `has-bidi`: `bidi character: \
                 \u{fffd}`.",
                "Unicode bidirectional control character(s) added in `has-bidi`: `bidi character: \
                 \u{fffd}`.",
            ],
        );
    }

    #[test]
    fn test_reject_bidi_topic_allow() {
        let check = RejectBiDi::builder().allow(true).build().unwrap();
        let result = run_topic_check("test_reject_bidi_topic_allow", BAD_TOPIC, check);
        test_result_warnings(
            result,
            &[
                "Unicode bidirectional control character(s) added in `has-bidi`: `bidi character: \
                 \u{fffd}`.",
                "Unicode bidirectional control character(s) added in `has-bidi`: `bidi character: \
                 \u{fffd}`.",
                "Unicode bidirectional control character(s) added in `has-bidi`: `bidi character: \
                 \u{fffd}`.",
                "Unicode bidirectional control character(s) added in `has-bidi`: `bidi character: \
                 \u{fffd}`.",
                "Unicode bidirectional control character(s) added in `has-bidi`: `bidi character: \
                 \u{fffd}`.",
                "Unicode bidirectional control character(s) added in `has-bidi`: `bidi character: \
                 \u{fffd}`.",
                "Unicode bidirectional control character(s) added in `has-bidi`: `bidi character: \
                 \u{fffd}`.",
                "Unicode bidirectional control character(s) added in `has-bidi`: `bidi character: \
                 \u{fffd}`.",
            ],
        );
    }

    #[test]
    fn test_reject_bidi_delete_file() {
        let check = RejectBiDi::default();
        let conf = make_check_conf(&check);

        let result = test_check_base(
            "test_reject_bidi_delete_file",
            DELETE_TOPIC,
            BAD_TOPIC,
            &conf,
        );
        test_result_ok(result);
    }

    #[test]
    fn test_reject_bidi_delete_file_topic() {
        let check = RejectBiDi::default();
        let result = run_topic_check("test_reject_bidi_delete_file_topic", DELETE_TOPIC, check);
        test_result_ok(result);
    }

    #[test]
    fn test_reject_bidi_topic_fixed() {
        let check = RejectBiDi::default();
        run_topic_check_ok("test_reject_bidi_topic_fixed", FIX_TOPIC, check);
    }
}