You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by fa...@apache.org on 2026/10/04 15:58:17 UTC

(poi-xmlbeans) branch trunk updated: reject non-alphabet characters in base64Binary lex (#123)

This is an automated email from the ASF dual-hosted git repository.

pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi-xmlbeans.git


The following commit(s) were added to refs/heads/trunk by this push:
     new c0c570a4 reject non-alphabet characters in base64Binary lex (#123)
c0c570a4 is described below

commit c0c570a43fc8812815b14f056e87e2526b429fab
Author: aizu-m <ai...@gmail.com>
AuthorDate: Sun Oct 4 21:28:12 2026 +0530

    reject non-alphabet characters in base64Binary lex (#123)
---
 .../xmlbeans/impl/values/JavaBase64Holder.java     | 17 +++++++
 .../misc/checkin/Base64BinaryValidateTest.java     | 58 ++++++++++++++++++++++
 2 files changed, 75 insertions(+)

diff --git a/src/main/java/org/apache/xmlbeans/impl/values/JavaBase64Holder.java b/src/main/java/org/apache/xmlbeans/impl/values/JavaBase64Holder.java
index 9858bc70..d6d3ca35 100644
--- a/src/main/java/org/apache/xmlbeans/impl/values/JavaBase64Holder.java
+++ b/src/main/java/org/apache/xmlbeans/impl/values/JavaBase64Holder.java
@@ -21,6 +21,7 @@ import org.apache.xmlbeans.XmlErrorCodes;
 import org.apache.xmlbeans.XmlObject;
 import org.apache.xmlbeans.impl.common.QNameHelper;
 import org.apache.xmlbeans.impl.common.ValidationContext;
+import org.apache.xmlbeans.impl.common.XMLChar;
 import org.apache.xmlbeans.impl.schema.BuiltinSchemaTypeSystem;
 
 import java.util.Arrays;
@@ -53,6 +54,17 @@ public abstract class JavaBase64Holder extends JavaDigestableHolder {
     }
 
     public static byte[] lex(String v, ValidationContext c) {
+        // The MIME decoder silently discards any character outside the base64
+        // alphabet, so a value carrying stray characters decodes to a truncated
+        // result instead of being rejected. The base64Binary lexical space only
+        // permits the alphabet and XML whitespace, so reject anything else here.
+        for (int i = 0, len = v.length(); i < len; i++) {
+            char ch = v.charAt(i);
+            if (!isBase64Char(ch) && !XMLChar.isSpace(ch)) {
+                c.invalid(XmlErrorCodes.BASE64BINARY, new Object[]{"not encoded properly"});
+                return null;
+            }
+        }
         try {
             return Base64.getMimeDecoder().decode(v);
         } catch (IllegalArgumentException e) {
@@ -62,6 +74,11 @@ public abstract class JavaBase64Holder extends JavaDigestableHolder {
         }
     }
 
+    private static boolean isBase64Char(char ch) {
+        return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') ||
+               (ch >= '0' && ch <= '9') || ch == '+' || ch == '/' || ch == '=';
+    }
+
     public static byte[] validateLexical(String v, SchemaType sType, ValidationContext context) {
         final byte[] bytes = lex(v, context);
         if (bytes == null) {
diff --git a/src/test/java/misc/checkin/Base64BinaryValidateTest.java b/src/test/java/misc/checkin/Base64BinaryValidateTest.java
new file mode 100644
index 00000000..1873e815
--- /dev/null
+++ b/src/test/java/misc/checkin/Base64BinaryValidateTest.java
@@ -0,0 +1,58 @@
+/*   Licensed to the Apache Software Foundation (ASF) under one or more
+ *   contributor license agreements.  See the NOTICE file distributed with
+ *   this work for additional information regarding copyright ownership.
+ *   The ASF licenses this file to You under the Apache License, Version 2.0
+ *   (the "License"); you may not use this file except in compliance with
+ *   the License.  You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ */
+
+package misc.checkin;
+
+import org.apache.xmlbeans.SchemaTypeLoader;
+import org.apache.xmlbeans.XmlBeans;
+import org.apache.xmlbeans.XmlObject;
+import org.apache.xmlbeans.impl.xb.xsdschema.SchemaDocument;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class Base64BinaryValidateTest {
+
+    private static boolean validates(String value) throws Exception {
+        String xsd =
+            "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:t='urn:t' " +
+            "targetNamespace='urn:t' elementFormDefault='qualified'>" +
+            "  <xs:element name='root' type='xs:base64Binary'/>" +
+            "</xs:schema>";
+        SchemaTypeLoader loader = XmlBeans.loadXsd(new XmlObject[]{SchemaDocument.Factory.parse(xsd)});
+        XmlObject doc = loader.parse("<t:root xmlns:t='urn:t'>" + value + "</t:root>", null, null);
+        return doc.validate();
+    }
+
+    @Test
+    void charsOutsideAlphabetAreReported() throws Exception {
+        // the MIME decoder silently drops non-alphabet characters, so these used
+        // to validate clean (decoding to "Hello" or an empty array)
+        assertFalse(validates("SGVsbG8=!!!!"));
+        assertFalse(validates("SGV!!!sbG8="));
+        assertFalse(validates("!!!!"));
+    }
+
+    @Test
+    void validValuesStillValidate() throws Exception {
+        assertTrue(validates("SGVsbG8="));
+        // whitespace and line wrapping are part of the lexical space
+        assertTrue(validates("SGVs bG8="));
+        assertTrue(validates("SGVs\n  bG8="));
+        assertTrue(validates(""));
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org