Repository: mitmproxy/mitmproxy

Issue:
[Configuration file] keys are not used
##### Steps to reproduce the problem:

1. Create a configuration file at .mitmproxy/config.yaml
2. Set this configuration:
mode: "transparent"
showhost: true 
3. Start mitmproxy using this command : "mitmproxy --conf config.yaml" (I'm on the .mitmproxy folder of course)
The process is started but the traffic is not working
4. Start mitmproxy using this command : "mitmproxy -T --host"
The process is started and the traffic is working


##### Any other comments? What have you tried so far?

I tried to use the ":" and "=" as separator for the YAML file but only the ":" is recognized apparently


##### System information
I have the last release of mitmproxy (v2.0.2)

Well I've just notice that there is a v3 release, maybe this could help me ?
I prefer to create this ticket if someone else has the same issue :)


Discussion:
Thanks.
```
λ echo "showhost: true" > conf.yaml
λ mitmproxy --conf conf.yaml
```
work for me fine on master. Please check if 3.0 works for you.
Hello !
It works now using the last release.
However, it seems that I have another issue using the v3 release.
I'm using cuckoo with mitmproxy for analyzing malwares/urls.
I have a script in /opt/cuckoo/stuff/mitm.py that is used by the mitmdump command.

When I used the v2.0.2 release of mitmproxy with cuckoo 2.0.5, it works (well, it's not working with the config.yaml file but with the v3 as I said it's ok)
When I used the v3 release of mitmproxy with cuckoo 2.0.5 (same version), it doesn't work and I have an error in the mitm.err file (providing by cuckoo) saying that the script doesn't exist on the given path (while it's working using the v2 release...)
Did you have any idea about this ?

Cuckoo is using mitmproxy using this command :
https://github.com/cuckoosandbox/cuckoo/blob/6fb0b0fd17ebd0a08424166287aca88fee53c117/cuckoo/auxiliary/mitm.py (line 57).


Some screeshots :
Here is my script :

![capture du 2018-01-31 11-36-39](https://user-images.githubusercontent.com/10530375/35618556-a3adca64-067b-11e8-9db7-fa30fafe55a5.png)

Using v2 release :
![capture du 2018-01-31 11-36-45](https://user-images.githubusercontent.com/10530375/35618557-a3d3f5d6-067b-11e8-9713-3dfb6ad58aa9.png)

Using v3 release :
![capture du 2018-01-31 11-36-56](https://user-images.githubusercontent.com/10530375/35618558-a408a376-067b-11e8-95db-0ab9f8288cdd.png)

Hm, weird. It errors here: https://github.com/mitmproxy/mitmproxy/blob/aa8969b240310f265640c92e14a4b7d6363e464b/mitmproxy/addons/script.py#L52-L53. Is your `mitm.py` symlinked or is there something else that could make `isfile` fail?
Yes, I have a symlink ..., but it's the same for v2 and v3 release (I change the mitmdump program in /usr/bin/)
![capture du 2018-01-31 12-06-51](https://user-images.githubusercontent.com/10530375/35619815-7d4eb7f8-067f-11e8-9d56-67884cadb5f8.png)

Cuckoo is using /usr/local/bin/mitmdump, but I can try to change it

What's `os.path.isfile("/opt/cuckoo/stuff/mitm.py")` for you?
With this code :
![capture du 2018-01-31 13-10-09](https://user-images.githubusercontent.com/10530375/35622526-6b594ab4-0688-11e8-8659-80d596db6eba.png)

As espected :
![capture du 2018-01-31 13-10-12](https://user-images.githubusercontent.com/10530375/35622528-6b87abd4-0688-11e8-8655-5dcb1e06cd3d.png)

No idea why it's not working then, sorry. This is the only place where we raise this exception.
I will try to find the reason. As you said, it weird to have this issue because it's only happening when the file doesn't exist and the v2 is proving that the file exists.
I will keep you in touch
I’ll try something tomorrow but I would like to notice you on my idea.
As you can see on the code, the error is returning the path and not the fullpath variable .......
Maybe there is something wrong with the processing of path into the fullpath ? Because yes the path seems correct but what about the fullpath variable that is indeed tested ? I’ll keep you in touch 
I was thinking about that as well, but 
```python
os.path.expanduser("/opt/cuckoo/stuff/mitm.py") == "/opt/cuckoo/stuff/mitm.py"
```
should always hold true. Let me know if you find something!
I think I found out what's going on :

`2018-02-01 09:17:12,033 [cuckoo.auxiliary.mitm] INFO: ['/usr/local/bin/mitmdump', '-q', '-s', '"/opt/cuckoo/stuff/mitm.py" ', '-p', '8080', '-w', '/opt/cuckoo/storage/analyses/2/dump.mitm']`

Here is the parameters given to mitmproxy. You will see that the script parameter contains a space at the end of the string (maybe waiting for arguments depending on the script). I think that this is the bug here !

What I don't understand is why the same parameter passed in v2 and v3 releases of mitmproxy doesn't have the same behavior when it's seems to have no modification in the code for this part.


Resolving patch:
diff --git a/mitmproxy/addons/script.py b/mitmproxy/addons/script.py
--- a/mitmproxy/addons/script.py
+++ b/mitmproxy/addons/script.py
@@ -44,13 +44,15 @@ class Script:
     def __init__(self, path):
         self.name = "scriptmanager:" + path
         self.path = path
-        self.fullpath = os.path.expanduser(path)
+        self.fullpath = os.path.expanduser(
+            path.strip("'\" ")
+        )
         self.ns = None
 
         self.last_load = 0
         self.last_mtime = 0
         if not os.path.isfile(self.fullpath):
-            raise exceptions.OptionsError("No such script: %s" % path)
+            raise exceptions.OptionsError('No such script: "%s"' % self.fullpath)
 
     @property
     def addons(self):


Test patch:
diff --git a/test/mitmproxy/addons/test_script.py b/test/mitmproxy/addons/test_script.py
--- a/test/mitmproxy/addons/test_script.py
+++ b/test/mitmproxy/addons/test_script.py
@@ -68,6 +68,18 @@ def test_notfound(self):
             with pytest.raises(exceptions.OptionsError):
                 script.Script("nonexistent")
 
+    def test_quotes_around_filename(self):
+        """
+        Test that a script specified as '"foo.py"' works to support the calling convention of
+        mitmproxy 2.0, as e.g. used by Cuckoo Sandbox.
+        """
+        path = tutils.test_data.path("mitmproxy/data/addonscripts/recorder/recorder.py")
+
+        s = script.Script(
+            '"{}"'.format(path)
+        )
+        assert '"' not in s.fullpath
+
     def test_simple(self):
         with taddons.context() as tctx:
             sc = script.Script(