fix: replacing of sql placeholders replaces question marks in inserted values
This commit is contained in:
parent
911c8987b4
commit
41a83363b9
3 changed files with 41 additions and 26 deletions
20
Cargo.lock
generated
20
Cargo.lock
generated
|
@ -24,9 +24,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.94"
|
||||
version = "1.0.95"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c1fd03a028ef38ba2276dce7e33fcd6369c158a1bca17946c4b1b701891c1ff7"
|
||||
checksum = "34ac096ce696dc2fcabef30516bb13c0a68a11d30131d3df6f04711467681b04"
|
||||
|
||||
[[package]]
|
||||
name = "base64"
|
||||
|
@ -467,14 +467,14 @@ checksum = "46f859dbbf73865c6627ed570e78961cd3ac92407a2d117204c49232485da55e"
|
|||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.90",
|
||||
"syn 2.0.91",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.133"
|
||||
version = "1.0.134"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c7fceb2473b9166b2294ef05efcb65a3db80803f0b03ef86a5fc88a2b85ee377"
|
||||
checksum = "d00f4175c42ee48b15416f6193a959ba3a0d67fc699a0db9ad12df9f83991c7d"
|
||||
dependencies = [
|
||||
"itoa",
|
||||
"memchr",
|
||||
|
@ -495,7 +495,7 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "signal-decrypt-backup-wasm"
|
||||
version = "0.1.0"
|
||||
version = "0.1.1"
|
||||
dependencies = [
|
||||
"aes",
|
||||
"base64",
|
||||
|
@ -535,9 +535,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.90"
|
||||
version = "2.0.91"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "919d3b74a5dd0ccd15aeb8f93e7006bd9e14c295087c9896a110f490752bcf31"
|
||||
checksum = "d53cbcb5a243bd33b7858b1d7f4aca2153490815872d86d955d6ea29f743c035"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
|
@ -596,7 +596,7 @@ dependencies = [
|
|||
"log",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.90",
|
||||
"syn 2.0.91",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
||||
|
@ -618,7 +618,7 @@ checksum = "30d7a95b763d3c45903ed6c81f156801839e5ee968bb07e534c44df0fcd330c2"
|
|||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.90",
|
||||
"syn 2.0.91",
|
||||
"wasm-bindgen-backend",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "signal-decrypt-backup-wasm"
|
||||
version = "0.1.0"
|
||||
version = "0.1.1"
|
||||
edition = "2021"
|
||||
description = "Get the raw database from your Signal backup. Written for webassembly"
|
||||
repository = "https://git.duskflower.dev/duskflower/signal-decrypt-backup-wasm"
|
||||
|
|
45
src/lib.rs
45
src/lib.rs
|
@ -158,27 +158,33 @@ fn sql_parameter_to_string(
|
|||
fn process_parameter_placeholders(sql: &str, params: &[String]) -> Result<String, JsValue> {
|
||||
let mut result = sql.to_string();
|
||||
let mut param_index = 0;
|
||||
let mut str_index = 0;
|
||||
|
||||
while param_index < params.len() {
|
||||
let rest = &result[param_index..];
|
||||
|
||||
// Find the next placeholders
|
||||
// signal backups only use the standard type and not indexed or other ones
|
||||
let next_placeholder = rest.find('?').map(|i| (i, 1)); // ? style
|
||||
while str_index < result.len() {
|
||||
// Find the next placeholder
|
||||
let next_placeholder = result[str_index..].find('?');
|
||||
|
||||
match next_placeholder {
|
||||
Some((pos, len)) => {
|
||||
// Replace the placeholder with the parameter value
|
||||
if param_index < params.len() {
|
||||
let before = &result[..param_index + pos];
|
||||
let after = &result[param_index + pos + len..];
|
||||
result = format!("{}{}{}", before, params[param_index], after);
|
||||
param_index += 1;
|
||||
} else {
|
||||
Some(pos) => {
|
||||
// Calculate the actual position in the result string
|
||||
let actual_pos = str_index + pos;
|
||||
|
||||
// Check if we have enough parameters
|
||||
if param_index >= params.len() {
|
||||
return Err(JsValue::from_str(
|
||||
"Not enough parameters provided for SQL statement",
|
||||
));
|
||||
}
|
||||
|
||||
// Replace the placeholder with the parameter value
|
||||
let before = &result[..actual_pos];
|
||||
let after = &result[actual_pos + 1..]; // Skip the placeholder '?'
|
||||
|
||||
// Update str_index to the new position after the replacement
|
||||
str_index = before.len() + params[param_index].len();
|
||||
|
||||
result = format!("{}{}{}", before, params[param_index], after);
|
||||
param_index += 1;
|
||||
}
|
||||
None => {
|
||||
// No more placeholders found
|
||||
|
@ -554,7 +560,16 @@ impl BackupDecryptor {
|
|||
.map(|param| sql_parameter_to_string(param))
|
||||
.collect::<Result<_, _>>()?;
|
||||
|
||||
process_parameter_placeholders(&sql, ¶ms)?
|
||||
let sql_string = process_parameter_placeholders(&sql, ¶ms)?;
|
||||
|
||||
if sql_string.contains("?") {
|
||||
return Err(JsValue::from_str(&format!(
|
||||
"found unreplaced placeholder: sql: {}\n final sql string: {}\n params: {:?}",
|
||||
sql, sql_string, params
|
||||
)));
|
||||
}
|
||||
|
||||
sql_string
|
||||
} else {
|
||||
sql
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue