Skip to content

Commit

Permalink
Fix document output file #1118 (#1119)
Browse files Browse the repository at this point in the history
Fixed validity of the document parameter.
  • Loading branch information
wimjongman committed Nov 28, 2022
1 parent ca94f15 commit 62d2ab9
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 10 deletions.
Expand Up @@ -12,18 +12,16 @@
************************************************************************************/
package org.eclipse.birt.report.context;

import static org.junit.Assert.fail;

import org.eclipse.birt.report.exception.ViewerException;
import org.eclipse.birt.report.viewer.util.BaseTestCase;
import org.junit.Test;

/**
*
* Test the VBA.
*
*/
public class ViewerAttributeBeanTest {

public class ViewerAttributeBeanTest extends BaseTestCase {

/**
* Extensions with invalid characters are not allowed.
Expand All @@ -32,16 +30,71 @@ public class ViewerAttributeBeanTest {
*/
@Test
public void testCheckExtensionAllowedForRPTDocument() throws ViewerException {

ViewerAttributeBean.checkExtensionAllowedForRPTDocument("report");
ViewerAttributeBean.checkExtensionAllowedForRPTDocument("report.pdf");
ViewerAttributeBean.checkExtensionAllowedForRPTDocument("report.");
}

/**
* Extensions with invalid characters are not allowed.
*
* @throws ViewerException
*/
@Test
public void testDisallowEmptyExtension() throws ViewerException {

try {
ViewerAttributeBean.checkExtensionAllowedForRPTDocument("report.");
} catch (Exception e) {
return;
}

fail("invalid extension accepted");
}

/**
* Extensions with invalid characters are not allowed.
*
* @throws ViewerException
*/
@Test
public void testCheckExtensionAllowedForRPTDocument3() throws ViewerException {

try {
ViewerAttributeBean.checkExtensionAllowedForRPTDocument("report.pdf/");
ViewerAttributeBean.checkExtensionAllowedForRPTDocument("./file.jsp/.");
} catch (Exception e) {
return;
}

fail("invalid extension accepted");
}

/**
* Extensions with invalid characters are not allowed.
*
* @throws ViewerException
*/
@Test
public void testDocumentIsDirectory() throws ViewerException {

try {
ViewerAttributeBean.checkExtensionAllowedForRPTDocument("./file.jsp/");
} catch (Exception e) {
return;
}
fail("invalid extension accepted");
}

/**
* Extensions with invalid characters are not allowed.
*
* @throws ViewerException
*/
@Test
public void testValidDirectoryAndFile() throws ViewerException {
try {
ViewerAttributeBean.checkExtensionAllowedForRPTDocument("./file/hello.jsp/.test/blok.pdf");
} catch (Exception e) {
fail("valid extension not accepted");
}
}
}
Expand Up @@ -1086,12 +1086,28 @@ public boolean isReportRtl() {
* @throws ViewerException
*/
protected static void checkExtensionAllowedForRPTDocument(String rptDocumentName) throws ViewerException {
int extIndex = rptDocumentName.lastIndexOf(".");

// Parse the filename
String report = rptDocumentName;
try {
report = new File(rptDocumentName).getName();
} catch (Exception e) {
throw new ViewerException(BirtResources.getMessage(ResourceConstants.GENERAL_EXCEPTION_DOCUMENT_FILE_ERROR,
new String[] { report }));
}

// Catch invalid document names
if (report == null || report.trim().isEmpty() || report.trim().endsWith(".")) {
throw new ViewerException(BirtResources.getMessage(ResourceConstants.GENERAL_EXCEPTION_DOCUMENT_FILE_ERROR,
new String[] { report }));
}

int extIndex = report.lastIndexOf(".");
String extension = null;
boolean validExtension = true;

if (extIndex > -1 && (extIndex + 1) < rptDocumentName.length()) {
extension = rptDocumentName.substring(extIndex + 1);
if (extIndex > -1 && (extIndex + 1) < report.length()) {
extension = report.substring(extIndex + 1);
if (!extension.matches("^[A-Za-z0-9]+$")) {
validExtension = false;
}
Expand Down

0 comments on commit 62d2ab9

Please sign in to comment.