Skip to content

Commit

Permalink
fix (wip): remove_soft_line_break support
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyas-londhe committed Sep 1, 2024
1 parent dd2f4b0 commit 9b243cb
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 12 deletions.
43 changes: 38 additions & 5 deletions src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ struct EmailCircuitInput {
timestamp_idx: usize,
code_idx: usize,
command_idx: usize,
cleaned_body: Option<Vec<u8>>,
}

#[derive(Serialize, Deserialize)]
Expand All @@ -33,6 +34,7 @@ pub struct EmailCircuitParams {
pub max_header_length: Option<usize>,
pub max_body_length: Option<usize>,
pub sha_precompute_selector: Option<String>,
pub remove_soft_line_breaks: Option<bool>,
}

#[derive(Serialize, Deserialize)]
Expand Down Expand Up @@ -92,6 +94,22 @@ impl CircuitInputParams {
}
}

fn remove_quoted_printable_soft_breaks(body: Vec<u8>) -> Vec<u8> {
let mut result = Vec::with_capacity(body.len());
let mut iter = body.iter().enumerate();

while let Some((i, &byte)) = iter.next() {
if byte == b'=' && body.get(i + 1..i + 3) == Some(&[b'\r', b'\n']) {
iter.nth(1); // Skip the next two bytes
} else {
result.push(byte);
}
}

result.resize(body.len(), 0);
result
}

fn generate_circuit_inputs(params: CircuitInputParams) -> CircuitInput {
let (header_padded, header_padded_len) =
sha256_pad(params.header.clone(), params.max_header_length);
Expand Down Expand Up @@ -148,24 +166,38 @@ pub async fn generate_email_circuit_input(
params
.as_ref()
.and_then(|p| p.sha_precompute_selector.clone()),
params.as_ref().and_then(|p| p.max_header_length),
params.as_ref().and_then(|p| p.max_body_length),
params.as_ref().and_then(|p| p.ignore_body_hash_check),
// params.as_ref().and_then(|p| p.max_header_length),
// params.as_ref().and_then(|p| p.max_body_length),
// params.as_ref().and_then(|p| p.ignore_body_hash_check)
Some(640),
Some(768),
Some(false),
);
let email_circuit_inputs = generate_circuit_inputs(circuit_input_params);

let cleaned_body = match email_circuit_inputs.body_padded.clone() {
Some(body) => remove_quoted_printable_soft_breaks(body),
None => vec![],
};

let body_for_circuit = if true {
String::from_utf8(cleaned_body.clone()).unwrap_or_default()
} else {
parsed_email.canonicalized_body.clone()
};

let from_addr_idx = parsed_email.get_from_addr_idxes()?.0;
let domain_idx = parsed_email.get_email_domain_idxes()?.0;
let subject_idx = parsed_email.get_subject_all_idxes()?.0;
let code_idx = match parsed_email.get_invitation_code_idxes() {
let code_idx = match parsed_email.get_invitation_code_idxes(&body_for_circuit) {
Ok(indexes) => indexes.0,
Err(_) => 0,
};
let timestamp_idx = match parsed_email.get_timestamp_idxes() {
Ok(indexes) => indexes.0,
Err(_) => 0,
};
let command_idx = match parsed_email.get_command_idxes() {
let command_idx = match parsed_email.get_command_idxes(&body_for_circuit) {
Ok(indexes) => indexes.0,
Err(_) => 0,
};
Expand All @@ -186,6 +218,7 @@ pub async fn generate_email_circuit_input(
padded_body_len: email_circuit_inputs.body_len_padded_bytes,
precomputed_sha: email_circuit_inputs.precomputed_sha,
command_idx,
cleaned_body: Some(cleaned_body),
};

Ok(serde_json::to_string(&email_auth_input)?)
Expand Down
6 changes: 6 additions & 0 deletions src/node/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,17 @@ pub(crate) fn generate_email_circuit_input_node(mut cx: FunctionContext) -> JsRe
.ok()
.and_then(|val: Handle<JsValue>| val.downcast::<JsBoolean, _>(&mut cx).ok())
.map(|js_boolean| js_boolean.value(&mut cx));
let remove_soft_line_breaks = params
.get(&mut cx, "removeSoftLineBreaks")
.ok()
.and_then(|val: Handle<JsValue>| val.downcast::<JsBoolean, _>(&mut cx).ok())
.map(|js_boolean| js_boolean.value(&mut cx));
EmailCircuitParams {
sha_precompute_selector,
max_header_length,
max_body_length,
ignore_body_hash_check,
remove_soft_line_breaks,
}
});

Expand Down
14 changes: 7 additions & 7 deletions src/parse_email.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,17 @@ impl ParsedEmail {
}

/// Extracts the invitation code from the canonicalized email body.
pub fn get_invitation_code(&self) -> Result<String> {
pub fn get_invitation_code(&self, body: &str) -> Result<String> {
let regex_config = serde_json::from_str(include_str!("../regexes/invitation_code.json"))?;
let idxes = extract_substr_idxes(&self.canonicalized_body, &regex_config)?[0];
let str = self.canonicalized_body[idxes.0..idxes.1].to_string();
let idxes = extract_substr_idxes(body, &regex_config)?[0];
let str = body[idxes.0..idxes.1].to_string();
Ok(str)
}

/// Retrieves the index range of the invitation code within the canonicalized email body.
pub fn get_invitation_code_idxes(&self) -> Result<(usize, usize)> {
pub fn get_invitation_code_idxes(&self, body: &str) -> Result<(usize, usize)> {
let regex_config = serde_json::from_str(include_str!("../regexes/invitation_code.json"))?;
let idxes = extract_substr_idxes(&self.canonicalized_body, &regex_config)?[0];
let idxes = extract_substr_idxes(body, &regex_config)?[0];
Ok(idxes)
}

Expand Down Expand Up @@ -182,9 +182,9 @@ impl ParsedEmail {
Ok(str)
}

pub fn get_command_idxes(&self) -> Result<(usize, usize)> {
pub fn get_command_idxes(&self, body: &str) -> Result<(usize, usize)> {
let regex_config = serde_json::from_str(include_str!("../regexes/command.json"))?;
let idxes = extract_substr_idxes(&self.canonicalized_body, &regex_config)?[0];
let idxes = extract_substr_idxes(body, &regex_config)?[0];
Ok(idxes)
}
}

0 comments on commit 9b243cb

Please sign in to comment.