Skip to content

Commit

Permalink
Allow blacklisting of long URLs by regex
Browse files Browse the repository at this point in the history
Signed-off-by: Ricky Elrod <ricky@elrod.me>
  • Loading branch information
relrod committed Dec 22, 2014
1 parent 91435e5 commit 8132b53
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/applications/shorten/shorten.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ private function isFreeShortURL() {
return !(bool)$count;
}

private function blacklisted($url) {
$blacklist_list = DaGdConfig::get('shorten.longurl_blacklist');
foreach ($blacklist_list as $regex) {
if (preg_match('#'.$regex.'#i', $url)) {
return true;
}
}
return false;
}

public function getLongURL($shorturl) {
$query = $this->db_connection->prepare(
'SELECT id,longurl FROM shorturls WHERE shorturl=? AND enabled=1');
Expand Down Expand Up @@ -171,13 +181,14 @@ public function set_longurl_or_400() {

if ($long_url = request_or_default('url')) {
// Something has at least been submitted. Is it valid?
if (preg_match('@^https?://@', $long_url)) {
if (preg_match('@^https?://@', $long_url) &&
!$this->blacklisted($long_url)) {
// Good enough for now...probably needs some better checks.
$this->long_url = $long_url;
return true;
} else {
error400(
'Malformed original URL. Try again (http or https '.
'Malformed or blacklisted original URL. Try again (http or https '.
'protocols only, please.).');
return false;
}
Expand Down
3 changes: 3 additions & 0 deletions src/config.dev.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ class DaGdConfig {
'gd',
),

// Regexes we blacklist on
'shorten.longurl_blacklist' => array(),

// The default transient whois server.
'whois.transient_server' => array(
'server' => 'whois.arin.net',
Expand Down

0 comments on commit 8132b53

Please sign in to comment.