-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Properly erase password from memory (resolves #187)
- Loading branch information
Showing
4 changed files
with
39 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import ctypes | ||
import platform | ||
import sys | ||
|
||
def memset_zero(secret): | ||
if platform.python_implementation() == 'CPython': | ||
strlen = len(secret) | ||
offset = sys.getsizeof(secret) - strlen - 1 | ||
ctypes.memset(id(secret) + offset, 0, strlen) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import platform | ||
import pytest | ||
from aws_adfs import helpers | ||
|
||
|
||
class TestHelpersMemsetZero: | ||
def test_helpers_memset_zero(self): | ||
if platform.python_implementation() != "CPython": | ||
pytest.skip( | ||
"Skipping memset_zero test becase Python implementation is not CPython: {}".format( | ||
platform.python_implementation() | ||
) | ||
) | ||
|
||
secret = "verysecretstring" | ||
copy = secret | ||
|
||
assert copy is secret | ||
|
||
helpers.memset_zero(secret) | ||
|
||
assert secret == "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" | ||
assert copy == "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" | ||
assert copy is secret |