Skip to content

armgit / jan

Public Imported from GitHub
0

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.toml
index b7e3e40c2..69da78a65 100644
--- a/src-tauri/Cargo.toml
+++ b/src-tauri/Cargo.toml
@@ -172,7 +172,7 @@ opt-level = "z" # Optimize for size
lto = "fat" # Aggressive Link Time Optimization
strip = "symbols" # Strip debug symbols for smaller binary
codegen-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 panics
overflow-checks = false # Disable overflow checks for size
debug = false # No debug info
debug-assertions = false # No debug assertions
diff --git a/src-tauri/plugins/tauri-plugin-rag/src/constants.rs b/src-tauri/plugins/tauri-plugin-rag/src/constants.rs
new file mode 100644
index 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 file
diff --git a/src-tauri/plugins/tauri-plugin-rag/src/lib.rs b/src-tauri/plugins/tauri-plugin-rag/src/lib.rs
index 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.rs
index 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 app
let 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 encoding
let mut detector = EncodingDetector::new();