Code Monkey home page Code Monkey logo

Comments (6)

npmccallum avatar npmccallum commented on August 15, 2024

Is there a reason you have to pass the value directly to jose? Because you could trivially pre-process the invalid value to convert it (remove '=', replace two characters).

from jose.

npmccallum avatar npmccallum commented on August 15, 2024

I'm thinking something like this:

$ echo 'ix6oO88/Qid+iPbREThqOw==' | sed -e 's|\+|-|g' -e 's|/|_|g' -e 's|=||g'
ix6oO88_Qid-iPbREThqOw

from jose.

mtottenh avatar mtottenh commented on August 15, 2024

I'm using libjose but that is essentially what I'm doing right now, I just figured I'd ask whether it's worth supporting in jose's b64 encode/decode functionality even if it isn't strictly within the JWK standard.

from jose.

npmccallum avatar npmccallum commented on August 15, 2024

I won't say no, because I suspect there are other bad producers out there. Implementations of standards should generally be liberal in what they accept and strict in what they produce. How invasive is the patch?

from jose.

mtottenh avatar mtottenh commented on August 15, 2024

The diff I've been using is:

diff --git a/lib/b64.c b/lib/b64.c
index 6223089..70f6e3a 100644
--- a/lib/b64.c
+++ b/lib/b64.c
@@ -185,11 +185,15 @@ jose_b64_dec(const json_t *i, void *o, size_t ol)
 
     if (json_unpack((json_t *) i, "s%", &b64, &len) < 0)
         return SIZE_MAX;
-
-    if (!o)
-        return b64_dlen(len);
-
-    return jose_b64_dec_buf(b64, len, o, ol);
+    size_t padding = 0;
+    for (size_t idx = 0; idx < len; idx++) {
+        if (b64[idx] == '=')
+            padding++;
+    }
+    if (!o) {
+        return b64_dlen(len - padding);
+    }
+    return jose_b64_dec_buf(b64, len - padding, o, ol);
 }
 
 jose_io_t *
@@ -231,8 +235,24 @@ jose_b64_dec_buf(const void *i, size_t il, void *o, size_t ol)
 
     for (size_t io = 0; io < il; io++) {
         uint8_t v = 0;
-
-        for (const char c = e[io]; v < len && c != map[v]; v++)
+        bool skip = false;
+
+        for (const char c = e[io]; v < len && c != map[v]; v++) {
+            if (c == '+') {
+                v=62;
+                break;
+            }
+            if (c == '/') {
+                v=63;
+                break;
+            }
+            if (c == '=') {
+                skip = true;
+                break;
+            }
+            continue;
+        }
+        if (skip)
             continue;
 
         if (v >= len)
-- 

I imagine we'd probably want to massage this a bit ensure the input encoding doesn't contain a mix of both b64/b64 urlsafe as this patch would happily process a broken mixture.

from jose.

mtottenh avatar mtottenh commented on August 15, 2024

I opened up a PR, I figure we could continue discussion there.

Closing.

from jose.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.