Skip to content

Commit 5c555c0

Browse files
committed
fixup: Do not leak DecodingKey
1 parent 86207d5 commit 5c555c0

File tree

2 files changed

+18
-21
lines changed

2 files changed

+18
-21
lines changed

rust/auth-impls/src/lib.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@
1414
use api::auth::{AuthResponse, Authorizer};
1515
use api::error::VssError;
1616
use async_trait::async_trait;
17-
use jsonwebtoken::{decode, Algorithm, Validation};
17+
use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation};
1818
use serde::{Deserialize, Serialize};
1919
use std::collections::HashMap;
2020

21-
pub use jsonwebtoken::DecodingKey;
22-
2321
/// A JWT based authorizer, only allows requests with verified 'JsonWebToken' signed by the given
2422
/// issuer key.
2523
///
@@ -43,9 +41,11 @@ pub(crate) struct Claims {
4341
const BEARER_PREFIX: &str = "Bearer ";
4442

4543
impl JWTAuthorizer {
46-
/// Create new instance of [`JWTAuthorizer`]
47-
pub async fn new(jwt_issuer_key: DecodingKey) -> Self {
48-
Self { jwt_issuer_key }
44+
/// Creates a new instance of [`JWTAuthorizer`], fails on failure to parse the PEM formatted RSA public key
45+
pub async fn new(rsa_pem: &str) -> Result<Self, String> {
46+
let jwt_issuer_key =
47+
DecodingKey::from_rsa_pem(rsa_pem.as_bytes()).map_err(|e| e.to_string())?;
48+
Ok(Self { jwt_issuer_key })
4949
}
5050
}
5151

@@ -76,7 +76,7 @@ mod tests {
7676
use crate::JWTAuthorizer;
7777
use api::auth::Authorizer;
7878
use api::error::VssError;
79-
use jsonwebtoken::{encode, Algorithm, DecodingKey, EncodingKey, Header};
79+
use jsonwebtoken::{encode, Algorithm, EncodingKey, Header};
8080
use serde::{Deserialize, Serialize};
8181
use std::collections::HashMap;
8282
use std::time::SystemTime;
@@ -134,7 +134,7 @@ mod tests {
134134
)
135135
.expect("Failed to create Encoding Key.");
136136

137-
let decoding_key = DecodingKey::from_rsa_pem(
137+
let decoding_key = String::from(
138138
"-----BEGIN PUBLIC KEY-----\
139139
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAysGpKU+I9i9b+QZSANu/\
140140
ExaA6w4qiQdFZaXeReiz49r1oDfABwKIFW9gK/kNnrnL9H8P+pYfj7jqUJ/glmgq\
@@ -143,12 +143,10 @@ mod tests {
143143
8YsTa5piV8KgJpG/rwYTGXuu3lcCmnWwjmbeDq1zFFrCDDVkaIHkGJgRuFIDPXaH\
144144
yUw5H2HvKlP94ySbvTDLXWZj6TyzHEHDbstqs4DgvurB/bIhi/dQ7zK3EIXL8KRB\
145145
hwIDAQAB\
146-
-----END PUBLIC KEY-----"
147-
.as_bytes(),
148-
)
149-
.expect("Failed to create Decoding Key.");
146+
-----END PUBLIC KEY-----",
147+
);
150148

151-
let jwt_authorizer = JWTAuthorizer::new(decoding_key).await;
149+
let jwt_authorizer = JWTAuthorizer::new(&decoding_key).await.unwrap();
152150

153151
let valid_jwt_token =
154152
encode(&Header::new(Algorithm::RS256), &claims, &valid_encoding_key).unwrap();

rust/server/src/main.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,24 @@
1010
#![deny(missing_docs)]
1111

1212
use std::net::SocketAddr;
13+
use std::sync::Arc;
1314

1415
use tokio::net::TcpListener;
1516
use tokio::signal::unix::SignalKind;
1617

1718
use hyper::server::conn::http1;
1819
use hyper_util::rt::TokioIo;
1920

20-
use crate::vss_service::VssService;
2121
use api::auth::{Authorizer, NoopAuthorizer};
2222
use api::kv_store::KvStore;
23-
use auth_impls::{DecodingKey, JWTAuthorizer};
23+
use auth_impls::JWTAuthorizer;
2424
use impls::postgres_store::{Certificate, PostgresPlaintextBackend, PostgresTlsBackend};
25-
use std::sync::Arc;
25+
use util::config::{Config, ServerConfig};
26+
use vss_service::VssService;
2627

2728
mod util;
2829
mod vss_service;
2930

30-
use util::config::{Config, ServerConfig};
31-
3231
fn main() {
3332
let args: Vec<String> = std::env::args().collect();
3433
if args.len() != 2 {
@@ -79,15 +78,15 @@ fn main() {
7978
};
8079
let rsa_pem = rsa_pem_env.or(jwt_auth_config.map(|config| config.rsa_pem));
8180
let authorizer: Arc<dyn Authorizer> = if let Some(pem) = rsa_pem {
82-
let rsa_public_key = match DecodingKey::from_rsa_pem(pem.as_bytes()) {
83-
Ok(p) => p,
81+
let authorizer = match JWTAuthorizer::new(pem.as_str()).await {
82+
Ok(auth) => auth,
8483
Err(e) => {
8584
println!("Failed to parse the PEM formatted RSA public key: {}", e);
8685
std::process::exit(-1);
8786
},
8887
};
8988
println!("Configured JWT authorizer with RSA public key");
90-
Arc::new(JWTAuthorizer::new(rsa_public_key).await)
89+
Arc::new(authorizer)
9190
} else {
9291
println!("No JWT authentication method configured");
9392
Arc::new(NoopAuthorizer {})

0 commit comments

Comments
 (0)