Git Product home page Git Product logo

protected's Introduction

Overview

Wouldn't it completely suck to accidentally leak sensitive configuration data like your AWS keys into the wild while running Django in DEBUG mode?

Yes, Django protects your configuration variables from disclosure if your variable name happens to match one of their "magic" pattersn like "API" or "SECRET". But good engineers don't depend on magic and that particular bit of magic does little to protect your secrets once they make it from your configuiration variables into the local varaibles in your code, which Django also happily shares in debug mode.

Well, our new protected module puts an end to that problem.

Getting started

To get started you need to install procted. It's been uploaded to Pypi to installation is a cinch:

easy_install protected

Now lets try it out. Normally when you create a seccret, its easily readable like this:

$ python
Python 2.6.5
Type "help", "copyright", "credits" or "license" for more information"
>>> secret = 'secret'
>>> secret
secret

That sort of sucks; ProtectedString tries to remedy this situation by providing an alternative __repr__ method for your secret so it isn't disclosed. Lets try it out

>>> from protected import ProtectedString
>>> secret = ProtectedString("uber secret")
>>> secret
>>> <Protected String #139757326589296 ***********>

Now you can get at the contents of this as a normal string if you really want like this:

>>> str(secret)
uber secret

So, what's happening? A ProtectedString object is simply a superclass of a string with __repr__ overridden to prevent its display. You can still convert it to a normal string to print its value by calling str() on it, which demotes it to a normal string.

But there's a little more happening under the hood than that.

>>> secret + "!"
<Protected String #139757326589392 ***********>
>>> str(secret + "!")
'uber secret!'

ProtectedString can survive basic operations against it. Even this works

>>> very = "Very %s!" % secret
>>> very
<Protected String #139757326548616 ***********>
>>> str(very)
'Very uber secret!'
>>> 

There are some limits to ProtectedString's ability to obfuscate itself. This operation does not protect the string:

>>> "%s" % (secret,)
uber secret

The difference likes in whom __rmod__ is called against.

"%s" % ProtectedString("secret") calls ProtectedString's __rmod__ method. "%s%" % (ProtectedString("secret"),) doesn't.

While imperfect, if we use this, it may decrease the surface area from which secrets might leak.

Anyhow, I encourage you to play with this, apply it to our config files and try to break it. Please try to break it. I look forward to your feedback - Adam DePrince (@adamdeprince)

protected's People

Contributors

karthikbgl avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

karthikbgl

protected's Issues

Unittest for replace doesn't confirm class

    ps_c = ProtectedString("abcdef")
    self.assertTrue(isinstance(ps_c, ProtectedString))
    ps_c = ps_c.replace('b', 'd')
    self.assertEqual(str(ps_c), 'adcdef')

self.assertTrue comes before the replace, so we don't actually know taht replace returns a ProtectedString

Refactor tests

A lot of the tests do something like this

protected_string = <some series of operations that might generate a protected string>
self.assertTrue(isinstance(protected_string, ProtectedString))
self.assertEqual(str(protected_string), 'adcdef')

Lets create a method like this:

def assertProtectedStringEquals(self, this, that):
    self.assertEquals(this.__class__, ProtectedString)
    self.assertEquals(this, that)

and then rewrite all of htese tests so they are one liners like this:

def test_replace(self):
   self.assertProtectedStringEquals(ProtectedString('abc').replace('a', 'X'), 'Xbc') 

What does ProtectedString(....) * X return?

In the test_multiplication test it isn't clear what type ProtectedString should be returning. Lets test to ensure that it returns a ProtectedString, and failing that, skip the test as is done in test_protected_string_replace

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.