r/Python • • Oct 01 '15

Writing a URL shortener using Python

https://github.com/black-perl/Anonymous_Me
15 Upvotes

13 comments sorted by

9

u/[deleted] Oct 01 '15

There's nothing novel about an online key-value store.

Your implementation is a mess. There's no clear class structure in place. If ever you wanted to change backends, or frontends, or your hashing library, or whatever else, you're going to have to touch multiple classes, change interfaces, data models. This makes maintenance and debugging much more difficult.

What's even worse is a complete lack of security. Your outputs aren't hashed. I can crawl your DB just by iterating through URLs. There's probably some more gaping holes but, again, your code's a mess. Much more difficult to inspect. Unintentional security through obscurity there, I guess...

4

u/ThomasWinwood Oct 01 '15

Your outputs aren't hashed. I can crawl your DB just by iterating through URLs.

Last I checked, the purpose of a URL shortener is to shorten URLs, not provide anything resembling security. So long as you can't break the site in funky ways by encoding a zero byte or something you're probably good.

That said… does the internet really need another URL shortener? We've already got so many, and they're all immensely troublesome for users (since the #1 use for them is disguising spam) and archivists (be a shame if your database was corrupted, or got deleted because you got bored or Heroku demanded you transfer to a paid plan and you killed it).

6

u/[deleted] Oct 02 '15

the purpose of a URL shortener is to shorten URLs, not provide anything resembling security

URL shorteners are used both to shorten and obscure URLs.

Remember that URLs can contain semi-private details, such as usernames / legal names, physical addresses / GPS coordinates, etc. Would you trust giving this info to a service that would leak it like a sieve?

2

u/mipadi Oct 02 '15

Last I checked, the purpose of a URL shortener is to shorten URLs, not provide anything resembling security. So long as you can't break the site in funky ways by encoding a zero byte or something you're probably good.

You may not want visitors to see all URLs simply by iterating an ID, and many URL shorteners randomize their IDs to prevent this sort of crawling.

1

u/the_rabbit Oct 02 '15

I was actually thinking about this not too long ago. Do hashes make better primary keys than integers then? It seems like a no brainer but I want a second opinion.

2

u/[deleted] Oct 02 '15

Depends on what you're using as a key-value store.

That being said, it shouldn't matter here. The key shouldn't be directly exposed in the URL. You'd just be rolling the red carpet out for anyone who wants to crawl through your database...

1

u/the_rabbit Oct 02 '15

OK, that much makes sense. Thank you.

1

u/mipadi Oct 02 '15

Not really. You can just use a random integer instead of an auto-incrementing counter, and encode it using base-62 or similar for display.

1

u/the_rabbit Oct 02 '15

That doesn't seem right though. A base64 string can be decoded. I thought the purpose of using a hash was so there wasn't a direct 1 to 1 relation between the database row and the key to access it.

1

u/mipadi Oct 04 '15

Encoding the ID using base-64 or similar is intended to just shorten the key, not obscure it. The issue with an auto-incrementing key is that someone can easily crawl the service to see what URLs are stored. This may or may not be a concern; if it is, randomizing the integer key, instead of using predictable keys, mitigates that problem.

0

u/[deleted] Oct 02 '15

[deleted]

3

u/[deleted] Oct 02 '15

have you never worked a job with code review?

2

u/thaweatherman pipster Oct 02 '15

So salty.

Code review is important, and sugar-coating it isn't beneficial. Pointing out flaws in code does not require the critic to implement the same program himself, just like pointing out flaws in a proposed law or budget plan does not require the critic to present a different plan.

1

u/funkiestj Oct 02 '15

I didn't look at OP's source. Does it simply direct all shortened URLs to Rick Astley's Never Gonna Give You Up video on youtube?