Showing posts with label Web. Show all posts
Showing posts with label Web. Show all posts

Saturday, February 1, 2014

Flask, Facebook Canvas App, localhost and SSL


If you're developing a facebook canvas app with Flask, then you're asked to provide URLs for your app. Initially I used myapp.herokuapp.com address for http and https. But I realized that for testing local changes, I'd like to use localhost.

When I give localhost, it says it's not SSL supported. Then I had to create SSL certificates and install pyopenssl to use them. See: http://kracekumar.com/post/54437887454/ssl-for-flask-local-development

But what I saw was that connection was untrusted and I could not Add an Exception. I learned that browsers do not trust localhost for SSL. So I had to create an alias for my herokuapp. Added the following line to /etc/hosts

127.0.0.1 myapp.herokuapp.com

And set the canvas url as myapp.herokuapp.com for http and https.

Now when the app starts, tries to load https://myapp.herokuapp.com. It loads https://127.0.0.1:443 which is listened by the Flask app:

if __name__ == "__main__":
    port = int(os.environ.get("PORT", 443))
    app.run('0.0.0.0', debug=True, port=port, ssl_context=('/home/user/projects/myapp/server.crt', '/home/user/projects/myapp/server.key'))

This way I could overcome the SSL localhost problem for facebook canvas apps.

UPDATE: If you don't want to run the app as root,  just forward port 443 to 3000 and listen 3000 port with the following command as root:

iptables -t nat -A OUTPUT -p tcp --dport 443 -j REDIRECT --to-port 3000

and to cancel it:

iptables -t nat -D OUTPUT -p tcp --dport 443 -j REDIRECT --to-port 3000

Note that forwarding 443, you won't be able to connect SSL web sites throughout your pc.

Maybe using vagrant could be more elegant.

Friday, May 24, 2013

Fix for SuspiciousOperation: Invalid HTTP_HOST header

With Django 1.5, HTTP_HOST header filter is applied to the requests.  If the HTTP_HOST header is not among the ALLOWED_HOSTS list in the settings.py, an error is raised, saying this is a suspicious operation.

Let me give an example. Someone (who is not Google), is trying to reach my IP address with the HTTP_HOST www.google.com as if I'm hosting the google.com homepage.:

SuspiciousOperation: Invalid HTTP_HOST header (you may need to set ALLOWED_HOSTS): www.google.com


This happens frequently, leading to emails sent to the admins which is annoying. To overcome this, you can add the IP Address of the requester to /etc/hosts.deny file.

Indeed I thought I could put a hostname filter in nginx configuration, especially in the listen part but my configuration did not have an effect.

The attacker tries to exploit a vulnerability and performs a scan over the web. They seem to be from Vietnam.