From 92a84f733caf7714871054a6dca61bae6e859ec7 Mon Sep 17 00:00:00 2001 From: Ofek Lev Date: Thu, 22 Jun 2023 13:13:58 -0400 Subject: [PATCH] Properly handle cases where options contain line feed characters --- Cargo.toml | 4 +++- build.rs | 7 +++++-- docs/changelog.md | 4 ++++ src/app.rs | 14 ++++++++++++-- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7f924ac..70ca4be 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ edition = "2021" [dependencies] anyhow = "1.0.71" +base64 = "0.21.2" bzip2 = "0.4.4" clap = { version = "4.2.5", features = ["derive"] } directories = "5.0.0" @@ -23,9 +24,10 @@ zip = { version = "0.6.4", features = ["deflate"] } zstd = "0.12.3" [build-dependencies] -rand = "0.8.5" +base64 = "0.21.2" flate2 = "1.0.26" highway = "1.0.0" +rand = "0.8.5" regex = "1.8.1" reqwest = { version = "0.11", features = ["blocking", "rustls-tls"], default-features = false } tar = "0.4.38" diff --git a/build.rs b/build.rs index 785fec5..f6d2f76 100644 --- a/build.rs +++ b/build.rs @@ -5,6 +5,7 @@ use std::hash::{Hash, Hasher}; use std::io::Read; use std::path::PathBuf; +use base64::{engine::general_purpose::STANDARD_NO_PAD, Engine as _}; use highway::PortableHash; use rand::distributions::{Alphanumeric, DistString}; use regex::Regex; @@ -349,10 +350,12 @@ fn set_project_dependency_file(dependency_file: &str) { } let file_name = path.file_name().unwrap().to_str().unwrap(); + let contents = fs::read_to_string(dependency_file) + .unwrap_or_else(|_| panic!("\n\nFailed to read dependency file {dependency_file}\n\n")); + set_runtime_variable( "PYAPP_PROJECT_DEPENDENCY_FILE", - fs::read_to_string(dependency_file) - .unwrap_or_else(|_| panic!("\n\nFailed to read dependency file {dependency_file}\n\n")), + STANDARD_NO_PAD.encode(contents.as_bytes()), ); set_runtime_variable("PYAPP__PROJECT_DEPENDENCY_FILE_NAME", file_name); } diff --git a/docs/changelog.md b/docs/changelog.md index 6e2b716..009e6ba 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -8,6 +8,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ## Unreleased +***Fixed:*** + +- Properly handle cases where options contain line feed characters + ## 0.9.0 - 2023-06-21 ***Changed:*** diff --git a/src/app.rs b/src/app.rs index 0eef33e..dcadec7 100644 --- a/src/app.rs +++ b/src/app.rs @@ -2,6 +2,7 @@ use std::env; use std::path::{Path, PathBuf}; use anyhow::{Context, Result}; +use base64::{engine::general_purpose::STANDARD_NO_PAD, Engine as _}; use directories::ProjectDirs; use once_cell::sync::OnceCell; @@ -23,6 +24,15 @@ pub fn initialize() -> Result<()> { Ok(()) } +fn decode_option(encoded: &'static str) -> String { + String::from_utf8( + STANDARD_NO_PAD + .decode(encoded) + .unwrap_or_else(|_| panic!("{} is not valid base64", encoded)), + ) + .unwrap_or_else(|_| panic!("{} is not valid UTF-8", encoded)) +} + pub fn embedded_distribution() -> &'static [u8] { // If this is empty, then the distribution will be downloaded at runtime include_bytes!("embed/distribution") @@ -78,7 +88,7 @@ pub fn project_version() -> String { } pub fn project_dependency_file() -> String { - env!("PYAPP_PROJECT_DEPENDENCY_FILE").into() + decode_option(env!("PYAPP_PROJECT_DEPENDENCY_FILE")) } pub fn project_dependency_file_name() -> String { @@ -94,7 +104,7 @@ pub fn exec_module() -> String { } pub fn exec_code() -> String { - env!("PYAPP_EXEC_CODE").into() + decode_option(env!("PYAPP_EXEC_CODE")) } pub fn pip_extra_args() -> String {