Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to configure Apache POI #232

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export(
aref,
aref2idx,
col2idx,
configurePOI,
cref2idx,
extractSheetName,
idx2aref,
Expand Down
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
XLConnect News
--------------

1.x.x 2025-??-??
* Add possibility to configure Apache POI through configurePOI

1.2.0 2025-02-17
* Fix #225 (loading simple logging context factory)
* Upgrade POI to 5.4.0
Expand Down
3 changes: 3 additions & 0 deletions R/XLConnectSettings.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ XLConnectSettings <- function(pdesc) {
options(XLConnect.RownameCol = ".rownames")

options(XLConnect.setCustomAttributes = FALSE)

# Apply default POI configuration
configurePOI()

invisible()
}
47 changes: 47 additions & 0 deletions R/configurePOI.R
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Looking at the POI reference you linked in the doc, should we also have a setting for setThresholdBytesForTempFiles ? Even if we would leave the default of not using temp files (in case we run on a readonly system), it seems like this may sometimes be needed when using large files.
  • I understand it would be wasteful to have a happy-path test that uses a large file, but we could have negative tests which check that an exception is produced after setting some config value to an arbitrarily low level. This could alert us to some potential future changes in behavior of these config options

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#############################################################################
#
# XLConnect
# Copyright (C) 2010-2024 Mirai Solutions GmbH
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
#############################################################################

#############################################################################
#
# Configures Apache POI and related components.
# See https://poi.apache.org/components/configuration.html
#
# Author: Martin Studer, Mirai Solutions GmbH
#
#############################################################################

configurePOI <- function(
zip_max_files = 1000L,
zip_min_inflate_ratio = 0.001,
zip_max_entry_size = 0xFFFFFFFF,
zip_max_text_size = 10*1024*1024,
max_size_byte_array = -1L
) {
ioutils <- J("org.apache.poi.util.IOUtils")
ioutils$setByteArrayMaxOverride(as.integer(max_size_byte_array))

zip <- J("org.apache.poi.openxml4j.util.ZipSecureFile")
zip$setMaxFileCount(rJava::.jlong(zip_max_files))
zip$setMinInflateRatio(zip_min_inflate_ratio)
zip$setMaxEntrySize(rJava::.jlong(zip_max_entry_size))
zip$setMaxTextSize(rJava::.jlong(zip_max_text_size))

invisible()
}
Binary file not shown.
51 changes: 51 additions & 0 deletions man/configurePOI.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
\name{configurePOI}
\alias{configurePOI}
\title{
Configuring Apache POI
}
\description{
Configures Apache POI and related components.
}
\usage{
configurePOI(zip_max_files = 1000L, zip_min_inflate_ratio = 0.001,
zip_max_entry_size = 0xFFFFFFFF, zip_max_text_size = 10*1024*1024,
max_size_byte_array = -1L)
}
\arguments{
\item{zip_max_files}{Integer scalar specifying the maximum number of files
allowed inside an *.xlsx file. Defaults to \code{1000}.}
\item{zip_min_inflate_ratio}{Numeric scalar specifying the ratio between
de- and inflated bytes to detect zip-bombs. If the compression ratio is
better than the specified number an error will be thrown. Defaults to
\code{0.001}.}
\item{zip_max_entry_size}{Integer scalar specifying the maximum file size
of a single zip entry in an *.xlsx file. Defaults to 4'294'967'295 bytes,
which is 4GB.}
\item{zip_max_text_size}{Integer scalar specifying the maximum number of
characters of text that are extracted before an error is thrown. Defaults
to 10'485'760.}
\item{max_size_byte_array}{Integer scalar specifying the maximum number of
bytes that should be possible to be allocated in a single step. Increasing
this limit can help if you are dealing with large Excel files, but note that
this may demand a larger heap space (see option \code{java.parameters}; e.g.
\code{options(java.parameters = "-Xmx8192m")}. Defaults to -1, which means
that record-specific limits apply.)}
}
\details{
Many of the settings exposed here exist for security reasons to prevent excessive
memory consumption and protect against security vulnerabilities when processing
documents provided by untrusted sources.
}
\references{
Apache POI configuration: \url{https://poi.apache.org/components/configuration.html}
}
\author{
Martin Studer\cr
Mirai Solutions GmbH \url{https://mirai-solutions.ch}
}
\examples{
\dontrun{
configurePOI(zip_max_files = 5000L, max_size_byte_array = 250000000L)
}
}
\keyword{IO}
Loading