Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
fix: handle pdf parser panic and enforce file size limit (#7157)
Roushan Kumar Singh27a96bb6d83e
diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.tomlindex b7e3e40c2..69da78a65 100644--- a/src-tauri/Cargo.toml+++ b/src-tauri/Cargo.toml@@ -172,7 +172,7 @@ opt-level = "z" # Optimize for sizelto = "fat" # Aggressive Link Time Optimizationstrip = "symbols" # Strip debug symbols for smaller binarycodegen-units = 1 # Reduce parallel codegen for better optimization-panic = "abort" # Don't unwind on panic, saves space+panic = "unwind" # Unwind on panic, allows catching library panicsoverflow-checks = false # Disable overflow checks for sizedebug = false # No debug infodebug-assertions = false # No debug assertionsdiff --git a/src-tauri/plugins/tauri-plugin-rag/src/constants.rs b/src-tauri/plugins/tauri-plugin-rag/src/constants.rsnew file mode 100644index 000000000..11d8196c5--- /dev/null+++ b/src-tauri/plugins/tauri-plugin-rag/src/constants.rs@@ -0,0 +1,5 @@+pub const MAX_PDF_FILE_SIZE: u64 = 20 * 1024 * 1024;+pub const MAX_CSV_FILE_SIZE: u64 = 20 * 1024 * 1024;+pub const MAX_SPREADSHEET_FILE_SIZE: u64 = 20 * 1024 * 1024;+pub const MAX_PPTX_FILE_SIZE: u64 = 20 * 1024 * 1024;+pub const MAX_DOCX_FILE_SIZE: u64 = 20 * 1024 * 1024;\ No newline at end of filediff --git a/src-tauri/plugins/tauri-plugin-rag/src/lib.rs b/src-tauri/plugins/tauri-plugin-rag/src/lib.rsindex 1c66e3388..6fe2c7c20 100644--- a/src-tauri/plugins/tauri-plugin-rag/src/lib.rs+++ b/src-tauri/plugins/tauri-plugin-rag/src/lib.rs@@ -6,8 +6,10 @@ use tauri::{mod parser;mod error;mod commands;+mod constants;pub use error::RagError;+pub use constants::*;pub fn init<R: Runtime>() -> TauriPlugin<R> {Builder::new("rag")diff --git a/src-tauri/plugins/tauri-plugin-rag/src/parser.rs b/src-tauri/plugins/tauri-plugin-rag/src/parser.rsindex 59a8d527e..626cb707e 100644--- a/src-tauri/plugins/tauri-plugin-rag/src/parser.rs+++ b/src-tauri/plugins/tauri-plugin-rag/src/parser.rs@@ -1,4 +1,4 @@-use crate::RagError;+use crate::{RagError, MAX_PDF_FILE_SIZE, MAX_SPREADSHEET_FILE_SIZE, MAX_CSV_FILE_SIZE, MAX_PPTX_FILE_SIZE, MAX_DOCX_FILE_SIZE};use std::borrow::Cow;use std::fs;use std::io::{Cursor, Read};@@ -13,6 +13,10 @@ use quick_xml::Reader;use zip::read::ZipArchive;pub fn parse_pdf(file_path: &str) -> Result<String, RagError> {+ let metadata = fs::metadata(file_path)?;+ if metadata.len() > MAX_PDF_FILE_SIZE {+ return Err(RagError::ParseError("File too large (max 20MB)".to_string()));+ }let bytes = fs::read(file_path)?;// pdf-extract can panic on some malformed PDFs; guard to avoid crashing the applet text = match catch_unwind(AssertUnwindSafe(|| pdf_extract::extract_text_from_mem(&bytes))) {@@ -125,6 +129,10 @@ pub fn parse_document(file_path: &str, file_type: &str) -> Result<String, RagErr}fn parse_docx(file_path: &str) -> Result<String, RagError> {+ let metadata = std::fs::metadata(file_path)?;+ if metadata.len() > MAX_DOCX_FILE_SIZE {+ return Err(RagError::ParseError("File too large (max 20MB)".to_string()));+ }let file = std::fs::File::open(file_path)?;let mut zip = ZipArchive::new(file).map_err(|e| RagError::ParseError(e.to_string()))?;@@ -195,6 +203,10 @@ fn parse_docx(file_path: &str) -> Result<String, RagError> {}fn parse_csv(file_path: &str) -> Result<String, RagError> {+ let metadata = fs::metadata(file_path)?;+ if metadata.len() > MAX_CSV_FILE_SIZE {+ return Err(RagError::ParseError("File too large (max 20MB)".to_string()));+ }let mut rdr = csv_crate::ReaderBuilder::new().has_headers(false).flexible(true)@@ -210,6 +222,10 @@ fn parse_csv(file_path: &str) -> Result<String, RagError> {}fn parse_spreadsheet(file_path: &str) -> Result<String, RagError> {+ let metadata = fs::metadata(file_path)?;+ if metadata.len() > MAX_SPREADSHEET_FILE_SIZE {+ return Err(RagError::ParseError("File too large (max 20MB)".to_string()));+ }let mut workbook = open_workbook_auto(file_path).map_err(|e| RagError::ParseError(e.to_string()))?;let mut out = String::new();@@ -240,6 +256,10 @@ fn parse_spreadsheet(file_path: &str) -> Result<String, RagError> {}fn parse_pptx(file_path: &str) -> Result<String, RagError> {+ let metadata = std::fs::metadata(file_path)?;+ if metadata.len() > MAX_PPTX_FILE_SIZE {+ return Err(RagError::ParseError("File too large (max 20MB)".to_string()));+ }let file = std::fs::File::open(file_path)?;let mut zip = ZipArchive::new(file).map_err(|e| RagError::ParseError(e.to_string()))?;@@ -314,6 +334,10 @@ fn parse_html(file_path: &str) -> Result<String, RagError> {}fn read_text_auto(file_path: &str) -> Result<String, RagError> {+ let metadata = fs::metadata(file_path)?;+ if metadata.len() > 50 * 1024 * 1024 {+ return Err(RagError::ParseError("File too large (max 50MB)".to_string()));+ }let bytes = fs::read(file_path)?;// Detect encodinglet mut detector = EncodingDetector::new();