C#: How comprehensive is this malicious URL test?

How comprehensive is this malicious URL test?

        public static bool IsUrlDomainValid(this Uri uri, List<string> whitelist)
        {
            return whitelist.Any(w => uri.Host.EndsWith(w));
        }

Open in new window


I create a while list that contains various good domains:

www.abc.com
www.xyz.com
www.123.com

and want this function to fail if there is a single domain that is not whitelisted.

Is that was this code does? I get worried with the use of "endswith"

What if the last domain is a good one, but there is a bad one in the middle?

Thanks
newbiewebSr. Software EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nociSoftware EngineerCommented:
endwith means:

if www.abc.com is ok, then anything that ends with www.abc.com is ok:
    123.www.abc.com
    def.www.abc.com
    whatever.www.abc.com

But not:
     123.abc.com
     whatever.abc.com

while for most Domains *.abc.com  belongs to abc.com (exceptions are domain names from ISP's and some counties:
england has 2 level domain name system    .co.uk  etc.  isp often have vanity names: custermer.isp.net  or the like.
0
Shaun VermaakTechnical SpecialistCommented:
As per above, seems that function was created to be used with DOMAIN.TLD and explicitly trust all subdomains
0
ambienceCommented:
What if the last domain is a good one, but there is a bad one in the middle?

Its a whitelist, bad one in the middle cannot appear.

Domain names are case-insensitive there following is preferrable

public static bool IsUrlDomainValid(this Uri uri, List<string> whitelist)
 {
    return whitelist.Any(w => uri.Host.EndsWith(w, StringComparison.CurrentCultureIgnoreCase));
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
newbiewebSr. Software EngineerAuthor Commented:
thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Vulnerabilities

From novice to tech pro — start learning today.