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]]
|
[[package]]
|
||||||
name = "anyhow"
|
name = "anyhow"
|
||||||
version = "1.0.94"
|
version = "1.0.95"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c1fd03a028ef38ba2276dce7e33fcd6369c158a1bca17946c4b1b701891c1ff7"
|
checksum = "34ac096ce696dc2fcabef30516bb13c0a68a11d30131d3df6f04711467681b04"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "base64"
|
name = "base64"
|
||||||
|
@ -467,14 +467,14 @@ checksum = "46f859dbbf73865c6627ed570e78961cd3ac92407a2d117204c49232485da55e"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.90",
|
"syn 2.0.91",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde_json"
|
name = "serde_json"
|
||||||
version = "1.0.133"
|
version = "1.0.134"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c7fceb2473b9166b2294ef05efcb65a3db80803f0b03ef86a5fc88a2b85ee377"
|
checksum = "d00f4175c42ee48b15416f6193a959ba3a0d67fc699a0db9ad12df9f83991c7d"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"itoa",
|
"itoa",
|
||||||
"memchr",
|
"memchr",
|
||||||
|
@ -495,7 +495,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "signal-decrypt-backup-wasm"
|
name = "signal-decrypt-backup-wasm"
|
||||||
version = "0.1.0"
|
version = "0.1.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"aes",
|
"aes",
|
||||||
"base64",
|
"base64",
|
||||||
|
@ -535,9 +535,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "syn"
|
name = "syn"
|
||||||
version = "2.0.90"
|
version = "2.0.91"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "919d3b74a5dd0ccd15aeb8f93e7006bd9e14c295087c9896a110f490752bcf31"
|
checksum = "d53cbcb5a243bd33b7858b1d7f4aca2153490815872d86d955d6ea29f743c035"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
|
@ -596,7 +596,7 @@ dependencies = [
|
||||||
"log",
|
"log",
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.90",
|
"syn 2.0.91",
|
||||||
"wasm-bindgen-shared",
|
"wasm-bindgen-shared",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -618,7 +618,7 @@ checksum = "30d7a95b763d3c45903ed6c81f156801839e5ee968bb07e534c44df0fcd330c2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.90",
|
"syn 2.0.91",
|
||||||
"wasm-bindgen-backend",
|
"wasm-bindgen-backend",
|
||||||
"wasm-bindgen-shared",
|
"wasm-bindgen-shared",
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "signal-decrypt-backup-wasm"
|
name = "signal-decrypt-backup-wasm"
|
||||||
version = "0.1.0"
|
version = "0.1.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
description = "Get the raw database from your Signal backup. Written for webassembly"
|
description = "Get the raw database from your Signal backup. Written for webassembly"
|
||||||
repository = "https://git.duskflower.dev/duskflower/signal-decrypt-backup-wasm"
|
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> {
|
fn process_parameter_placeholders(sql: &str, params: &[String]) -> Result<String, JsValue> {
|
||||||
let mut result = sql.to_string();
|
let mut result = sql.to_string();
|
||||||
let mut param_index = 0;
|
let mut param_index = 0;
|
||||||
|
let mut str_index = 0;
|
||||||
|
|
||||||
while param_index < params.len() {
|
while str_index < result.len() {
|
||||||
let rest = &result[param_index..];
|
// Find the next placeholder
|
||||||
|
let next_placeholder = result[str_index..].find('?');
|
||||||
// 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
|
|
||||||
|
|
||||||
match next_placeholder {
|
match next_placeholder {
|
||||||
Some((pos, len)) => {
|
Some(pos) => {
|
||||||
// Replace the placeholder with the parameter value
|
// Calculate the actual position in the result string
|
||||||
if param_index < params.len() {
|
let actual_pos = str_index + pos;
|
||||||
let before = &result[..param_index + pos];
|
|
||||||
let after = &result[param_index + pos + len..];
|
// Check if we have enough parameters
|
||||||
result = format!("{}{}{}", before, params[param_index], after);
|
if param_index >= params.len() {
|
||||||
param_index += 1;
|
|
||||||
} else {
|
|
||||||
return Err(JsValue::from_str(
|
return Err(JsValue::from_str(
|
||||||
"Not enough parameters provided for SQL statement",
|
"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 => {
|
None => {
|
||||||
// No more placeholders found
|
// No more placeholders found
|
||||||
|
@ -554,7 +560,16 @@ impl BackupDecryptor {
|
||||||
.map(|param| sql_parameter_to_string(param))
|
.map(|param| sql_parameter_to_string(param))
|
||||||
.collect::<Result<_, _>>()?;
|
.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 {
|
} else {
|
||||||
sql
|
sql
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue