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

twilio security not working on my .net core 3.0/3.1 application properly #509

Open
KamranShahid opened this issue Dec 10, 2019 · 19 comments
Labels
difficulty: unknown or n/a fix is unknown in difficulty status: help wanted requesting help from the community type: bug bug in the library

Comments

@KamranShahid
Copy link

KamranShahid commented Dec 10, 2019

I have implemented validation as per https://www.twilio.com/docs/usage/tutorials/how-to-secure-your-csharp-aspnet-core-app-by-validating-incoming-twilio-requests

I have my .net core 3.0 application where callback url is like https://mydomain.com:81/Twilio/TwilioAction

Request validator seems always rejecting the validation.

Same code with my other deployment where my address is like
https://myotherdomain.com/Twilio/TwilioAction is working fine.

It looks like due to following implementation in twilio RequestValidator

private string RemovePort(UriBuilder uri)
        {
            // UriBuilder.ToString() will not display the port 
            // if the Port property is set to -1
            uri.Port = -1;
            return uri.ToString();
        }

        private string AddPort(UriBuilder uri)
        {
            if (uri.Port != -1)
            {          
                return uri.ToString();
            }
            **uri.Port = uri.Scheme == "https" ? 443 : 80;**          
            return uri.ToString();
        }

I have even upgraded to release 5.37.1 but issue still exists

@thinkingserious
Copy link
Contributor

Hello @KamranShahid,

Thanks for bringing this to our attention!

This issue has been added to our internal backlog to be prioritized. Pull requests and +1s on the issue summary will help it move up the backlog.

With best regards,

Elmer

@thinkingserious thinkingserious added difficulty: unknown or n/a fix is unknown in difficulty status: help wanted requesting help from the community type: bug bug in the library labels Dec 11, 2019
@childish-sambino
Copy link
Contributor

What sort of callback is this not working for?

@childish-sambino childish-sambino added the status: waiting for feedback waiting for feedback from the submitter label Dec 11, 2019
@KamranShahid
Copy link
Author

this is request validation as per recommendation from twilio.

@childish-sambino
Copy link
Contributor

Yes, but which particular webhook is the request validation failing for? SMS, Voice, Conference, or something else?

@KamranShahid
Copy link
Author

SMS

@childish-sambino childish-sambino removed the status: waiting for feedback waiting for feedback from the submitter label Dec 11, 2019
@KamranShahid
Copy link
Author

If someone can hint what change i would do in https://github.com/twilio/twilio-csharp/blob/master/src/Twilio/Security/RequestValidator.cs then i will try out it locally in my project

@childish-sambino
Copy link
Contributor

I don't know yet what the change would be. The line you've highlighted about adding a default port only applies if the URL doesn't contain a port, which in your case it should.

You can try debugging by seeing if the URL being fed into the validator matches the URL exactly as you've configured it in your SMS webhook.

@KamranShahid
Copy link
Author

I don't know yet what the change would be. The line you've highlighted about adding a default port only applies if the URL doesn't contain a port, which in your case it should.

You can try debugging by seeing if the URL being fed into the validator matches the URL exactly as you've configured it in your SMS webhook.

then i might have to check what the value of my token with my url/form element compare to the one expected with X-Twilio-Signature and my token.
Done this thing quite a while back and looks like i needed to do it again :(

@childish-sambino
Copy link
Contributor

I would ignore the token for now and just focus on the first part: does the URL (protocol, host, port) being fed into the validator match what's configured in the SMS webhook?

@KamranShahid
Copy link
Author

my url in web hook is like
https://mysite.com:81/twilio/mytwilioaction
as per above
protocol https
host mysite.com
port 81

please try out this type of url

@childish-sambino
Copy link
Contributor

There are unit tests that verify such URLs. What I'm asking is what URL is actually being fed into the validator? From the validation example, the code looks like this:

private static string RequestRawUrl(HttpRequest request)
{
    return $"{request.Scheme}://{request.Host}{request.Path}{request.QueryString}";
}

Is this what your code is doing? If so, what's the result?

@KamranShahid
Copy link
Author

yes it is exactly like this and it is even working for my urls like
https://mysite.com/twilio/mytwilioaction
but not for
https://mysite.com:81/twilio/mytwilioaction

I know about this validation and even did it in one of my old asp.net WCF application.

@KamranShahid KamranShahid changed the title twilio security not working on my .net core 3.0 application properly twilio security not working on my .net core 3.0/3.1 application properly Dec 19, 2019
@KamranShahid
Copy link
Author

Any update on this issue?

@childish-sambino
Copy link
Contributor

I've not had a chance to come back to this yet to try to recreate what's going on here.

@KamranShahid
Copy link
Author

I've not had a chance to come back to this yet to try to recreate what's going on here.

Ok Sambino

@yroush
Copy link

yroush commented Dec 26, 2019

Im having the same issue with the code Twilio provides for .net core request validation.

It fails every time.

@KamranShahid
Copy link
Author

Im having the same issue with the code Twilio provides for .net core request validation.

It fails every time.

give some details. what's the callback url? is it https or http

@yroush
Copy link

yroush commented Dec 27, 2019

Im having the same issue with the code Twilio provides for .net core request validation.
It fails every time.

give some details. what's the callback url? is it https or http

http callback url.

I tried hosting that url on both port 80 and another port, but the request validator still does not work.

The RequestRawURL function works properly, so I at least know that it isn't part of the problem.

@winzig
Copy link

winzig commented Sep 25, 2020

Spent a few hours now trying to figure this out, and I also cannot get Twilio's signature validation library to work on .NET 3.x. Using Twilio 5.39.0 on .NET 3.x.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
difficulty: unknown or n/a fix is unknown in difficulty status: help wanted requesting help from the community type: bug bug in the library
Projects
None yet
Development

No branches or pull requests

5 participants