Opened 2 years ago

Closed 9 months ago

#2784 closed bug (Fixed)

[Execute] Escape call parameters for Windows

Reported by: Cas Owned by:
Priority: minor Milestone: 1.3.14
Component: Plugin Version: 1.3.12
Keywords: Cc:

Description

From: http://forum.deluge-torrent.org/viewtopic.php?f=9&t=48203&p=221405

To expand on my prior post, I'm talking about escaping required by the Windows OS command line. Unless an ampersand is escaped, Windows interprets it as separating 2 commands, so it runs everything before the ampersand as one command and everything after it as another command (ref: http://www.robvanderwoude.com/condexec.php). So when the execute plugin tries to run a command and the filename has an ampersand in it, Windows tries to run it as 2 separate and partial commands.

I have tested my script by calling it manually, and if it is called with the ampersand properly escaped, my script works fine. So the problem isn't my script as you suggested. And it's not an encoding issue. It's a Windows-specific command line escaping issue with the plugin.

http://stackoverflow.com/questions/14020944/how-do-i-escape-ampersands-in-batch-file-function-parameter

Change History (4)

comment:1 Changed 19 months ago by Cas

So the answer here is to escape the ampersand with a tripe-caret e.g. ^^^& and then quote any echo output in the batch script. This requires more testing and a better understanding of how getProcessOutputAndValue works so leaving this one for now.

from twisted.internet.utils import getProcessOutputAndValue
from twisted.internet import defer
from twisted.internet import reactor
import os
import traceback

@defer.inlineCallbacks
def runProgram():
    torrent_id = "torrhash"
    torrent_name = "test&name".replace('&', '^^^&')
    download_location = "C:/dir"
    command = "C:/Documents and Settings/Administrator/Desktop/test.bat"
    args = (torrent_id, torrent_name, download_location)
    try:
        result = yield getProcessOutputAndValue(command, args, env=os.environ)
        print result
        reactor.stop()
    except Exception:
        traceback.print_exc()
        reactor.stop()

reactor.callLater(0, runProgram)
reactor.run()
import subprocess
torrent_id = 'torrhash'
torrent_name = 'test^^^&name'
download_location = 'C:/dir'
command = 'C:/Documents and Settings/Administrator/Desktop/test.bat'
result = subprocess.call([command, torrent_id, torrent_name, download_location])

test.bat

@echo off
set torrentid=%1
set torrentname=%2
set torrentpath=%3

@echo "Torrent Details:  %torrentname% %torrentpath% %torrentid%"  >> "%userprofile%\Desktop\execute_script.log"

comment:2 Changed 18 months ago by Cas

  • Resolution set to Fixed
  • Status changed from new to closed

Fixed the use-case for ampersand as other chars unlikely to be in torrent name (libtorrent fixes invalid chars on windows) or directories.

1.3-stable: [5f92810f761e7df7], develop: [abf90f1dd647a9b46]

Last edited 18 months ago by Cas (previous) (diff)

comment:3 Changed 9 months ago by dashbad

  • Resolution Fixed deleted
  • Status changed from closed to reopened

I have come across this bug because my execute script is failing on Linux, because Ampersands are being escaped to

^^^&

Steps to reproduce:

  1. Write bash script:
    #!/bin/bash
    exec 1> >(logger -s -t $(basename $0)) 2>&1
    export name="$1"
    echo $name
    
  2. Set script as execute script
  3. Trigger script for a torrent with an ampersand in the name

The syslog shows that the ampersand in the name has been converted to

^^^&

Can this change be rolled back/modified so it doesn't impact non-Windows users?

Last edited 9 months ago by dashbad (previous) (diff)

comment:4 Changed 9 months ago by Cas

  • Milestone changed from 1.3.13 to 1.3.14
  • Resolution set to Fixed
  • Status changed from reopened to closed

This was fixed in 1.3.14: [df88c82265f7b]

Note: See TracTickets for help on using tickets.