Skip to content

Commit

Permalink
Refactor mcbe_backup regex
Browse files Browse the repository at this point in the history
Escape Docker container once
Don't escape ) right parenthesis for grep -E
Rename escape variables
  • Loading branch information
TapeWerm committed Aug 4, 2024
1 parent 262f464 commit 95e0ac3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
12 changes: 6 additions & 6 deletions src/mcbe_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,9 @@ def server_do(cmd: str) -> typing.Union[str, None, datetime.datetime]:
:return: systemd cursor or time for server_read
"""
if ARGS.docker:
# Escape r'][(){}:,!!\" ' for socat address specifications and command line
service_esc = re.sub(r'([\\" ])', r"\\\\\\\1", SERVICE)
service_esc = re.sub("([][(){}:,!])", r"\\\1", service_esc)
cmd_cursor = datetime.datetime.now().astimezone()
subprocess.run(
["socat", "-", f"EXEC:docker container attach -- {service_esc},pty"],
["socat", "-", f"EXEC:docker container attach -- {SERVICE_SOCAT},pty"],
check=True,
input=cmd + "\n",
stdout=subprocess.DEVNULL,
Expand Down Expand Up @@ -118,7 +115,7 @@ def server_read(cmd_cursor: typing.Union[str, None, datetime.datetime]) -> str:
sys.exit(
f"No world {WORLD} in {WORLDS_DIR}, check level-name in server.properties too"
)
WORLD_ESC = re.escape(WORLD)
WORLD_REGEX = re.escape(WORLD)
if ARGS.docker:
TEMP_DIR = pathlib.Path("/tmp/docker_mcbe_backup", SERVER_DIR.parent.name)
else:
Expand All @@ -143,6 +140,9 @@ def server_read(cmd_cursor: typing.Union[str, None, datetime.datetime]) -> str:
!= "running"
):
sys.exit(f"Container {SERVICE} not running")
# Escape r'][(){}:,!!\" ' for socat address specifications and command-line
SERVICE_SOCAT = re.sub(r'([\\" ])', r"\\\\\\\1", SERVICE)
SERVICE_SOCAT = re.sub("([][(){}:,!])", r"\\\1", SERVICE_SOCAT)
else:
# Trim off SERVICE after last .service
if SERVICE.endswith(".service"):
Expand Down Expand Up @@ -211,7 +211,7 @@ def server_read(cmd_cursor: typing.Union[str, None, datetime.datetime]) -> str:
# {WORLD}not :...:#...
# Minecraft Bedrock Edition says file:bytes, file:bytes, ...
# journald LineMax splits lines so delete newlines
files = re.findall(f"{WORLD_ESC}[^:]+:[0-9]+", QUERY.replace(os.linesep, ""))
files = re.findall(f"{WORLD_REGEX}[^:]+:[0-9]+", QUERY.replace(os.linesep, ""))

TEMP_DIR.mkdir(parents=True, exist_ok=True)
# zip restores path of directory given to it (WORLD), not just the directory itself
Expand Down
14 changes: 7 additions & 7 deletions src/mcbe_backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,8 @@ syntax='Usage: mcbe_backup.sh [OPTION]... SERVER_DIR SERVICE'
# echo "$*" to $service input
server_do() {
if [ "$docker" = true ]; then
# Escape '][(){}:,!!\" ' for socat address specifications and command line
local service_esc
service_esc=$(echo "$service" | sed -Ee 's/([\" ])/\\\\\\\1/g')
service_esc=$(echo "$service_esc" | sed -Ee 's/([][(){}:,!])/\\\1/g')
date --iso-8601=ns
echo "$*" | socat - EXEC:"docker container attach -- $service_esc",pty > /dev/null
echo "$*" | socat - EXEC:"docker container attach -- $service_socat",pty > /dev/null
else
{
journalctl "_SYSTEMD_UNIT=$service.service" --show-cursor -n 0 -o cat || true
Expand Down Expand Up @@ -100,7 +96,8 @@ if [ ! -d "$worlds_dir/$world" ]; then
>&2 echo "No world $world in $worlds_dir, check level-name in server.properties too"
exit 1
fi
world_esc=$(echo "$world" | sed -Ee 's/([.?*+{|()[\^$])/\\\1/g')
# Escape '.?*+{|([\^$' for grep -E
world_regex=$(echo "$world" | sed -Ee 's/([.?*+{|([\^$])/\\\1/g')
if [ "$docker" = true ]; then
temp_dir=/tmp/docker_mcbe_backup/$(basename "$(dirname "$server_dir")")
else
Expand All @@ -113,6 +110,9 @@ if [ "$docker" = true ]; then
>&2 echo "Container $service not running"
exit 1
fi
# Escape '][(){}:,!!\" ' for socat address specifications and command-line
service_socat=$(echo "$service" | sed -Ee 's/([\" ])/\\\\\\\1/g')
service_socat=$(echo "$service_socat" | sed -Ee 's/([][(){}:,!])/\\\1/g')
else
# Trim off $2 after last .service
service=${2%.service}
Expand Down Expand Up @@ -176,7 +176,7 @@ done
# Minecraft Bedrock Edition says $file:$bytes, $file:$bytes, ...
# journald LineMax splits lines so delete newlines
# shellcheck disable=SC1087
files=$(echo "$query" | tr -d '\n' | grep -Eo -- "$world_esc[^:]+:[0-9]+")
files=$(echo "$query" | tr -d '\n' | grep -Eo -- "$world_regex[^:]+:[0-9]+")

mkdir -p "$temp_dir"
# zip restores path of directory given to it ($world), not just the directory itself
Expand Down
2 changes: 1 addition & 1 deletion tests/test_mcbe.sh
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ echo 'Test mcbe-backup@testme level-name flag injection'
test_backup

systemctl stop "mcbe@$instance.socket"
sed -ie 's/^level-name=.*/level-name=.+{()[^$/' "$properties"
sed -ie 's/^level-name=.*/level-name=.+{([^$/' "$properties"
start_server

echo 'Test mcbe-backup@testme level-name regex injection'
Expand Down

0 comments on commit 95e0ac3

Please sign in to comment.