Skip to content

Commit

Permalink
fix: trim base64 file input
Browse files Browse the repository at this point in the history
  • Loading branch information
robjtede committed Jun 7, 2024
1 parent 05863d9 commit e64b275
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ updates:
- package-ecosystem: github-actions
directory: /
schedule:
interval: weekly
interval: monthly
- package-ecosystem: cargo
directory: /
schedule:
Expand Down
10 changes: 3 additions & 7 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ jobs:
- { os: macos-latest, target: x86_64-apple-darwin }
- { os: ubuntu-latest, target: aarch64-unknown-linux-gnu }
- { os: ubuntu-latest, target: aarch64-unknown-linux-musl }
# - { os: ubuntu-latest, target: x86_64-unknown-freebsd } # not supported by aws-lc
- { os: ubuntu-latest, target: x86_64-unknown-freebsd }
- { os: ubuntu-latest, target: x86_64-unknown-linux-gnu }
- { os: ubuntu-latest, target: x86_64-unknown-linux-musl }
# - { os: windows-latest, target: aarch64-pc-windows-msvc } # not supported by aws-lc
- { os: windows-latest, target: aarch64-pc-windows-msvc }
- { os: windows-latest, target: x86_64-pc-windows-msvc }

name: Deploy (${{ matrix.target }})
Expand All @@ -44,10 +44,6 @@ jobs:
steps:
- uses: actions/checkout@v4

- name: Install nasm
if: matrix.target.os == 'windows-latest'
uses: ilammy/[email protected]

- name: Install Rust
uses: actions-rust-lang/[email protected]
with:
Expand All @@ -73,7 +69,7 @@ jobs:
id: upload-release
uses: taiki-e/[email protected]
with:
bin: inspect-cert-chain
bin: protobug
target: ${{ matrix.target }}
tar: all
zip: all
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
name: Release

on:
push: { branches: [main] }
push:
branches: [main]

permissions:
contents: write
Expand Down
23 changes: 15 additions & 8 deletions protobug/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ enum Commands {
schema: Utf8PathBuf,
},

/// Inspects a protobuf file (base64) using a schema.
Inspect {
#[arg(long)]
schema: Utf8PathBuf,
Expand Down Expand Up @@ -89,11 +90,17 @@ fn inspect(schema: Utf8PathBuf, file: Utf8PathBuf) -> Result<(), Inspect> {

let md = fd.message_by_package_relative_name(&msg_name).unwrap();

let file_contents = fs::read_to_string(file).change_context(Inspect)?;
let decoded_message = decode_any_base64(&file_contents);
let file_contents = fs::read_to_string(&file)
.attach_printable_lazy(|| format!("File: {file}"))
.change_context(Inspect)?;

let decoded_message = decode_any_base64(file_contents.trim_end())
.attach_printable_lazy(|| format!("File: {file}"))
.change_context(Inspect)?;

let msg = md
.parse_from_bytes(&decoded_message)
.attach_printable_lazy(|| format!("File: {file}"))
.change_context(Inspect)?;

let mut tui = tui::init().change_context(Inspect)?;
Expand Down Expand Up @@ -142,10 +149,10 @@ fn validate_schema(schema_path: Utf8PathBuf) -> Result<(), anyhow::Error> {
Ok(())
}

fn decode_any_base64(encoded: &str) -> Vec<u8> {
None.or_else(|| BASE64_STANDARD.decode(encoded).ok())
.or_else(|| BASE64_STANDARD_NO_PAD.decode(encoded).ok())
.or_else(|| BASE64_URL_SAFE.decode(encoded).ok())
.or_else(|| BASE64_URL_SAFE_NO_PAD.decode(encoded).ok())
.unwrap()
fn decode_any_base64(encoded: &str) -> Result<Vec<u8>, base64::DecodeError> {
Ok(BASE64_STANDARD
.decode(encoded)
.or_else(|_| BASE64_STANDARD_NO_PAD.decode(encoded))
.or_else(|_| BASE64_URL_SAFE.decode(encoded))
.or_else(|_| BASE64_URL_SAFE_NO_PAD.decode(encoded))?)
}

0 comments on commit e64b275

Please sign in to comment.