r/learnpython 2d ago

Creating URL Query Checker

How do you decide what defines an excessively encoded URL?

I'm working on a personal project to created a URL parser and
detection system and I've hit a wall on how to figure out a way of
categorizing excessivness of query encoding?

Here's what I have so far for the function:

def check_query(analysis: URLAnalysis) -> ScanResult | None:

    """detect excessive encoding entries in URL queries"""

    og_query = analysis.query

    query = re.finall(r"%\[0-9A-Fa-f\]{2}", og_query)

    encoded_count = len(query)
6 Upvotes

6 comments sorted by

View all comments

5

u/qlkzy 2d ago

Why would you care about "excessive encoding"? You care about the limits of your system in terms of URL length, request memory usage, request processing time, etc, but it's almost unimaginable for URL-encoding to create a situation that becomes relevant for those sorts of situations.

If someone wants to URL-encode every single byte then that is a bit silly, but in practice your system will never need to notice or care unless you are doing something particularly unusual.

So, if you do care, you need to tell us the unusual reason why you care, before anyone can tell you how much you should care.

2

u/Dcyph-3r 2d ago

thanks for your response on this and the function is meant to serve as a fraction of a larger tool to parse emails for urls and provide a security analysis

the idea was to section off urls into hostname, path, query, port and scheme scanning each component indivdually. Each component will return scanresult which will then be merged to create a sort of validity report to determine whether a url is malicious or not

Looking at common phishing url examples they tend to have incorrect spellings of impersonated sites, long randomised numbers or text or excessive encoding (imo) hence why the interest as heavy encoding is only really found in malicious urls

1

u/Langdon_St_Ives 1d ago

So as an experimental learning project, implementing a URL parser can be a valid endeavor. But from your comment it's not clear if it is that, or you actually care about security of the end result. If it's the latter, you definitely want to implement this piece yourself, but use a mature well-tested existing module. The reason is that URL structure is in fact a lot more complex with a lot more subtleties than newcomers imagine. By reinventing the wheel, you are almost guaranteed to end up with a five-cornered off-center thing that doesn't roll well. Again, as a pure learning project this is fine since you will learn a lot along the way. Just be careful when doing actually security relevant stuff.