Giter Site home page Giter Site logo

Canonicity error about rust-base64 HOT 5 CLOSED

marshallpierce avatar marshallpierce commented on May 30, 2024
Canonicity error

from rust-base64.

Comments (5)

marshallpierce avatar marshallpierce commented on May 30, 2024

Padding is optional and has no semantic meaning.

from rust-base64.

AljoschaMeyer avatar AljoschaMeyer commented on May 30, 2024

The rfc is pretty vague on this.

https://tools.ietf.org/html/rfc4648#section-3.2

Implementations MUST include appropriate pad characters at the end of encoded data unless the specification referring to this document explicitly states otherwise.

https://tools.ietf.org/html/rfc4648#page-6

Since all base 64 input is an integral number of octets, only the following cases can arise: [..]

https://tools.ietf.org/html/rfc4648#section-3.3

If more than the allowed number of pad characters is found at the end of the string (e.g., a base 64 string terminated with "==="), the excess pad characters MAY also be ignored.

In particular the section on canonicity fails to mention this issue at all, which is somewhat disappointing.

The crate parses b"AAAA" into [0, 0, 0], but rejects b"AAAA=" - so it appears to do some checks on padding =s - it just happens to accept AA, AA= and AA== (but not AA===). So what is the exact API of this crate regarding trailing padding? Does it accept any number of padding =s up to the maximum amount that could be produced by the regular encoding algorithm?

I guess a workaround would be pretty easy here (decode, check length of encoded data, check that the canonical amount of padding was available in the encoding), so probably no need to add an option for this in this crate.

from rust-base64.

marshallpierce avatar marshallpierce commented on May 30, 2024

Does it accept any number of padding =s up to the maximum amount that could be produced by the regular encoding algorithm?

Yep. It detects that AAAA= is an impossible length (can't have |input| % 4 == 1 or == 5). It has to pay attention to the length at least somewhat because it needs to detect how much of the input it could conceivably do with the fastest type of decoding. Having just 1 padding char when there should be 2 is a bit of an odd case -- it really doesn't do any harm (no padding is ever needed, after all -- IMO it was a mistake to include it in the spec), and it won't cause padding to spill into blocks where it shouldn't exist, so it won't trip anything up. In general I'm pretty vigorously pro-strictness when it comes to parsing, but given that padding is completely useless to begin with, I'm not sure I want to fight the battle of making sure everyone's base64 has correct padding 🤷

from rust-base64.

AljoschaMeyer avatar AljoschaMeyer commented on May 30, 2024

Yup, that sounds reasonable. I'm stuck with a legacy system that has to enforce canonicity, but let's hope not many other people will ever have to deal with this... Canonicity can be enforced by checking that |input| % 4 == 0 I think, so that's something that can be easily done outside this crate.

from rust-base64.

marshallpierce avatar marshallpierce commented on May 30, 2024

Yep, if you're requiring padding, then it should always be 0.

from rust-base64.

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.