|
9 | 9 | use lithe_plugin_model::{PluginRejection, Validation}; |
10 | 10 | use lithe_workspace::{self as workspace, fs}; |
11 | 11 | use serde_json::Value; |
| 12 | +use std::io::Read; |
12 | 13 | use std::path::{Component, Path, PathBuf}; |
13 | 14 | use std::time::UNIX_EPOCH; |
14 | 15 |
|
@@ -62,24 +63,41 @@ fn first_line(text: &str) -> &str { |
62 | 63 |
|
63 | 64 | /// Reads a UTF-8 file up to the output bound. |
64 | 65 | pub(crate) fn read_bounded(path: &Path) -> Validation<String> { |
| 66 | + String::from_utf8(read_file_bytes(path)?) |
| 67 | + .map_err(|_| invalid_output("The build tool output is not UTF-8.")) |
| 68 | +} |
| 69 | + |
| 70 | +/// Bytes of a file up to the output bound, or `None` when it is absent, not |
| 71 | +/// a regular file, or too large. |
| 72 | +pub(crate) fn read_bytes_bounded(path: &Path) -> Option<Vec<u8>> { |
| 73 | + // Sync fingerprints treat an unreadable input as absent. |
| 74 | + read_file_bytes(path).ok() |
| 75 | +} |
| 76 | + |
| 77 | +fn read_file_bytes(path: &Path) -> Validation<Vec<u8>> { |
65 | 78 | let metadata = fs::metadata(path) |
66 | 79 | .map_err(|_| invalid_output("The build tool did not write its output file."))?; |
67 | 80 | if !metadata.is_file() || metadata.len() > MAX_OUTPUT_BYTES as u64 { |
68 | 81 | return Err(invalid_output( |
69 | 82 | "The build tool output file is missing or too large.", |
70 | 83 | )); |
71 | 84 | } |
72 | | - fs::read_to_string(path).map_err(|_| invalid_output("The build tool output is not UTF-8.")) |
| 85 | + let file = fs::File::open(path) |
| 86 | + .map_err(|_| invalid_output("The build tool output file could not be opened."))?; |
| 87 | + read_output(file, metadata.len() as usize) |
73 | 88 | } |
74 | 89 |
|
75 | | -/// Bytes of a file up to the output bound, or `None` when it is absent, not |
76 | | -/// a regular file, or too large. |
77 | | -pub(crate) fn read_bytes_bounded(path: &Path) -> Option<Vec<u8>> { |
78 | | - let metadata = fs::metadata(path).ok()?; |
79 | | - if !metadata.is_file() || metadata.len() > MAX_OUTPUT_BYTES as u64 { |
80 | | - return None; |
| 90 | +fn read_output(reader: impl Read, observed_size: usize) -> Validation<Vec<u8>> { |
| 91 | + let mut bytes = Vec::with_capacity(observed_size + 1); |
| 92 | + // The opened file may have grown since the metadata check. |
| 93 | + reader |
| 94 | + .take(MAX_OUTPUT_BYTES as u64 + 1) |
| 95 | + .read_to_end(&mut bytes) |
| 96 | + .map_err(|_| invalid_output("The build tool output file could not be read."))?; |
| 97 | + if bytes.len() > MAX_OUTPUT_BYTES { |
| 98 | + return Err(invalid_output("The build tool output is too large.")); |
81 | 99 | } |
82 | | - fs::read(path).ok() |
| 100 | + Ok(bytes) |
83 | 101 | } |
84 | 102 |
|
85 | 103 | /// Bounds a host-provided output text before it is parsed line by line. |
@@ -428,6 +446,29 @@ fn is_current(path: &Path, not_before_millis: Option<u64>) -> bool { |
428 | 446 | mod tests { |
429 | 447 | use super::*; |
430 | 448 |
|
| 449 | + #[test] |
| 450 | + fn output_read_enforces_the_limit_even_if_the_file_grows() { |
| 451 | + let limit = MAX_OUTPUT_BYTES as u64; |
| 452 | + let at_limit = std::io::repeat(b'x').take(limit); |
| 453 | + assert_eq!( |
| 454 | + read_output(at_limit, MAX_OUTPUT_BYTES).unwrap().len(), |
| 455 | + MAX_OUTPUT_BYTES |
| 456 | + ); |
| 457 | + |
| 458 | + // Metadata reported an in-budget size, but the stream contains more. |
| 459 | + let mut over_limit = std::io::repeat(b'x').take(limit + 2); |
| 460 | + let rejected = match read_output(&mut over_limit, MAX_OUTPUT_BYTES) { |
| 461 | + Ok(bytes) => panic!("accepted an oversized output of {} bytes", bytes.len()), |
| 462 | + Err(error) => error, |
| 463 | + }; |
| 464 | + assert_eq!(rejected.code, "plugin.build.invalidOutput"); |
| 465 | + assert_eq!( |
| 466 | + over_limit.limit(), |
| 467 | + 1, |
| 468 | + "read past the extra byte needed to detect overflow" |
| 469 | + ); |
| 470 | + } |
| 471 | + |
431 | 472 | #[test] |
432 | 473 | fn wildcards_match_within_a_segment() { |
433 | 474 | assert!(wildcard("TEST-*.xml", "TEST-demo.OrderTest.xml")); |
|
0 commit comments