troposphere icon indicating copy to clipboard operation
troposphere copied to clipboard

MaintenanceWindowLambdaParameters Payload incorrect validation.

Open blade2005 opened this issue 3 years ago • 3 comments

Per the documents here https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ssm-maintenancewindowtask-maintenancewindowlambdaparameters.html#cfn-ssm-maintenancewindowtask-maintenancewindowlambdaparameters-payload

"Although Type is listed as "String" for this property, the payload content must be formatted as a Base64-encoded binary data object."

https://github.com/cloudtools/troposphere/blob/main/troposphere/ssm.py#L194 validates that it's proper JSON but the expected value should be base64 encoded JSON string.

blade2005 avatar Jan 20 '22 21:01 blade2005

Interesting. Thanks for highlighting this issue. One solution would be using Fn::Base64 although that would likely break for encoded strings larger than 4096. Perhaps a validator like this? (untested)

def validate_json_base64(payload):
    """
    Property: MaintenanceWindowLambdaParameters.Payload
    """
    import base64
    import json
    from .. import AWSHelperFn

    if isinstance(payload, AWSHelperFn):
        return payload
    elif isinstance(payload, str):
        # Verify it is a valid json string
        payload = json.loads(payload)
    elif isinstance(payload, dict):
        # Convert the dict to a basestring
        payload = json.dumps(payload)
    else:
        raise TypeError("json object must be a str or dict")

    # base64 encode and check length
    payload = base64.b64encode(bytes(payload, 'utf-8')).decode()
    if len(payload) > 4096:
        raise ValueError("payload is greater than 4096 (base64 encoded)")

    return payload

markpeek avatar Jan 20 '22 22:01 markpeek

@blade2005 have you had a chance to review or try the above?

markpeek avatar Jan 24 '22 17:01 markpeek

@markpeek somehow I missed this notification. The code looks like it would work. I've not tried it. I'm no longer working on that project anymore.

I'm not sure if the 4096 constraint is on base64 encoded data or the decoded format.

blade2005 avatar Mar 28 '22 15:03 blade2005