Example usage for org.apache.poi.openxml4j.util ZipSecureFile setMaxEntrySize

List of usage examples for org.apache.poi.openxml4j.util ZipSecureFile setMaxEntrySize

Introduction

In this page you can find the example usage for org.apache.poi.openxml4j.util ZipSecureFile setMaxEntrySize.

Prototype

public static void setMaxEntrySize(long maxEntrySize) 

Source Link

Document

Sets the maximum file size of a single zip entry.

Usage

From source file:org.pentaho.di.trans.steps.excelinput.ExcelInput.java

License:Apache License

/**
 * This method is responsible for setting the configuration values that control how the ZipSecureFile class behaves
 * when trying to detect zipbombs (check PDI-17586 for more details).
 *///  ww w  .j  a  v a  2  s  .  c o m
protected void setZipBombConfiguration() {

    // The minimum allowed ratio between de- and inflated bytes to detect a zipbomb.
    String minInflateRatioVariable = EnvUtil.getSystemProperty(Const.KETTLE_ZIP_MIN_INFLATE_RATIO,
            Const.KETTLE_ZIP_MIN_INFLATE_RATIO_DEFAULT_STRING);
    double minInflateRatio;
    try {
        minInflateRatio = Double.parseDouble(minInflateRatioVariable);
    } catch (NullPointerException | NumberFormatException e) {
        minInflateRatio = Const.KETTLE_ZIP_MIN_INFLATE_RATIO_DEFAULT;
    }
    ZipSecureFile.setMinInflateRatio(minInflateRatio);

    // The maximum file size of a single zip entry.
    String maxEntrySizeVariable = EnvUtil.getSystemProperty(Const.KETTLE_ZIP_MAX_ENTRY_SIZE,
            Const.KETTLE_ZIP_MAX_ENTRY_SIZE_DEFAULT_STRING);
    long maxEntrySize;
    try {
        maxEntrySize = Long.parseLong(maxEntrySizeVariable);
    } catch (NullPointerException | NumberFormatException e) {
        maxEntrySize = Const.KETTLE_ZIP_MAX_ENTRY_SIZE_DEFAULT;
    }
    ZipSecureFile.setMaxEntrySize(maxEntrySize);

    // The maximum number of characters of text that are extracted before an exception is thrown during extracting
    // text from documents.
    String maxTextSizeVariable = EnvUtil.getSystemProperty(Const.KETTLE_ZIP_MAX_TEXT_SIZE,
            Const.KETTLE_ZIP_MAX_TEXT_SIZE_DEFAULT_STRING);
    long maxTextSize;
    try {
        maxTextSize = Long.parseLong(maxTextSizeVariable);
    } catch (NullPointerException | NumberFormatException e) {
        maxTextSize = Const.KETTLE_ZIP_MAX_TEXT_SIZE_DEFAULT;
    }
    ZipSecureFile.setMaxTextSize(maxTextSize);
}

From source file:org.pentaho.di.trans.steps.excelinput.ExcelInputContentParsingTest.java

License:Apache License

@Test
public void testZipBombConfiguration_Default() throws Exception {

    // First set some random values
    Long bogusMaxEntrySize = 1000L;
    ZipSecureFile.setMaxEntrySize(bogusMaxEntrySize);
    Long bogusMaxTextSize = 1000L;
    ZipSecureFile.setMaxTextSize(bogusMaxTextSize);
    Double bogusMinInflateRatio = 0.5d;
    ZipSecureFile.setMinInflateRatio(bogusMinInflateRatio);

    // Verify that the bogus values were set
    assertEquals(bogusMaxEntrySize, (Long) ZipSecureFile.getMaxEntrySize());
    assertEquals(bogusMaxTextSize, (Long) ZipSecureFile.getMaxTextSize());
    assertEquals(bogusMinInflateRatio, (Double) ZipSecureFile.getMinInflateRatio());

    // Initializing the ExcelInput step should make the new values to be set
    meta.setSpreadSheetType(SpreadSheetType.SAX_POI);
    init("Balance_Type_Codes.xlsx");

    // Verify that the default values were used
    assertEquals(Const.KETTLE_ZIP_MAX_ENTRY_SIZE_DEFAULT, (Long) ZipSecureFile.getMaxEntrySize());
    assertEquals(Const.KETTLE_ZIP_MAX_TEXT_SIZE_DEFAULT, (Long) ZipSecureFile.getMaxTextSize());
    assertEquals(Const.KETTLE_ZIP_MIN_INFLATE_RATIO_DEFAULT, (Double) ZipSecureFile.getMinInflateRatio());
}