I have come up with the following method to remove sensitive information with the help of the garbage collector:
def wipe_sensitive_info(d, keys):
"""
In Python we are not able to overwrite memory areas. What we do is:
- remove references to the specified objects, in this case keys in a dictionary
- immediately call garbage collection
"""
# TODO: the following points are pending
# - this relies on the gc to remove the objects from memory (de-allocate memory).
# - the memory itself is not guaranteed to be overwritten.
# - this will only deallocate objects which are not being referred somewhere else (ref count 0 after del)
for key in keys:
del d[key]
logger.info('Garbage collected {} objects'.format(gc.collect()))
This would be called from a django app as follows:
wipe_sensitive_info(request.data, ['password'])
Any ideas or comments on how to improve this implementation?