Skip to content

Commit 4e6be03

Browse files
committed
f Check namespace/key for control characters
... and use `PrintableString`
1 parent 100e201 commit 4e6be03

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

lightning-persister/src/fs_store.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Objects related to [`FilesystemStore`] live here.
22
use lightning::util::persist::KVStore;
3+
use lightning::util::string::PrintableString;
34

45
use std::collections::HashMap;
56
use std::fs;
@@ -53,7 +54,17 @@ impl KVStore for FilesystemStore {
5354

5455
fn read(&self, namespace: &str, key: &str) -> std::io::Result<Self::Reader> {
5556
if key.is_empty() {
56-
let msg = format!("Failed to read {}/{}: key may not be empty.", namespace, key);
57+
let msg = format!("Failed to read {}/{}: key may not be empty.",
58+
PrintableString(namespace), PrintableString(key));
59+
return Err(std::io::Error::new(std::io::ErrorKind::Other, msg));
60+
}
61+
62+
if namespace.chars().any(|c| !c.is_ascii() || c.is_control()) ||
63+
key.chars().any(|c| !c.is_ascii() || c.is_control()) {
64+
debug_assert!(false, "Failed to read {}/{}: namespace and key must be valid ASCII
65+
strings.", PrintableString(namespace), PrintableString(key));
66+
let msg = format!("Failed to read {}/{}: namespace and key must be valid ASCII strings.",
67+
PrintableString(namespace), PrintableString(key));
5768
return Err(std::io::Error::new(std::io::ErrorKind::Other, msg));
5869
}
5970

@@ -69,7 +80,17 @@ impl KVStore for FilesystemStore {
6980

7081
fn write(&self, namespace: &str, key: &str, buf: &[u8]) -> std::io::Result<()> {
7182
if key.is_empty() {
72-
let msg = format!("Failed to write {}/{}: key may not be empty.", namespace, key);
83+
let msg = format!("Failed to write {}/{}: key may not be empty.",
84+
PrintableString(namespace), PrintableString(key));
85+
return Err(std::io::Error::new(std::io::ErrorKind::Other, msg));
86+
}
87+
88+
if namespace.chars().any(|c| !c.is_ascii() || c.is_control()) ||
89+
key.chars().any(|c| !c.is_ascii() || c.is_control()) {
90+
debug_assert!(false, "Failed to write {}/{}: namespace and key must be valid ASCII
91+
strings.", PrintableString(namespace), PrintableString(key));
92+
let msg = format!("Failed to write {}/{}: namespace and key must be valid ASCII strings.",
93+
PrintableString(namespace), PrintableString(key));
7394
return Err(std::io::Error::new(std::io::ErrorKind::Other, msg));
7495
}
7596

@@ -144,7 +165,17 @@ impl KVStore for FilesystemStore {
144165

145166
fn remove(&self, namespace: &str, key: &str) -> std::io::Result<()> {
146167
if key.is_empty() {
147-
let msg = format!("Failed to remove {}/{}: key may not be empty.", namespace, key);
168+
let msg = format!("Failed to remove {}/{}: key may not be empty.",
169+
PrintableString(namespace), PrintableString(key));
170+
return Err(std::io::Error::new(std::io::ErrorKind::Other, msg));
171+
}
172+
173+
if namespace.chars().any(|c| !c.is_ascii() || c.is_control()) ||
174+
key.chars().any(|c| !c.is_ascii() || c.is_control()) {
175+
debug_assert!(false, "Failed to remove {}/{}: namespace and key must be valid ASCII
176+
strings.", PrintableString(namespace), PrintableString(key));
177+
let msg = format!("Failed to remove {}/{}: namespace and key must be valid ASCII strings.",
178+
PrintableString(namespace), PrintableString(key));
148179
return Err(std::io::Error::new(std::io::ErrorKind::Other, msg));
149180
}
150181

0 commit comments

Comments
 (0)