Skip to content

Work in FIPS enabled environments #345

Closed

Description

@frapell

On FIPS enabled environments, when trying to render a view it fails with

...
...                                                                       
  File "/opt/plone/buildout-cache/eggs/zope.pagetemplate-4.5.0-py3.6.egg/zope/pagetemplate/pagetemplate.py", line 129, in pt_render                                                                                          
    raise PTRuntimeError(str(self._v_errors))                                                                                                                                                                                
zope.pagetemplate.pagetemplate.PTRuntimeError: ['Compilation failed', 'builtins.ValueError: [digital envelope routines: EVP_DigestInit_ex] disabled for FIPS']  

The issue is in reality in the usage of the md5 module in https://github.com/malthe/chameleon/blob/master/src/chameleon/zpt/template.py

There is a flag that can be used when hasing with md5 in a non security related usage that is not available if FIPS is not enabled in the environment called usedforsecurity

I will submit a PR shortly with a proposed fix (The same that was done with zodb/relstorage#481)

Activity

  1. jamadden commented on Jul 21, 2021

    @jamadden
    Contributor

    The base template already uses hashlib.sha1 in digest(). Could that be used in the ZPTTemplate if it's FIPS compliant (I'm assuming it must be)? That seems simpler.

    The only consequence I can think of is that on-disk template caches would be invalidated, but that doesn't seem significant because it happens anyway when you change the installed package versions.

    This is really a question for @malthe

  2. frapell commented on Jul 21, 2021

    @frapell
    ContributorAuthor

    @jamadden Yeah, makes more sense, I just tried it and worked fine, so I have changed it

  3. malthe commented on Jul 21, 2021

    @malthe
    Owner

    I suppose that it makes sense to switch to SHA256 – perhaps with an option to provide a salt using an environment variable CHAMELEON_CACHE_SALT. That would provide some additional security in the case where we can have user-contributed templates.

  4. jamadden commented on Jul 21, 2021

    @jamadden
    Contributor

    I'm probably missing something, but it's not clear to me that being able to provide a salt in the environment adds much/any benefit. As I understand it, the main purpose of a salt is to keep two identical inputs from having identical hash outputs; for that to work, the salt has to change based on some function.

    Take password hashing; you might, for example, use the username as the salt. In that way, hash('password', salt='joe') is never the same as hash('password', salt='alice'), and no one that compromises Joe's password (or knows its hash) can see that Alice's hashed password is actually the same and transitively compromise her password.

    But if the salt is hardcoded, or a single environment variable set for the life of a process, that goes away: hash('password', salt=CHAMELEON_CACHE_SALT) == hash('password', salt=CHAMELEON_CACHE_SALT). The extra dimension of mixing in the username (context) is lost.

    It seems that we'd at least need a way to distinguish salts for "application provided" templates vs "user provided" templates, and I don't know how that could be generalized at this level.

    If you're worried about some sort of on-disk cache poisoning, wouldn't it be easier to just delete the cache? Or make different processes use different cache directories (already possible with environment variables)?

  5. jamadden commented on Jul 21, 2021

    @jamadden
    Contributor

    Besides the (probably negligible) speed impact, something to consider about using SHA256 vs SHA1 is that the former adds 24 characters to the hex form, which would result in longer filenames. (Going from md5 to sha1 already adds 8 characters.) In an old Chameleon disk cache I have laying around, the longest filename is 79 characters; adding 8 + 24 brings that to 111.

    I know that in the past, some operating systems had pretty tight limits on the length of path segments and the total path (cough Windows); I don't know if that's true anymore, but it's something to think about.

  6. malthe commented on Jul 21, 2021

    @malthe
    Owner

    @jamadden yes, you're right – adding salt doesn't make sense here.

    On the length of the hash, there is no requirement to use the full hash value. AFAIK SHA256 is often truncated to 128 bits without a significant loss in cryptographic properties.

  7. frapell commented on Jul 22, 2021

    @frapell
    ContributorAuthor

    @malthe @jamadden I have no preference on which algorithm to use, as far as I understand, this part of the code is used to generate the filename for cached templates, so has no security implications whatsoever, right?

    it might make sense though, to use SHA256 to avoid this same issue in the future...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions