sendgrid / sendgrid-ruby

The Official Twilio SendGrid Led, Community Driven Ruby API Library
https://sendgrid.com
MIT License
625 stars 322 forks source link

Validate SenderAuthentication domain is invalid JSON #403

Closed cseeman closed 4 years ago

cseeman commented 4 years ago

Issue Summary

I am adding some sender authentication calls into an application, and have run into an issue working with validating the sender authentication domain. The returned response body is not valid JSON data. I wouldn't even call it valid string data, which it might be according to the API reference, or it should be null. Should the returned validation reason data be parsable? image

Steps to Reproduce

  1. Call SendGrid V3 POST /whitelabel/domains/{domain_id} with a valid domain id (for my testing I am using 3186093)

  2. If the response contains validation issues that have reasons, the text in each reason is broken up so it cannot be parsed. See below response body, on the dkim1 and dkim2 validation results:

    {
    "id": 3186093,
    "valid": false,
    "validation_results": {
        "mail_cname": {
            "valid": true,
            "reason": null
        },
        "dkim1": {
            "valid": false,
            "reason": "Expected CNAME for "
            s1._domainkey.example.co.nz
            " to match "
            s1.domainkey.u11308629.wl130.sendgrid.net
            "."
        },
        "dkim2": {
            "valid": false,
            "reason": "Expected CNAME for "
            s2._domainkey.example.co.nz
            " to match "
            s2.domainkey.u11308629.wl130.sendgrid.net
            "."
        }
    }
    }

Raw response from SendGrid via Ruby library:

#<SendGrid::Response:0x00007f8ddfee7720 @status_code="200", @body="{\"id\":3186093,\"valid\":false,\"validation_results\":{\"mail_cname\":{\"valid\":true,\"reason\":null},\"dkim1\":{\"valid\":false,\"reason\":\"Expected CNAME for \\\"s1._domainkey.example.co.nz\\\" to match \\\"s1.domainkey.u11308629.wl130.sendgrid.net\\\".\"},\"dkim2\":{\"valid\":false,\"reason\":\"Expected CNAME for \\\"s2._domainkey.example.co.nz\\\" to match \\\"s2.domainkey.u11308629.wl130.sendgrid.net\\\".\"}}}\n", @headers={"server"=>["nginx"], "date"=>["Fri, 17 Jan 2020 16:09:42 GMT"], "content-type"=>["application/json"], "content-length"=>["380"], "connection"=>["close"], "access-control-allow-methods"=>["HEAD, GET, PATCH, PUT, POST, OPTIONS, DELETE"], "access-control-max-age"=>["21600"], "access-control-expose-headers"=>["Link, Location"], "access-control-allow-origin"=>["*"], "access-control-allow-headers"=>["AUTHORIZATION, Content-Type, On-behalf-of, x-sg-elas-acl, X-Recaptcha, X-Request-Source"], "content-security-policy"=>["default-src https://api.sendgrid.com; frame-src 'none'; object-src 'none'"], "x-content-type-options"=>["nosniff"], "strict-transport-security"=>["max-age=31536000"], "x-ratelimit-remaining"=>["59"], "x-ratelimit-limit"=>["60"], "x-ratelimit-reset"=>["1579277400"], "powered-by"=>["Mako"]}>

Code Snippet

api = SendGrid::API.new(api_key: ENV['API_KEY'])
response = api.client.whitelabel.domains._(id).validate.post
json_response = JSON.parse(response.body)

Exception/Log

     JSON::ParserError:
       784: unexpected token at '{
        "id": 3186093,
        "valid": false,
        "validation_results": {
            "mail_cname": {
                "valid": true,
                "reason": null
            },
            "dkim1": {
                "valid": false,
                "reason": "Expected CNAME for "
                s1._domainkey.example.co.nz
                " to match "
                s1.domainkey.u11308629.wl130.sendgrid.net
                "."
            },
            "dkim2": {
                "valid": false,
                "reason": "Expected CNAME for "
                s2._domainkey.example.co.nz
                " to match "
                s2.domainkey.u11308629.wl130.sendgrid.net
                "."
            }
        }
       }
       '

Technical details:

cseeman commented 4 years ago

For now I am going to go forward with checking the raw response body for \\\" which seems like the escape character for the quote is itself being escaped which kind of invalidates it and just replace that response body with \"

childish-sambino commented 4 years ago

I've not been able to recreate this issue locally. The JSON parsing works fine even with the double-escaped quotes (which makes it proper JSON). Are you still seeing this issue? And could you post a dump of the installed gems?