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

@base64d does not handle web-safe encoded base64 #2262

Open
osresearch opened this issue Feb 1, 2021 · 2 comments
Open

@base64d does not handle web-safe encoded base64 #2262

osresearch opened this issue Feb 1, 2021 · 2 comments

Comments

@osresearch
Copy link

Describe the bug
Many programs output "web safe" or "filename safe" base64 (defined in RFC 4648 and called base64url). In this format the / and + characters are replaced with _ and -, and the = padding is often URI escaped with %3D. The @base64d string formatter does not handle this variant.

To Reproduce

$ echo '_-8%3D' | jq -R -r '@base64d'
jq: error (at <stdin>:1): string ("_-%3D") is not valid base64 data

Expected behavior

$ echo '_-8%3D' | jq -R -r '@base64d' | xxd -g1
00000000: ff ef

Environment (please complete the following information):

  • Ubuntu 20.04.
  • jq-1.6

Additional context
Having a way to output the raw binary would be helpful, too. The @base64d does not output the two bytes 0xFF 0xFE, but instead does a UTF-8 expansion.

@apstndb
Copy link

apstndb commented Jun 12, 2021

I define functions to work around.

def base64url: @base64 | gsub("\\+"; "-") | gsub("/"; "_") | gsub("="; "");
def base64urld: gsub("-"; "+") | gsub("_"; "/") | gsub("%3D"; "=") | @base64d;

Having a way to output the raw binary would be helpful, too. The @base64d does not output the two bytes 0xFF 0xFE, but instead does a UTF-8 expansion.

Binary supports seems working in this PR.
#2314

@jfromaniello
Copy link

@apstndb thanks, this helped me to create a jwt-decode function:

~ » cat ~/.jq
def base64url: @base64 | gsub("\\+"; "-") | gsub("/"; "_") | gsub("="; "");
def base64urld: gsub("-"; "+") | gsub("_"; "/") | gsub("%3D"; "=") | @base64d;

~ » function jwt-decode () {
  jq -R 'split(".") | .[1] | base64urld | fromjson'
}


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants