Hacker News new | ask | show | jobs
by outotrai 6133 days ago
A thought on security - I think #5 should be changed from

    if socket.gethostname() == 'productionserver.com':
        DEBUG = False
    else:
        DEBUG = True
to

    if socket.gethostname() == 'developmentcomp':
        DEBUG = True
    else:
        DEBUG = False
It's better practice to whitelist one box than to enable DEBUG on all computers but the production server - what if you deploy to another machine without thinking?
3 comments

You're right. Another handy way to do this is to keep debug as False and use middleware to show the debug error if you're logged in as an admin or from an IP that matches in INTERNAL_IPS:

http://ericholscher.com/blog/2008/nov/15/debugging-django-pr...

Sort of the best of both worlds.

Please, for the love of conciseness, never assign a boolean based on an if statement.
What if the assignment to the boolean was only one of the things you wanted to execute based on the result of the if statement?
Even then, I'd rather see:

DEBUG = socket.gethostname() == 'developmentcomp'

if DEBUG then...

It's not
How would you rewrite it?

  DEBUG = socket.gethostname() == 'developmentcomp'
An odd thing thing about this piece of advice is that it contradicts a lot of the previous advice which is 'don't hardcode stuff' This pattern is questionable for another reason - basing the app's configuration on some other, largely unrelated configuration (in this case, that of the network) can lead to elusive and annoying bugs. Configuration problems are much easier to debug when an application is told what its configuration is rather than trying to magically divine it from its environment.