Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add 'usedforsecurity=False' arg to hashlib.md5 usage #1190

Merged
merged 3 commits into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lark/lark.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from .exceptions import ConfigurationError, assert_config, UnexpectedInput
from .utils import Serialize, SerializeMemoizer, FS, isascii, logger
from .load_grammar import load_grammar, FromPackageLoader, Grammar, verify_used_files, PackageResource
from .load_grammar import load_grammar, FromPackageLoader, Grammar, verify_used_files, PackageResource, md5_digest
from .tree import Tree
from .common import LexerConf, ParserConf, _ParserArgType, _LexerArgType

Expand Down Expand Up @@ -301,7 +301,7 @@ def __init__(self, grammar: 'Union[Grammar, str, IO[str]]', **options) -> None:
options_str = ''.join(k+str(v) for k, v in options.items() if k not in unhashable)
from . import __version__
s = grammar + options_str + __version__ + str(sys.version_info[:2])
cache_md5 = hashlib.md5(s.encode('utf8')).hexdigest()
cache_md5 = md5_digest(s)

if isinstance(self.options.cache, str):
cache_fn = self.options.cache
Expand Down
16 changes: 14 additions & 2 deletions lark/load_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -1312,7 +1312,7 @@ def do_import(self, dotted_path: Tuple[str, ...], base_path: Optional[str], alia
except IOError:
continue
else:
h = hashlib.md5(text.encode('utf8')).hexdigest()
h = md5_digest(text)
if self.used_files.get(joined_path, h) != h:
raise RuntimeError("Grammar file was changed during importing")
self.used_files[joined_path] = h
Expand Down Expand Up @@ -1391,7 +1391,7 @@ def verify_used_files(file_hashes):
if text is None: # We don't know how to load the path. ignore it.
continue

current = hashlib.md5(text.encode()).hexdigest()
current = md5_digest(text)
if old != current:
logger.info("File %r changed, rebuilding Parser" % path)
return False
Expand All @@ -1407,3 +1407,15 @@ def load_grammar(grammar, source, import_paths, global_keep_all_tokens):
builder = GrammarBuilder(global_keep_all_tokens, import_paths)
builder.load_grammar(grammar, source)
return builder.build(), builder.used_files


def md5_digest(s: str) -> str:
"""Get the md5 digest of a string

Supports the `usedforsecurity` argument for Python 3.9+ to allow running on
a FIPS-enabled system.
"""
if sys.version_info >= (3, 9):
return hashlib.md5(s.encode('utf8'), usedforsecurity=False).hexdigest()
else:
return hashlib.md5(s.encode('utf8')).hexdigest()