Skip to content

Commit b6c8f06

Browse files
committed
Add tests for invalid plugin name chars
1 parent 3e3e6a8 commit b6c8f06

5 files changed

Lines changed: 106 additions & 0 deletions

File tree

age/src/plugin.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,3 +461,65 @@ impl<C: Callbacks> crate::Identity for IdentityPluginV1<C> {
461461
self.unwrap_stanzas(stanzas.iter())
462462
}
463463
}
464+
465+
#[cfg(test)]
466+
mod tests {
467+
use crate::{Callbacks, DecryptError, EncryptError};
468+
469+
use super::{
470+
Identity, IdentityPluginV1, Recipient, RecipientPluginV1, PLUGIN_IDENTITY_PREFIX,
471+
PLUGIN_RECIPIENT_PREFIX,
472+
};
473+
474+
const INVALID_PLUGIN_NAME: &str = "foobar/../../../../../../../usr/bin/echo";
475+
476+
struct NoCallbacks;
477+
impl Callbacks for NoCallbacks {
478+
fn prompt(&self, _: &str) {}
479+
fn request_public_string(&self, _: &str) -> Option<String> {
480+
None
481+
}
482+
fn request_passphrase(&self, _: &str) -> Option<secrecy::SecretString> {
483+
None
484+
}
485+
}
486+
487+
#[test]
488+
fn recipient_rejects_invalid_chars() {
489+
let invalid_recipient = bech32::encode(
490+
&format!("{}{}", PLUGIN_RECIPIENT_PREFIX, INVALID_PLUGIN_NAME),
491+
[],
492+
bech32::Variant::Bech32,
493+
)
494+
.unwrap();
495+
assert!(invalid_recipient.parse::<Recipient>().is_err());
496+
}
497+
498+
#[test]
499+
fn identity_rejects_invalid_chars() {
500+
let invalid_identity = bech32::encode(
501+
&format!("{}{}-", PLUGIN_IDENTITY_PREFIX, INVALID_PLUGIN_NAME),
502+
[],
503+
bech32::Variant::Bech32,
504+
)
505+
.expect("HRP is valid")
506+
.to_uppercase();
507+
assert!(invalid_identity.parse::<Identity>().is_err());
508+
}
509+
510+
#[test]
511+
fn recipient_plugin_v1_rejects_invalid_chars() {
512+
assert!(matches!(
513+
RecipientPluginV1::new(INVALID_PLUGIN_NAME, &[], &[], NoCallbacks),
514+
Err(EncryptError::MissingPlugin { binary_name }) if binary_name == INVALID_PLUGIN_NAME,
515+
));
516+
}
517+
518+
#[test]
519+
fn identity_plugin_v1_rejects_invalid_chars() {
520+
assert!(matches!(
521+
IdentityPluginV1::new(INVALID_PLUGIN_NAME, &[], NoCallbacks),
522+
Err(DecryptError::MissingPlugin { binary_name }) if binary_name == INVALID_PLUGIN_NAME,
523+
));
524+
}
525+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-----BEGIN AGE ENCRYPTED FILE-----
2+
YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBHUGc3Zlhpekp0K012aXdu
3+
T1VZN0lmWlRmNjdLYVB4RldkTFVLTkNDUXlBCmJjRUcrM3E0a0U0N3IyK1JsTitG
4+
dHVTd0N6TVFRTWgzdG5uSzJmNm9YMTgKLT4gQXQ1WWAtZ3JlYXNlIDxodGFSVHJg
5+
IFg0cWYsO0ogZ2Fzc1EKZGtPSTB3Ci0tLSBKazRIaHJxdnNJcHpyclRkQjg3QW5r
6+
SVE2MHdtWkErYTNrNWJibWd1bmNBCkK9FoOkiLB93gD79vNed8L3LM9rhKm5qma2
7+
lSiwRx/aM1DKaZO0CMmYQkoM2tPReA==
8+
-----END AGE ENCRYPTED FILE-----
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
bin.name = "rage"
2+
args = "--decrypt --identity - file.age.txt"
3+
status = "failed"
4+
stdin = """
5+
AGE-PLUGIN-FOOBAR/../../../../../../../USR/BIN/ECHO-1HKGPY3
6+
"""
7+
stdout = ""
8+
stderr = """
9+
Error: identity file contains non-identity data on line 1
10+
11+
[ Did rage not do what you expected? Could an error be more useful? ]
12+
[ Tell us: https://str4d.xyz/rage/report ]
13+
"""
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
bin.name = "rage"
2+
args = "--decrypt -j foobar/../../../../../../../usr/bin/echo"
3+
status = "failed"
4+
stdin = ""
5+
stdout = ""
6+
stderr = """
7+
Error: Could not find 'foobar/../../../../../../../usr/bin/echo' on the PATH.
8+
Have you installed the plugin?
9+
10+
[ Did rage not do what you expected? Could an error be more useful? ]
11+
[ Tell us: https://str4d.xyz/rage/report ]
12+
"""
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
bin.name = "rage"
2+
args = "--encrypt --recipient age1foobar/../../../../../../../usr/bin/echo1849l6e"
3+
status = "failed"
4+
stdin = ""
5+
stdout = ""
6+
stderr = """
7+
Error: Invalid recipient 'age1foobar/../../../../../../../usr/bin/echo1849l6e'.
8+
9+
[ Did rage not do what you expected? Could an error be more useful? ]
10+
[ Tell us: https://str4d.xyz/rage/report ]
11+
"""

0 commit comments

Comments
 (0)