Revert "Fix Invalid URL format error"

This commit is contained in:
Renan Altendorf 2026-03-05 16:52:26 -03:00 • committed by GitHub
parent a8df04e4c2
commit cee85c3ef8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 133 additions and 73 deletions

View file

@ -327,7 +327,7 @@ main {
input {
background-color: var(--input-bg);
padding: 16px 70px 16px 44px;
padding: 16px 0 16px 44px;
border: 0;
border-radius: 8px;
width: 100%;

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -1,2 +1,52 @@
User-agent: *
Disallow: /
Allow: /
User-agent: *
Disallow: /p/
Disallow: /api/
User-agent: AI2Bot
User-agent: Ai2Bot-Dolma
User-agent: Amazonbot
User-agent: anthropic-ai
User-agent: Applebot
User-agent: Applebot-Extended
User-agent: Bytespider
User-agent: CCBot
User-agent: ChatGPT-User
User-agent: Claude-Web
User-agent: ClaudeBot
User-agent: cohere-ai
User-agent: cohere-training-data-crawler
User-agent: Crawlspace
User-agent: Diffbot
User-agent: DuckAssistBot
User-agent: FacebookBot
User-agent: FriendlyCrawler
User-agent: Google-Extended
User-agent: GoogleOther
User-agent: GoogleOther-Image
User-agent: GoogleOther-Video
User-agent: GPTBot
User-agent: iaskspider/2.0
User-agent: ICC-Crawler
User-agent: ImagesiftBot
User-agent: img2dataset
User-agent: ISSCyberRiskCrawler
User-agent: Kangaroo Bot
User-agent: Meta-ExternalAgent
User-agent: Meta-ExternalFetcher
User-agent: OAI-SearchBot
User-agent: omgili
User-agent: omgilibot
User-agent: PanguBot
User-agent: PerplexityBot
User-agent: PetalBot
User-agent: Scrapy
User-agent: SemrushBot
User-agent: Sidetrade indexer bot
User-agent: Timpibot
User-agent: VelenPublicWebCrawler
User-agent: Webzio-Extended
User-agent: YouBot
Disallow: /

View file

@ -17,6 +17,7 @@ self.addEventListener('share_target', (event) => {
event.respondWith((async () => {
const formData = await event.request.formData();
const url = formData.get('url') || '';
return Response.redirect('/?url=' + encodeURIComponent(url), 303);
const redirectUrl = `/p/${encodeURIComponent(url)}`;
return Response.redirect(redirectUrl, 303);
})());
});

View file

@ -24,29 +24,17 @@ class Router
{
$this->dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r) {
// Main route - home page
$r->addRoute(['GET'], '/', function() {
$r->addRoute(['GET','POST'], '/', function() {
require_once __DIR__ . '/../config.php';
require_once __DIR__ . '/../inc/Cache.php';
require_once __DIR__ . '/../inc/Language.php';
\Inc\Language::init(LANGUAGE);
// Check GET param
$url = isset($_GET['url']) ? trim($_GET['url']) : '';
if (!empty($url)) {
$sanitizedUrl = $this->processUrl($url);
if (empty($sanitizedUrl)) {
header('Location: /?message=INVALID_URL');
exit;
}
header('Location: ' . SITE_URL . '/p/' . urlencode($sanitizedUrl));
exit;
}
$message = '';
$message_type = '';
$url = '';
// Sanitize and process query string messages
if (isset($_GET['message'])) {
$message_key = htmlspecialchars(trim($_GET['message']), ENT_QUOTES | ENT_HTML5, 'UTF-8');
@ -54,26 +42,45 @@ class Router
$message = htmlspecialchars($messageData['message'], ENT_QUOTES | ENT_HTML5, 'UTF-8');
$message_type = htmlspecialchars($messageData['type'], ENT_QUOTES | ENT_HTML5, 'UTF-8');
}
// Process form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['url'])) {
$originalUrl = trim($_POST['url']);
if (filter_var($originalUrl, FILTER_VALIDATE_URL)) {
$sanitizedUrl = $this->sanitizeUrl($originalUrl);
if (!empty($sanitizedUrl)) {
header('Location: ' . SITE_URL . '/p/' . $sanitizedUrl);
exit;
}
}
$messageData = \Inc\Language::getMessage('INVALID_URL');
$message = $messageData['message'];
$message_type = $messageData['type'];
}
// Initialize cache for counting
$cache = new \Inc\Cache();
$cache_folder = $cache->getCacheFileCount();
require __DIR__ . '/views/home.php';
});
// API route - uses URLProcessor in API mode
$r->addRoute('GET', '/api/{url:.+}', function($vars) {
$url = urldecode($vars['url']);
$sanitizedUrl = $this->processUrl($url);
if (empty($sanitizedUrl)) {
if (!preg_match('#^https?://#', $url)) {
$url = 'https://' . $url;
}
if (filter_var($url, FILTER_VALIDATE_URL)) {
$sanitizedUrl = $this->sanitizeUrl($url);
$processor = new URLProcessor($sanitizedUrl, true);
$processor->process();
} else {
header('Location: /?message=INVALID_URL');
exit;
}
$processor = new URLProcessor($sanitizedUrl, true);
$processor->process();
});
// API route without parameters - redirects to root
@ -85,41 +92,58 @@ class Router
// Processing route - uses URLProcessor in web mode
$r->addRoute('GET', '/p/{url:.+}', function($vars) {
require_once __DIR__ . '/../config.php';
$url = urldecode($vars['url']);
$sanitizedUrl = $this->processUrl($url);
if (empty($sanitizedUrl)) {
$originalUrl = $vars['url'];
$needsRedirect = false;
if ($originalUrl !== $url || preg_match('#^https?://#', $url)) {
$needsRedirect = true;
}
if (!preg_match('#^https?://#', $url)) {
$url = 'https://' . $url;
}
if (filter_var($url, FILTER_VALIDATE_URL)) {
$sanitizedUrl = $this->sanitizeUrl($url);
if ($needsRedirect && !empty($sanitizedUrl)) {
header('Location: ' . SITE_URL . '/p/' . $sanitizedUrl);
exit;
}
$processor = new URLProcessor($sanitizedUrl, false);
$processor->process();
} else {
header('Location: /?message=INVALID_URL');
exit;
}
$processor = new URLProcessor($sanitizedUrl, false);
$processor->process();
});
// Processing route with query parameter or without parameters
$r->addRoute('GET', '/p[/]', function() {
if (isset($_GET['url']) || isset($_GET['text'])) {
$originalUrl = isset($_GET['url']) ? trim($_GET['url']) : '';
$originalText = isset($_GET['text']) ? trim($_GET['text']) : '';
if (!empty($originalUrl)) {
$sanitizedUrl = $this->processUrl($originalUrl);
if (!empty($originalUrl) && filter_var($originalUrl, FILTER_VALIDATE_URL)) {
$sanitizedUrl = $this->sanitizeUrl($originalUrl);
if (!empty($sanitizedUrl)) {
header('Location: /p/' . urlencode($sanitizedUrl));
header('Location: /p/' . $sanitizedUrl);
exit;
}
} else if (!empty($originalText)) {
$sanitizedUrl = $this->processUrl($originalText);
if (!empty($sanitizedUrl)) {
header('Location: /p/' . urlencode($sanitizedUrl));
} elseif (!empty($originalText) && filter_var($originalText, FILTER_VALIDATE_URL)) {
$sanitizedText = $this->sanitizeUrl($originalText);
if (!empty($sanitizedText)) {
header('Location: /p/' . $sanitizedText);
exit;
}
} else {
header('Location: /?message=INVALID_URL');
exit;
}
header('Location: /?message=INVALID_URL');
exit;
}
header('Location: /');
exit;
@ -132,24 +156,6 @@ class Router
});
}
/**
* Process and sanitize a valid URL
* @param string $url
* @return string
*/
private function processUrl(string $url): string
{
if (!preg_match('#^https?://#', $url)) {
$url = 'https://' . $url;
}
if (!filter_var($url, FILTER_VALIDATE_URL)) {
return '';
}
return $this->sanitizeUrl($url);
}
/**
* Sanitizes and normalizes URLs
* @param string $url The URL to sanitize and normalize
@ -193,7 +199,8 @@ class Router
$cleanedUrl = preg_replace('/[\x00-\x1F\x7F]/', '', $cleanedUrl);
$cleanedUrl = filter_var($cleanedUrl, FILTER_SANITIZE_URL);
return $cleanedUrl;
// Convert special characters to HTML entities
return htmlspecialchars($cleanedUrl, ENT_QUOTES | ENT_HTML5, 'UTF-8');
}
/**

View file

@ -2,10 +2,10 @@
namespace App;
use Inc\Cache;
use Inc\Language;
use Inc\URLAnalyzer;
use Inc\URLAnalyzer\URLAnalyzerException;
use Inc\Cache;
/**
* URL Processor
@ -81,7 +81,8 @@ class URLProcessor
if (!$this->isApi) {
$redirectInfo = $this->analyzer->checkStatus($this->url);
if ($redirectInfo['hasRedirect'] && $redirectInfo['finalUrl'] !== $this->url) {
$this->redirect(SITE_URL . '/p/' . urlencode($redirectInfo['finalUrl']));
$finalUrl = preg_replace('#^https?://#', '', $redirectInfo['finalUrl']);
$this->redirect(SITE_URL . '/p/' . urlencode($finalUrl));
}
}
@ -89,8 +90,9 @@ class URLProcessor
$content = $this->analyzer->analyze($this->url);
if ($this->isApi) {
$displayUrl = preg_replace('#^https?://#', '', $this->url);
$this->sendApiResponse([
'url' => SITE_URL . '/p/' . urlencode($this->url),
'url' => SITE_URL . '/p/' . $displayUrl
]);
} else {
echo $content;
@ -123,7 +125,7 @@ class URLProcessor
$url = ''; // Initialize url variable for the view
// Initialize cache for counting
$cache = new Cache();
$cache = new \Inc\Cache();
$cache_folder = $cache->getCacheFileCount();
require __DIR__ . '/views/home.php';

View file

@ -68,7 +68,7 @@
<p class="walls_destroyed">
<strong><?php echo number_format($cache_folder, 0, ',', '.'); ?></strong> <span><?php echo \Inc\Language::get('walls_destroyed'); ?></span>
</p>
<form id="urlForm" method="GET" action="/" class="space-y-6">
<form id="urlForm" method="POST" action="/" class="space-y-6">
<div class="fields">
<div class="input">
<span class="icon icon--link"></span>
@ -115,7 +115,7 @@
<?php echo str_replace('{site_name}', SITE_NAME, \Inc\Language::get('bookmarklet_description')); ?>
</p>
<div>
<a href="javascript:(function(){window.location.href='<?php echo SITE_URL; ?>/p/'+encodeURIComponent(window.location.href);})()"
<a href="javascript:(function(){let currentUrl=window.location.href.replace(/^https?:\/\//, '');window.location.href='<?php echo SITE_URL; ?>/p/'+encodeURIComponent(currentUrl);})()"
onclick="return false;">
<?php echo str_replace('{site_name}', SITE_NAME, \Inc\Language::get('open_in')); ?>
</a>