From b617f3ccec1bbe8e105671b7b4c7f80ec2dcd001 Mon Sep 17 00:00:00 2001 From: Javen O'Neal Date: Mon, 20 Jun 2016 10:27:52 +0000 Subject: [PATCH] bug 56781: disallow names with symbols, except underscore, period, and backslash git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1749293 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/hssf/usermodel/HSSFName.java | 13 ++++-- .../org/apache/poi/ss/usermodel/Name.java | 2 +- .../apache/poi/xssf/usermodel/XSSFName.java | 16 +++++-- .../poi/ss/usermodel/BaseTestNamedRange.java | 44 +++++++++++++++++++ 4 files changed, 66 insertions(+), 9 deletions(-) diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFName.java b/src/java/org/apache/poi/hssf/usermodel/HSSFName.java index 13818f5b2..f6135c951 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFName.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFName.java @@ -17,6 +17,8 @@ package org.apache.poi.hssf.usermodel; +import java.util.regex.Pattern; + import org.apache.poi.hssf.model.HSSFFormulaParser; import org.apache.poi.hssf.model.InternalWorkbook; import org.apache.poi.hssf.record.NameCommentRecord; @@ -30,6 +32,11 @@ import org.apache.poi.ss.usermodel.Name; * 'named range' or name of a user defined function. */ public final class HSSFName implements Name { + private static final Pattern isValidName = Pattern.compile( + "[\\p{IsAlphabetic}_\\\\]" + + "[\\p{IsAlphabetic}0-9_.\\\\]*", + Pattern.CASE_INSENSITIVE); + private HSSFWorkbook _book; private NameRecord _definedNameRec; private NameCommentRecord _commentRec; @@ -155,10 +162,8 @@ public final class HSSFName implements Name { private static void validateName(String name){ if(name.length() == 0) throw new IllegalArgumentException("Name cannot be blank"); - - char c = name.charAt(0); - if(!(c == '_' || Character.isLetter(c)) || name.indexOf(' ') != -1) { - throw new IllegalArgumentException("Invalid name: '"+name+"'; Names must begin with a letter or underscore and not contain spaces"); + if(!isValidName.matcher(name).matches()) { + throw new IllegalArgumentException("Invalid name: '"+name+"'"); } } diff --git a/src/java/org/apache/poi/ss/usermodel/Name.java b/src/java/org/apache/poi/ss/usermodel/Name.java index a07154e5f..983e438b5 100644 --- a/src/java/org/apache/poi/ss/usermodel/Name.java +++ b/src/java/org/apache/poi/ss/usermodel/Name.java @@ -55,7 +55,7 @@ public interface Name { /** * Get the sheets name which this named range is referenced to * - * @return sheet name, which this named range refered to + * @return sheet name, which this named range referred to */ String getSheetName(); diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java index 1e592514a..c9db0bfda 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java @@ -17,6 +17,10 @@ package org.apache.poi.xssf.usermodel; import org.apache.poi.ss.formula.ptg.Ptg; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + import org.apache.poi.ss.formula.FormulaParser; import org.apache.poi.ss.formula.FormulaType; import org.apache.poi.ss.usermodel.Name; @@ -52,6 +56,11 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDefinedName; * @author Yegor Kozlov */ public final class XSSFName implements Name { + + private static final Pattern isValidName = Pattern.compile( + "[\\p{IsAlphabetic}_\\\\]" + + "[\\p{IsAlphabetic}0-9_.\\\\]*", + Pattern.CASE_INSENSITIVE); /** * A built-in defined name that specifies the workbook's print area @@ -344,12 +353,11 @@ public final class XSSFName implements Name { XSSFName cf = (XSSFName) o; return _ctName.toString().equals(cf.getCTName().toString()); } - + private static void validateName(String name){ if(name.length() == 0) throw new IllegalArgumentException("Name cannot be blank"); - char c = name.charAt(0); - if(!(c == '_' || Character.isLetter(c)) || name.indexOf(' ') != -1) { - throw new IllegalArgumentException("Invalid name: '"+name+"'; Names must begin with a letter or underscore and not contain spaces"); + if (!isValidName.matcher(name).matches()) { + throw new IllegalArgumentException("Invalid name: '"+name+"'"); } } } diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestNamedRange.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestNamedRange.java index 18a0ef3e0..463314fc1 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestNamedRange.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestNamedRange.java @@ -25,6 +25,7 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.io.IOException; +import java.util.Arrays; import java.util.List; import org.apache.poi.ss.ITestDataProvider; @@ -677,4 +678,47 @@ public abstract class BaseTestNamedRange { wb.close(); } + + //@Ignore + @Test + public void test56781() throws IOException { + Workbook wb = _testDataProvider.createWorkbook(); + + Name name = wb.createName(); + for (String valid : Arrays.asList( + "Hello", + "number1", + "_underscore", + "p.e.r.o.i.d.s", + "\\Backslash", + "Backslash\\" + )) { + name.setNameName(valid); + } + + try { + name.setNameName(""); + fail("expected exception: (blank)"); + } catch (final IllegalArgumentException e) { + assertEquals("Name cannot be blank", e.getMessage()); + } + + for (String invalid : Arrays.asList( + "1number", + "Sheet1!A1", + "Exclamation!", + "Has Space", + "Colon:", + "A-Minus", + "A+Plus", + "Dollar$")) { + try { + name.setNameName(invalid); + fail("expected exception: " + invalid); + } catch (final IllegalArgumentException e) { + assertEquals("Invalid name: '" + invalid + "'", e.getMessage()); + } + } + + } }