Skip to content

base64 validation too strict; padding is optional #1156

@fluxsauce

Description

@fluxsauce

Context

  • node version: 6.9.2
  • joi version: 10.4.1
  • environment node
  • used with standalone:
  • any other relevant information:

What are you trying to achieve or the steps to reproduce ?

Determine if a URL parameter contains valid base64 encoded content.

const schema = Joi.string().base64()

Example values:

  • MTYyMDcxMDozOjE

The problem is that the validation fails on a base64 string that is valid but does not have padding; the issue was dismissed in #1081

I don't agree with that interpretation; it's not invalid, it just doesn't have optional padding.

https://en.wikipedia.org/wiki/Base64#Output_Padding

In theory, the padding character is not needed for decoding, since the number of missing bytes can be calculated from the number of Base64 digits. In some implementations, the padding character is mandatory, while for others it is not used.

The input is not normalized and comes from a variety of sources that I do not have direct control over and I cannot force changes, nor can I just arbitrarily ignore unpadded content.

Attempting to pad the content prior to validation is hacky.

// Joi requires padding.
const paddingLength = working.length % 4;
const workingPadded = paddingLength ? working + '='.repeat(4 - paddingLength) : working;

A more graceful solution would be to make padding optional.

  • options: - an optional object with the following optional keys:
    • paddingRequired: if true, requires that the string include = padding. Defaults to true.

Which result you had ?

message: '"value" must be a valid base64 string',

What did you expect ?

No errors.

Metadata

Metadata

Assignees

Labels

featureNew functionality or improvement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions