Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat Add container id validate #1602

Merged
merged 1 commit into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 57 additions & 2 deletions crates/libcontainer/src/container/builder.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{syscall::Syscall, utils::PathBufExt};
use anyhow::{Context, Result};
use anyhow::{anyhow, Context, Result};
use std::path::PathBuf;

use super::{init_builder::InitContainerBuilder, tenant_builder::TenantContainerBuilder};
Expand Down Expand Up @@ -50,7 +50,6 @@ impl<'a> ContainerBuilder<'a> {
/// ```
pub fn new(container_id: String, syscall: &'a dyn Syscall) -> Self {
let root_path = PathBuf::from("/run/youki");

Self {
container_id,
root_path,
Expand All @@ -61,6 +60,40 @@ impl<'a> ContainerBuilder<'a> {
}
}

/// validate_id checks if the supplied container ID is valid, returning
/// the ErrInvalidID in case it is not.
///
/// The format of valid ID was never formally defined, instead the code
/// was modified to allow or disallow specific characters.
///
/// Currently, a valid ID is a non-empty string consisting only of
/// the following characters:
/// - uppercase (A-Z) and lowercase (a-z) Latin letters;
/// - digits (0-9);
/// - underscore (_);
/// - plus sign (+);
/// - minus sign (-);
/// - period (.).
///
/// In addition, IDs that can't be used to represent a file name
/// (such as . or ..) are rejected.
pub fn validate_id(self) -> Result<Self> {
let container_id = self.container_id.clone();
if container_id.is_empty() {
return Err(anyhow!("invalid container ID format: {:?}", container_id));
}
if container_id == "." || container_id == ".." {
return Err(anyhow!("invalid container ID format: {:?}", container_id));
}
for c in container_id.chars() {
match c {
'a'..='z' | 'A'..='Z' | '0'..='9' | '_' | '+' | '-' | '.' => (),
_ => return Err(anyhow!("invalid container ID format: {:?}", container_id)),
}
}
Ok(self)
}

/// Transforms this builder into a tenant builder
/// # Example
///
Expand Down Expand Up @@ -227,4 +260,26 @@ mod tests {

Ok(())
}

#[test]
fn test_validate_id() -> Result<()> {
let syscall = create_syscall();
// validate container_id
let result = ContainerBuilder::new("$#".to_owned(), syscall.as_ref()).validate_id();
assert!(result.is_err());

let result = ContainerBuilder::new(".".to_owned(), syscall.as_ref()).validate_id();
assert!(result.is_err());

let result = ContainerBuilder::new("..".to_owned(), syscall.as_ref()).validate_id();
assert!(result.is_err());

let result = ContainerBuilder::new("...".to_owned(), syscall.as_ref()).validate_id();
assert!(result.is_ok());

let result =
ContainerBuilder::new("74f1a4cb3801".to_owned(), syscall.as_ref()).validate_id();
assert!(result.is_ok());
Ok(())
}
}
1 change: 1 addition & 0 deletions crates/youki/src/commands/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub fn create(args: Create, root_path: PathBuf, systemd_cgroup: bool) -> Result<
.with_console_socket(args.console_socket.as_ref())
.with_root_path(root_path)?
.with_preserved_fds(args.preserve_fds)
.validate_id()?
.as_init(&args.bundle)
.with_systemd(systemd_cgroup)
.build()?;
Expand Down
1 change: 1 addition & 0 deletions crates/youki/src/commands/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub fn exec(args: Exec, root_path: PathBuf) -> Result<i32> {
.with_root_path(root_path)?
.with_console_socket(args.console_socket.as_ref())
.with_pid_file(args.pid_file.as_ref())?
.validate_id()?
.as_tenant()
.with_detach(args.detach)
.with_cwd(args.cwd.as_ref())
Expand Down
1 change: 1 addition & 0 deletions crates/youki/src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub fn run(args: Run, root_path: PathBuf, systemd_cgroup: bool) -> Result<()> {
.with_console_socket(args.console_socket.as_ref())
.with_root_path(root_path)?
.with_preserved_fds(args.preserve_fds)
.validate_id()?
.as_init(&args.bundle)
.with_systemd(systemd_cgroup)
.build()?;
Expand Down