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

base64: atob returns incorrect response for binary strings. #4

Open
sharmaashish13 opened this issue Jan 5, 2022 · 0 comments
Open

Comments

@sharmaashish13
Copy link

Issue:

While decoding a binary string, for example signatures in JWS access tokens for various identity providers, there are few issues:

  1. StdEncoding is unable to handle unpadded base64 strings.

  2. Fixing above issue using RawStdEncoding leads to other issues:

    • bytes returned to javascript world is not same as what was decoded in Go.
    • if a string has null character, output gets terminated at that string.

Sample Code:

package main

import (
	stdBase64 "encoding/base64"
	"fmt"
	"os"
	"go.kuoruan.net/v8go-polyfills/base64"
	"go.kuoruan.net/v8go-polyfills/console"
	"rogchap.com/v8go"
)

const script = `
let result = atob('CN7ySxWjjWNpbYPB1n/TBR8avujZS2cHdXRR5ZM7Fi6QBjlVzBqPu0CI9pQXcW9fvbMXdqU+57XY/QdGozJT19+gbQDM0ZQVzjwqtpyLorcPHjMqum+7dHD6XF5cXo3NZlKGsxcnvSVyClBDU5M1dUCe8bB9yV0wVM6ge+0WAmTX2GYbncilTjDw0bSJI1Z+71NT8UQCfmimKhVxJiKrnkaTrTw2Ma/1I2w4Dny3cRlFtCtob9cvNOeeIm8HtQoi/7HXoE0uFr1C39OL2hCC1TJsxX94djtNFqd9aUOPYrwT+zErSokSvbNYS5WpEjEpRJze9+TCV9NLmqCnARK4Bw'); 
let e = result.split("").map((c) => c.charCodeAt(0));
console.log("console value: ", e)
`

func main() {
	iso := v8go.NewIsolate()
	global := v8go.NewObjectTemplate(iso)

	if err := base64.InjectTo(iso, global); err != nil {
		panic(err)
	}

	ctx := v8go.NewContext(iso, global)
	if err := console.InjectTo(ctx, console.WithOutput(os.Stdout)); err != nil {
		panic(err)
	}

	_, err := ctx.RunScript(script, "fetch.js")
	if err != nil {
		panic(err)
	}
	//GO bytes
	byts, err := stdBase64.RawStdEncoding.DecodeString("CN7ySxWjjWNpbYPB1n/TBR8avujZS2cHdXRR5ZM7Fi6QBjlVzBqPu0CI9pQXcW9fvbMXdqU+57XY/QdGozJT19+gbQDM0ZQVzjwqtpyLorcPHjMqum+7dHD6XF5cXo3NZlKGsxcnvSVyClBDU5M1dUCe8bB9yV0wVM6ge+0WAmTX2GYbncilTjDw0bSJI1Z+71NT8UQCfmimKhVxJiKrnkaTrTw2Ma/1I2w4Dny3cRlFtCtob9cvNOeeIm8HtQoi/7HXoE0uFr1C39OL2hCC1TJsxX94djtNFqd9aUOPYrwT+zErSokSvbNYS5WpEjEpRJze9+TCV9NLmqCnARK4Bw")
	fmt.Println("go value: : ", byts)
}

Expected:

Both javascript and golang byte arrays should be same.

Actual:

console val:  8,65533,65533,75,21,65533,65533,99,105,109,65533,65533,65533,127,65533,5,31,26,65533,65533,65533,75,103,7,117,116,81,65533,59,22,46,65533,6,57,85,65533,26,65533,65533,64,65533,65533,65533,23,113,111,95,65533,65533,23,118,65533,62,65533,65533,65533,7,70,65533,50,83,65533,2016,109

go val:  [8 222 242 75 21 163 141 99 105 109 131 193 214 127 211 5 31 26 190 232 217 75 103 7 117 116 81 229 147 59 22 46 144 6 57 85 204 26 143 187 64 136 246 148 23 113 111 95 189 179 23 118 165 62 231 181 216 253 7 70 163 50 83 215 223 160 109 0 204 209 148 21 206 60 42 182 156 139 162 183 15 30 51 42 186 111 187 116 112 250 92 94 92 94 141 205 102 82 134 179 23 39 189 37 114 10 80 67 83 147 53 117 64 158 241 176 125 201 93 48 84 206 160 123 237 22 2 100 215 216 102 27 157 200 165 78 48 240 209 180 137 35 86 126 239 83 83 241 68 2 126 104 166 42 21 113 38 34 171 158 70 147 173 60 54 49 175 245 35 108 56 14 124 183 113 25 69 180 43 104 111 215 47 52 231 158 34 111 7 181 10 34 255 177 215 160 77 46 22 189 66 223 211 139 218 16 130 213 50 108 197 127 120 118 59 77 22 167 125 105 67 143 98 188 19 251 49 43 74 137 18 189 179 88 75 149 169 18 49 41 68 156 222 247 228 194 87 211 75 154 160 167 1 18 184 7]

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

No branches or pull requests

1 participant