Ensure we encode the config in manifest schema 2 via the canonical JSON format

This commit is contained in:
Joseph Schorr 2018-12-18 14:52:19 -05:00
parent 48e584905a
commit feee49be9e
4 changed files with 48 additions and 5 deletions

View file

@ -25,8 +25,28 @@ class ContentRetrieverForTesting(ContentRetriever):
def ensure_utf8(unicode_or_str):
""" Ensures the given string is utf-8 encoded and not unicode. """
""" Ensures the given string is a utf-8 encoded str and not a unicode type. """
if isinstance(unicode_or_str, unicode):
return unicode_or_str.encode('utf-8')
return unicode_or_str
class _CustomEncoder(json.JSONEncoder):
def encode(self, o):
encoded = super(_CustomEncoder, self).encode(o)
if isinstance(o, basestring):
encoded = encoded.replace('<', '\\u003c')
encoded = encoded.replace('>', '\\u003e')
encoded = encoded.replace('&', '\\u0026')
return encoded
def to_canonical_json(value, ensure_ascii=True, indent=None):
""" Returns the canonical JSON string form of the given value,
as per the guidelines in https://github.com/docker/distribution/blob/master/docs/spec/json.md.
`indent` is allowed only for the purposes of indenting for debugging.
"""
return json.dumps(value, ensure_ascii=ensure_ascii, sort_keys=True, separators=(',', ':'),
cls=_CustomEncoder, indent=indent)