escape string for command line

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ksenia Marasanova

    #1

    escape string for command line

    Hi,

    I have a simple ecard creation script on a website, where user can add
    text to a graphic. I use ImageMagick for it:

    # template_file => path to image template file
    # new_file => path to generated file
    # text => user input
    command = '''convert %s -font OfficinaSanITC-BookOS -pointsize 12
    -fill "#8C2F48" -draw "gravity north text 0,26 '%s'" %s''' % (
    template_file, text, new_file)
    system(command)

    I was wondering, is there a general way to escape the string entered
    by the user, to prevent code injection into command line? Will it
    always be safe, even when binary data is submitted through POST?
    Or maybe some stable Python interface for ImageMagick that takes care of it :)

    Thanks in advance,
    --
    Ksenia
  • Marc 'BlackJack' Rintsch

    #2
    Re: escape string for command line

    In <mailman.321.11 05127978.22381. python-list@python.org >, Ksenia
    Marasanova wrote:
    [color=blue]
    > I have a simple ecard creation script on a website, where user can add
    > text to a graphic. I use ImageMagick for it:
    >
    > # template_file => path to image template file
    > # new_file => path to generated file
    > # text => user input
    > command = '''convert %s -font OfficinaSanITC-BookOS -pointsize 12
    > -fill "#8C2F48" -draw "gravity north text 0,26 '%s'" %s''' % (
    > template_file, text, new_file)
    > system(command)
    >
    > I was wondering, is there a general way to escape the string entered
    > by the user, to prevent code injection into command line?[/color]

    Take a look at the "string-escape" encoding:
    [color=blue][color=green][color=darkred]
    >>> evil = "'; rm -rf /;"
    >>> command = "echo '%s'"
    >>> print command % evil.encode('st ring-escape')[/color][/color][/color]
    echo '\'; rm -rf /;'
    [color=blue]
    > Will it
    > always be safe, even when binary data is submitted through POST?[/color]

    Don't know if it's always safe. Unprintable bytes like 0x00 will be
    escaped as '\x00'.

    Ciao,
    Marc 'BlackJack' Rintsch

    Comment

    • Ksenia Marasanova

      #3
      Re: escape string for command line

      > >[color=blue][color=green]
      > > I was wondering, is there a general way to escape the string entered
      > > by the user, to prevent code injection into command line?[/color]
      >
      > Take a look at the "string-escape" encoding:
      >[color=green][color=darkred]
      > >>> evil = "'; rm -rf /;"
      > >>> command = "echo '%s'"
      > >>> print command % evil.encode('st ring-escape')[/color][/color]
      > echo '\'; rm -rf /;'[/color]

      Cool, thanks! Next time I'll study stdlib better before asking the question :)

      --
      Ksenia

      Comment

      Working...