From e430df0a31b0da6f6d9b210bd494227a59e80de2 Mon Sep 17 00:00:00 2001 From: Gustavo Trott Date: Mon, 19 Apr 2021 13:36:03 -0300 Subject: [PATCH] Apply security enhancements for libreoffice conversion scripts --- bbb-libreoffice/assets/convert-local.sh | 33 +++++++++++++------ bbb-libreoffice/assets/convert-remote.sh | 20 +++++++++-- .../assets/zzz-bbb-docker-libreoffice | 8 ++--- 3 files changed, 45 insertions(+), 16 deletions(-) diff --git a/bbb-libreoffice/assets/convert-local.sh b/bbb-libreoffice/assets/convert-local.sh index 89501ee83b..8e9f0a00a2 100644 --- a/bbb-libreoffice/assets/convert-local.sh +++ b/bbb-libreoffice/assets/convert-local.sh @@ -1,4 +1,8 @@ #!/bin/bash +set -e +set -u +PATH="/bin/:/usr/bin/" + # Conversion of office files to Pdf using local docker bbb-soffice # This script receives three params @@ -6,9 +10,21 @@ # Param 2: Output pdf file path (e.g. "/tmp/test.pdf") # Param 3: Output format (pdf default) -while [ -z "$randomDirectoryName" -o -d "/tmp/bbb-libreoffice-conversion/$randomDirectoryName" ]; do - randomDirectoryName=$(shuf -i 100000000-999999999 -n 1) -done +if (( $# == 0 )); then + echo "Missing parameter 1 (Input office file path)"; + exit 1 +elif (( $# == 1 )); then + echo "Missing parameter 2 (Output pdf file path)"; + exit 1 +fi; + + +#Create tmp dir for conversion +mkdir -p "/tmp/bbb-soffice-$(whoami)/" +tempDir="$(mktemp -d -p /tmp/bbb-soffice-$(whoami)/)" + +source=${1} +dest=${2} #If output format is missing, define PDF convertTo="${3:-pdf}" @@ -20,12 +36,9 @@ then convertToParam="$convertToParam --writer" fi -mkdir -p "/tmp/bbb-libreoffice-conversion/" -chmod 777 "/tmp/bbb-libreoffice-conversion/" -mkdir "/tmp/bbb-libreoffice-conversion/$randomDirectoryName/" -cp "$1" "/tmp/bbb-libreoffice-conversion/$randomDirectoryName/file" -sudo /usr/bin/docker run --rm --network none --env="HOME=/tmp/" -w /tmp/ --user=$(printf %05d `id -u`) -v "/tmp/bbb-libreoffice-conversion/$randomDirectoryName/":/data/ --rm bbb-soffice sh -c "/usr/bin/soffice -env:UserInstallation=file:///tmp/ $convertToParam --outdir /data /data/file" -cp "/tmp/bbb-libreoffice-conversion/$randomDirectoryName/file.$convertTo" "$2" -rm -r "/tmp/bbb-libreoffice-conversion/$randomDirectoryName/" +cp "${source}" "$tempDir/file" +sudo /usr/bin/docker run --rm --network none --env="HOME=/tmp/" -w /tmp/ --user=$(printf %05d `id -u`) -v "$tempDir/":/data/ --rm bbb-soffice sh -c "/usr/bin/soffice -env:UserInstallation=file:///tmp/ $convertToParam --outdir /data /data/file" +cp "$tempDir/file.$convertTo" "${dest}" +rm -r "$tempDir/" exit 0 diff --git a/bbb-libreoffice/assets/convert-remote.sh b/bbb-libreoffice/assets/convert-remote.sh index e0df27cab5..74c672146c 100644 --- a/bbb-libreoffice/assets/convert-remote.sh +++ b/bbb-libreoffice/assets/convert-remote.sh @@ -1,4 +1,8 @@ -#/bin/bash +#!/bin/bash +set -e +set -u +PATH="/bin/:/usr/bin/" + # This is a sample script - adjust it per your need # 1 - setup a server with JOD-CONVERTER-REST ( docker run --memory 512m --rm -p 8080:8080 eugenmayer/jodconverter:rest ) # 2 - replace the HOST information in below command with your server host @@ -8,9 +12,21 @@ # Param 2: Output pdf file path (e.g. "/tmp/test.pdf") # Param 3: Destination Format (pdf default) +if (( $# == 0 )); then + echo "Missing parameter 1 (Input office file path)"; + exit 1 +elif (( $# == 1 )); then + echo "Missing parameter 2 (Output pdf file path)"; + exit 1 +fi; + + +source=${1} +dest=${2} + #If output format is missing, define PDF convertTo="${3:-pdf}" -curl -X POST "http://127.0.0.1:8080/lool/convert-to/$convertTo" -H "accept: application/octet-stream" -H "Content-Type: multipart/form-data" -F "data=@$1" > $2 +curl -X POST "http://127.0.0.1:8080/lool/convert-to/$convertTo" -H "accept: application/octet-stream" -H "Content-Type: multipart/form-data" -F "data=@${source}" > ${dest} exit 0 diff --git a/bbb-libreoffice/assets/zzz-bbb-docker-libreoffice b/bbb-libreoffice/assets/zzz-bbb-docker-libreoffice index c64b9384b4..0a31109c3d 100644 --- a/bbb-libreoffice/assets/zzz-bbb-docker-libreoffice +++ b/bbb-libreoffice/assets/zzz-bbb-docker-libreoffice @@ -1,4 +1,4 @@ -bigbluebutton ALL=(ALL) NOPASSWD: /usr/bin/docker run --rm --network none --env=HOME=/tmp/ -w /tmp/ --user=[0-9][0-9][0-9][0-9][0-9] -v /tmp/bbb-libreoffice-conversion/[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]/\:/data/ --rm bbb-soffice sh -c /usr/bin/soffice -env\:UserInstallation=file\:///tmp/ --convert-to pdf --outdir /data /data/file -etherpad ALL=(ALL) NOPASSWD: /usr/bin/docker run --rm --network none --env=HOME=/tmp/ -w /tmp/ --user=[0-9][0-9][0-9][0-9][0-9] -v /tmp/bbb-libreoffice-conversion/[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]/\:/data/ --rm bbb-soffice sh -c /usr/bin/soffice -env\:UserInstallation=file\:///tmp/ --convert-to pdf --writer --outdir /data /data/file -etherpad ALL=(ALL) NOPASSWD: /usr/bin/docker run --rm --network none --env=HOME=/tmp/ -w /tmp/ --user=[0-9][0-9][0-9][0-9][0-9] -v /tmp/bbb-libreoffice-conversion/[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]/\:/data/ --rm bbb-soffice sh -c /usr/bin/soffice -env\:UserInstallation=file\:///tmp/ --convert-to odt --writer --outdir /data /data/file -etherpad ALL=(ALL) NOPASSWD: /usr/bin/docker run --rm --network none --env=HOME=/tmp/ -w /tmp/ --user=[0-9][0-9][0-9][0-9][0-9] -v /tmp/bbb-libreoffice-conversion/[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]/\:/data/ --rm bbb-soffice sh -c /usr/bin/soffice -env\:UserInstallation=file\:///tmp/ --convert-to doc --outdir /data /data/file +bigbluebutton ALL=(ALL) NOPASSWD: /usr/bin/docker run --rm --network none --env=HOME=/tmp/ -w /tmp/ --user=[0-9][0-9][0-9][0-9][0-9] -v /tmp/bbb-soffice-bigbluebutton/tmp.[0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z]/\:/data/ --rm bbb-soffice sh -c /usr/bin/soffice -env\:UserInstallation=file\:///tmp/ --convert-to pdf --outdir /data /data/file +etherpad ALL=(ALL) NOPASSWD: /usr/bin/docker run --rm --network none --env=HOME=/tmp/ -w /tmp/ --user=[0-9][0-9][0-9][0-9][0-9] -v /tmp/bbb-soffice-etherpad/tmp.[0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z]/\:/data/ --rm bbb-soffice sh -c /usr/bin/soffice -env\:UserInstallation=file\:///tmp/ --convert-to pdf --writer --outdir /data /data/file +etherpad ALL=(ALL) NOPASSWD: /usr/bin/docker run --rm --network none --env=HOME=/tmp/ -w /tmp/ --user=[0-9][0-9][0-9][0-9][0-9] -v /tmp/bbb-soffice-etherpad/tmp.[0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z]/\:/data/ --rm bbb-soffice sh -c /usr/bin/soffice -env\:UserInstallation=file\:///tmp/ --convert-to odt --writer --outdir /data /data/file +etherpad ALL=(ALL) NOPASSWD: /usr/bin/docker run --rm --network none --env=HOME=/tmp/ -w /tmp/ --user=[0-9][0-9][0-9][0-9][0-9] -v /tmp/bbb-soffice-etherpad/tmp.[0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z]/\:/data/ --rm bbb-soffice sh -c /usr/bin/soffice -env\:UserInstallation=file\:///tmp/ --convert-to doc --outdir /data /data/file