Skip to content

single quotes not properly escaped #34

Open

Description

@sanderhahn

This line doesn't seem to properly escapes single quotes using bash as a shell: https://github.com/fabric/patchwork/blob/master/patchwork/files.py#L130, possibly because bash needs a $'...\'...' (https://wiki.bash-hackers.org/scripting/bashchanges#quoting_expansions_substitutions_and_related).

patchwork.files.append(c, filename="/etc/postgresql/10/main/postgresql.conf", text="listen_addresses = '*'", escape=True)
# line that is added:
# listen_addresses = \*\\

This script shows the problem that i am experiencing:

import subprocess

def run(script):
    process = subprocess.run(
        ["bash", "-s"], capture_output=True, input=script.encode("ascii"))
    stderr = process.stderr.decode("ascii")
    print(stderr, end="")
    stdout = process.stdout.decode("ascii")
    print(stdout, end="")

line = "listen_addresses = '*'"

escaped = line.replace("'", r"'\\''")
script = f"echo '{escaped}'"
run(script)
# listen_addresses = \*\\

line = "listen_addresses = '*'"
escaped = "$'" + line.replace("'", r"\'") + "'"
script = f"echo {escaped}"
run(script)
# listen_addresses = '*'

Thank you for Fabric! :)

Activity

  1. sanderhahn commented on Jul 18, 2019

    @sanderhahn
    Author

    shlex.quote can also quote shell arguments: https://docs.python.org/3/library/shlex.html?highlight=shlex#shlex.quote

    import shlex
    print(shlex.quote("listen_addresses = '*'"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions