[ 
https://issues.apache.org/jira/browse/CODEC-333?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18009940#comment-18009940
 ] 

Aleksandr Beliakov commented on CODEC-333:
------------------------------------------

Hi [~ggregory] ,

 

Sorry for the delay, I was busy these days.
{quote}You would like to configure decoding to throw an exception when working 
on data that doesn't match URL-safety.
{quote}
Well, actually I want:
 * _decodeBase64_ method to be able to decode only "standard" base64 
characters. The method shall fail when encountering any non "standard" base64 
characters, including special base64Url symbols (namely '-' and '_');
 * _decodeBase64Url_ method to decode only base64Url characters. It means 
failing for all other characters, including '+' and '/'.

Similar behavior shall be applied for methods _isBase64_ and {_}isBase64Url{_}.

This is required to ensure correct processing, when one or another encoding 
allowed by a used standard.

 

The current problem is that existing methods _decodeBase64_ and _isBase64_ do 
not differentiate between "standard" base64 and base64Url encodings. So maybe 
it would make sense two introduce the following new methods, clearly separating 
the behavior, without breaking the logic of the original  _decodeBase64_ and 
_isBase64_ methods:
 * _isStandardBase64_ - verifying the conformance of the characters in string 
to the "standard" base64 encoding;
 * _isBase64Url_ - verifying the conformance of the characters in string to the 
base64Url encoding;
 * _decodeStandardBase64_ - decoding a string conformant to the "standard" 
base64 encoding, throwing an exception for other characters;
 * _decodeBase64Url_ - decoding a string conformant to the base64Url encoding, 
throwing an exception for other characters.

Modifying the default behavior for existing _isBase64_ and _decodeBase64_ 
methods, will break the behavior for existing application, therefore I do not 
suggest it.

 
{quote}If that's the case, I think this could look like this:

Add Builder.setUrlSafeDecoding(boolean), where the default behavior matches the 
current behavior
For clarity, deprecate Builder.setUrlSafe() and add 
Builder.setUrlSafeEncoding(boolean) (effectively renaming the method)
{quote}
The problem with the Builder is that it takes only a boolean parameter, while 
in fact we need a tristate: "standard" base64 only, base64Url only and a 
current "merged" base64+base64Url situation. Therefore I do not see how we 
could safely re-use it.

 

Best regards,

Aleksandr

> Separate Base64 decoding to "real" base64 and base64Url
> -------------------------------------------------------
>
>                 Key: CODEC-333
>                 URL: https://issues.apache.org/jira/browse/CODEC-333
>             Project: Commons Codec
>          Issue Type: Improvement
>    Affects Versions: 1.18.0
>            Reporter: Aleksandr Beliakov
>            Priority: Minor
>
> The behavior applied in CODEC-75 does not distinguish between Base64 and 
> Base64Url implementations within 
> [isBase64|https://commons.apache.org/proper/commons-codec/apidocs/org/apache/commons/codec/binary/Base64.html#isBase64(java.lang.String)]
>  and 
> [decodeBase64|https://commons.apache.org/proper/commons-codec/apidocs/org/apache/commons/codec/binary/Base64.html#decodeBase64(java.lang.String)]
>  methods. Nor, it does change the behavior, even if instantiating a class 
> object using a Builder like below, explicitly stating that url-safe 
> processing should not be used:
> {code:java}
> Base64 base64 = Base64.builder().setUrlSafe(false).get(); {code}
> with the method 
> [isInAlphabet|https://commons.apache.org/proper/commons-codec/apidocs/org/apache/commons/codec/binary/Base64.html#isInAlphabet(byte)]
>  still using the base64Url-safe decoding table.
>  
> This causes some issues in the implementation, for instance inability to 
> distinguish between Base64 and PEM encoded certificates, with Apache Codec 
> accepting the latest as Base64. See a (chunked and dummy) example below (with 
> _Base64.isBase64()_ method returning true):
> {code:java}
> -----BEGIN CERTIFICATE-----
> AQIDBAU=
> -----END CERTIFICATE-----{code}
>  
> According to the [RFC 4648|https://datatracker.ietf.org/doc/html/rfc4648], 
> handling of Base64 and Base64Url together should not be used:
> {panel:title=5. Base 64 Encoding with URL and Filename Safe Alphabet}
> This encoding may be referred to as "base64url".  *This encoding should not 
> be regarded as the same as the "base64" encoding* and should not be referred 
> to as only "base64".  Unless clarified otherwise, "base64" refers to the base 
> 64 in the previous section.
> {panel}
> While same handling may be acceptable in some particular cases, we think a 
> generic library like Apache Commons Codes shall make a clear separation 
> between the two encodings and unless implementors explicitly state the 
> support of url-safe characters, it should not be applied by default.
>  
> This should also avoid potential errors within the implementations, when a 
> system shall treat base64 and base64url encodings as two separate ones (for 
> instance like in 
> [standards|https://www.etsi.org/deliver/etsi_ts/119100_119199/11918201/01.02.01_60/ts_11918201v010201p.pdf]
>  explicitly requiring one or another implementation, based on the usage).
>  
> _FYI, Google Guava have two separate implementations 
> [BaseEncoding.base64()|https://guava.dev/releases/17.0/api/docs/com/google/common/io/BaseEncoding.html#base64()]
>  and 
> [BaseEncoding.base64Url()|https://guava.dev/releases/17.0/api/docs/com/google/common/io/BaseEncoding.html#base64Url()]
>  allowing implementors to explicitly choose one according to the project 
> needs._



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to