From 24a08a7d829dd780e1e3612fe4cd8519fef700f9 Mon Sep 17 00:00:00 2001 From: Dima Date: Wed, 11 Nov 2026 19:18:01 +0100 Subject: [PATCH] fix: reject malformed Base64 padding consistently on .NET Framework --- .github/workflows/base64-source-check.yml | 17 +++++++++++++++++ .../Base64String/Base64String.Tests.ps1 | 3 +++ Shared-Functions/Base64String/Base64String.ps1 | 6 ++++++ 3 files changed, 26 insertions(+) create mode 100644 .github/workflows/base64-source-check.yml diff --git a/.github/workflows/base64-source-check.yml b/.github/workflows/base64-source-check.yml new file mode 100644 index 0000000..a35c7fb --- /dev/null +++ b/.github/workflows/base64-source-check.yml @@ -0,0 +1,17 @@ +name: Base64String source compatibility +on: + push: + branches: [fix/base64-windows-padding, base64tring/develop] + pull_request: + branches: [base64tring/develop] +permissions: + contents: read +jobs: + source: + uses: stadub/PowershellScripts/.github/workflows/validate-release-candidate.yml@c5413b357e91d185c0e6698f776d83bd02131921 + with: + module: Base64String + version: '1.2.0' + source_sha: ${{ github.event.pull_request.head.sha || github.sha }} + automation_sha: c5413b357e91d185c0e6698f776d83bd02131921 + require_committed_package: false diff --git a/Shared-Functions/Base64String/Base64String.Tests.ps1 b/Shared-Functions/Base64String/Base64String.Tests.ps1 index 87ec9f4..ca1b655 100644 --- a/Shared-Functions/Base64String/Base64String.Tests.ps1 +++ b/Shared-Functions/Base64String/Base64String.Tests.ps1 @@ -101,6 +101,9 @@ Describe 'Base64String encoding compatibility' { @{ InputText = 'AAAAA' } @{ InputText = 'Zg=' } @{ InputText = 'Zm8==' } + @{ InputText = 'Zg======' } + @{ InputText = 'Zm9v====' } + @{ InputText = '====' } @{ InputText = 'Zg===' } @{ InputText = '=Zg=' } @{ InputText = 'Z=g=' } diff --git a/Shared-Functions/Base64String/Base64String.ps1 b/Shared-Functions/Base64String/Base64String.ps1 index b58688c..6cb7f16 100644 --- a/Shared-Functions/Base64String/Base64String.ps1 +++ b/Shared-Functions/Base64String/Base64String.ps1 @@ -74,6 +74,12 @@ Function ConvertFrom-Base64String { 3 { $normalized += '=' } } } + # .NET Framework accepts some over-padded input (for example Zm8==) + # that modern .NET rejects. Validate padding shape before either runtime. + if ($normalized.Contains('=') -and + ($normalized.Length % 4 -ne 0 -or $normalized -cnotmatch '^[A-Za-z0-9+/]+={1,2}$')) { + throw (New-Object System.FormatException -ArgumentList 'Invalid Base64 length or padding.') + } try { $bytes = [System.Convert]::FromBase64String($normalized) }