Skip to content

Commit

Permalink
Update KV store key naming restrictions
Browse files Browse the repository at this point in the history
  • Loading branch information
joeshaw committed Jan 28, 2025
1 parent 76d19e6 commit 61d072b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
**/target/
publish
vendor/
verify-publishable/
.vscode
33 changes: 23 additions & 10 deletions cli/tests/integration/kv_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,10 +446,10 @@ viceroy_test!(kv_store_bad_key_values, |is_component| {
authors = ["Jill Bryson <[email protected]>", "Rose McDowall <[email protected]>"]
language = "rust"
[local_server]
kv_stores.store_one = [{key = "howdy[", data = "This is some data"}]
kv_stores.store_one = [{key = "howdy#", data = "This is some data"}]
"#;
match Test::using_fixture("kv_store.wasm").adapt_component(is_component).using_fastly_toml(BAD_8_FASTLY_TOML) {
Err(e) => assert_eq!("invalid configuration for 'store_one': Invalid `key` value used: Keys for objects cannot contain a `[`.", &e.to_string()),
Err(e) => assert_eq!("invalid configuration for 'store_one': Invalid `key` value used: Keys for objects cannot contain a `#`.", &e.to_string()),
_ => panic!(),
}

Expand All @@ -459,10 +459,10 @@ viceroy_test!(kv_store_bad_key_values, |is_component| {
authors = ["Jill Bryson <[email protected]>", "Rose McDowall <[email protected]>"]
language = "rust"
[local_server]
kv_stores.store_one = [{key = "hello]", data = "This is some data"}]
kv_stores.store_one = [{key = "hello;", data = "This is some data"}]
"#;
match Test::using_fixture("kv_store.wasm").adapt_component(is_component).using_fastly_toml(BAD_9_FASTLY_TOML) {
Err(e) => assert_eq!("invalid configuration for 'store_one': Invalid `key` value used: Keys for objects cannot contain a `]`.", &e.to_string()),
Err(e) => assert_eq!("invalid configuration for 'store_one': Invalid `key` value used: Keys for objects cannot contain a `;`.", &e.to_string()),
_ => panic!(),
}

Expand All @@ -472,10 +472,10 @@ viceroy_test!(kv_store_bad_key_values, |is_component| {
authors = ["Jill Bryson <[email protected]>", "Rose McDowall <[email protected]>"]
language = "rust"
[local_server]
kv_stores.store_one = [{key = "yoohoo*", data = "This is some data"}]
kv_stores.store_one = [{key = "yoohoo?", data = "This is some data"}]
"#;
match Test::using_fixture("kv_store.wasm").adapt_component(is_component).using_fastly_toml(BAD_10_FASTLY_TOML) {
Err(e) => assert_eq!("invalid configuration for 'store_one': Invalid `key` value used: Keys for objects cannot contain a `*`.", &e.to_string()),
Err(e) => assert_eq!("invalid configuration for 'store_one': Invalid `key` value used: Keys for objects cannot contain a `?`.", &e.to_string()),
_ => panic!(),
}

Expand All @@ -485,10 +485,10 @@ viceroy_test!(kv_store_bad_key_values, |is_component| {
authors = ["Jill Bryson <[email protected]>", "Rose McDowall <[email protected]>"]
language = "rust"
[local_server]
kv_stores.store_one = [{key = "hey?", data = "This is some data"}]
kv_stores.store_one = [{key = "hey^", data = "This is some data"}]
"#;
match Test::using_fixture("kv_store.wasm").adapt_component(is_component).using_fastly_toml(BAD_11_FASTLY_TOML) {
Err(e) => assert_eq!("invalid configuration for 'store_one': Invalid `key` value used: Keys for objects cannot contain a `?`.", &e.to_string()),
Err(e) => assert_eq!("invalid configuration for 'store_one': Invalid `key` value used: Keys for objects cannot contain a `^`.", &e.to_string()),
_ => panic!(),
}

Expand All @@ -498,10 +498,23 @@ viceroy_test!(kv_store_bad_key_values, |is_component| {
authors = ["Jill Bryson <[email protected]>", "Rose McDowall <[email protected]>"]
language = "rust"
[local_server]
kv_stores.store_one = [{key = "ello ello#", data = "This is some data"}]
kv_stores.store_one = [{key = "ello ello|", data = "This is some data"}]
"#;
match Test::using_fixture("kv_store.wasm").adapt_component(is_component).using_fastly_toml(BAD_12_FASTLY_TOML) {
Err(e) => assert_eq!("invalid configuration for 'store_one': Invalid `key` value used: Keys for objects cannot contain a `#`.", &e.to_string()),
Err(e) => assert_eq!("invalid configuration for 'store_one': Invalid `key` value used: Keys for objects cannot contain a `|`.", &e.to_string()),
_ => panic!(),
}

const BAD_13_FASTLY_TOML: &str = r#"
name = "kv-store-test"
description = "kv store test"
authors = ["Jill Bryson <[email protected]>", "Rose McDowall <[email protected]>"]
language = "rust"
[local_server]
kv_stores.store_one = [{key = " ", data = "This is some data"}]
"#;
match Test::using_fixture("kv_store.wasm").adapt_component(is_component).using_fastly_toml(BAD_13_FASTLY_TOML) {
Err(e) => assert_eq!("invalid configuration for 'store_one': Invalid `key` value used: Keys for objects cannot contain a `\\u{20}`.", &e.to_string()),
_ => panic!(),
}

Expand Down
31 changes: 23 additions & 8 deletions lib/src/object_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,8 @@ impl From<&KvStoreError> for FastlyStatus {
/// * Keys cannot contain Carriage Return or Line Feed characters.
/// * Keys cannot start with `.well-known/acme-challenge/`.
/// * Keys cannot be named `.` or `..`.
/// * Keys cannot use Unicode characters 0 through 32, 65534 and 65535 as
/// single-character key names. (0x0 through 0x20, 0xFFFE and 0xFFFF)
fn is_valid_key(key: &str) -> Result<(), KeyValidationError> {
let len = key.as_bytes().len();
if len < 1 {
Expand All @@ -459,16 +461,29 @@ fn is_valid_key(key: &str) -> Result<(), KeyValidationError> {
return Err(KeyValidationError::Contains("\r".to_owned()));
} else if key.contains('\n') {
return Err(KeyValidationError::Contains("\n".to_owned()));
} else if key.contains('[') {
return Err(KeyValidationError::Contains("[".to_owned()));
} else if key.contains(']') {
return Err(KeyValidationError::Contains("]".to_owned()));
} else if key.contains('*') {
return Err(KeyValidationError::Contains("*".to_owned()));
} else if key.contains('?') {
return Err(KeyValidationError::Contains("?".to_owned()));
} else if key.contains('#') {
return Err(KeyValidationError::Contains("#".to_owned()));
} else if key.contains(';') {
return Err(KeyValidationError::Contains(";".to_owned()));
} else if key.contains('?') {
return Err(KeyValidationError::Contains("?".to_owned()));
} else if key.contains('^') {
return Err(KeyValidationError::Contains("^".to_owned()));
} else if key.contains('|') {
return Err(KeyValidationError::Contains("|".to_owned()));
}

if key.len() == 1 {
let k = key.chars().next().unwrap();
match k {
'\u{0}'..='\u{20}' => {
return Err(KeyValidationError::Contains(k.escape_unicode().to_string()));
}
'\u{FFFE}'..='\u{FFFF}' => {
return Err(KeyValidationError::Contains(k.escape_unicode().to_string()));
}
_ => {}
}
}

Ok(())
Expand Down

0 comments on commit 61d072b

Please sign in to comment.