This commit is contained in:
2026-11-28 11:21:08 -05:00
parent df4861cba6
commit 2d3ee73186
4 changed files with 170 additions and 12 deletions

View File

@ -10,6 +10,8 @@ $max_url_length = 100; // urls longer than this are considered nefarious
$allowed_domains = []; // list of allowed domains for URL shortening
$password = 'change me!'; // password to shorten a URL, leave empty to disable password protection
$enable_test = false; // enable the test function to output a series of incremented keys
$auth_cookie = 'shortener_auth'; // name of the cookie that remembers a correct password
$auth_cookie_days = 365; // how long the remembered password lasts
// Returns the contents of $last_file, or empty string if the file does not exist
function get_last() {
@ -146,6 +148,63 @@ $key = get_input('key');
$test_start = get_input('test_start');
$test_count = get_input('test_count');
// The auth cookie holds an HMAC of the password, never the password itself.
// Changing $password invalidates every cookie already issued.
function auth_token() {
global $password;
return hash_hmac('sha256', 'shortener-auth', $password);
}
// True if the request carries a valid auth cookie
function has_auth_cookie() {
global $password, $auth_cookie;
return $password !== ''
&& isset($_COOKIE[$auth_cookie])
&& is_string($_COOKIE[$auth_cookie])
&& hash_equals(auth_token(), $_COOKIE[$auth_cookie]);
}
// Sets the auth cookie, or clears it when $value is ''
function set_auth_cookie($value) {
global $auth_cookie, $auth_cookie_days;
setcookie($auth_cookie, $value, [
'expires' => $value === '' ? time() - 3600 : time() + $auth_cookie_days * 86400,
'path' => '/',
'secure' => !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off',
'httponly' => true,
'samesite' => 'Lax',
]);
}
// Checks the password or auth cookie. On a correct password with the
// "remember" box ticked, sets the cookie. The cookie only counts for POST,
// so a link like /?url=... on another site can't create short links with
// a visitor's cookie.
function is_authorized() {
global $password, $input_password, $has_cookie;
if ($password === '') {
return true;
}
if (is_string($input_password) && hash_equals($password, $input_password)) {
if (get_input('remember') === '1') {
set_auth_cookie(auth_token());
$has_cookie = true;
}
return true;
}
return $has_cookie && $_SERVER['REQUEST_METHOD'] === 'POST';
}
$has_cookie = has_auth_cookie();
// Log out: clear the cookie, then redirect so a refresh doesn't resubmit.
// POST only, so another site can't log a visitor out with a plain link.
if ($_SERVER['REQUEST_METHOD'] === 'POST' && get_input('logout') !== null) {
set_auth_cookie('');
header('Location: /', true, 303);
exit;
}
// Requests from the HTML form send format=html and get the result rendered
// in the page; everything else (curl, scripts) gets the plain-text response.
$from_form = get_input('format') === 'html';
@ -156,8 +215,8 @@ $error = null;
if ($url) {
$status = 200;
$parsed_url = parse_url($url);
if ($password !== '' && $input_password !== $password) {
// if a password is set, check it
if (!is_authorized()) {
// if a password is set, check it (or the auth cookie)
$status = 401; // Unauthorized
$error = "Unauthorized: Incorrect password.";
} elseif (strlen($url) > $max_url_length) {
@ -185,9 +244,9 @@ if ($url) {
}
// If test parameters are provided, output a series of incremented keys
if ($enable_test &&$test_start !== null && $test_count !== null) {
// validate password
if ($password !== '' && $input_password !== $password) {
if ($enable_test && $test_start !== null && $test_count !== null) {
// validate password (or the auth cookie)
if (!is_authorized()) {
http_response_code(401); // Unauthorized
echo "Unauthorized: Incorrect password.\n";
exit;
@ -284,6 +343,19 @@ if ($key !== '' && !$from_form) {
cursor: pointer;
}
.result a { display: inline-block; margin-top: 0.5rem; color: #15803d; word-break: break-all; }
.remember { display: flex; align-items: center; gap: 0.5rem; font-weight: normal; margin: -0.25rem 0 1rem; }
.remember input { width: 1.15rem; height: 1.15rem; margin: 0; accent-color: #2563eb; }
.logout { margin: 1.5rem 0 0; text-align: center; }
.logout button {
padding: 0.5rem;
font: inherit;
font-size: 0.9rem;
color: #6b7280;
background: none;
border: 0;
text-decoration: underline;
cursor: pointer;
}
.error { background: #fef2f2; border: 1px solid #f5b5b5; color: #b42318; font-weight: 600; }
@media (max-width: 480px) {
body { padding: 0.75rem; }
@ -297,6 +369,7 @@ if ($key !== '' && !$from_form) {
}
.result { background: #0f2a1b; border-color: #1f5a37; }
.result a { color: #6ee7a0; }
.logout button { color: #9ca3af; }
.error { background: #2d1515; border-color: #6b2a2a; color: #fca5a5; }
}
</style>
@ -341,9 +414,10 @@ if ($key !== '' && !$from_form) {
<input type="url" id="url" name="url" required placeholder="https://example.com/..."
value="<?= $error !== null ? htmlspecialchars($url) : '' ?>"
autocapitalize="off" autocorrect="off" spellcheck="false">
<?php if ($password !== ''): ?>
<?php if ($password !== '' && !$has_cookie): ?>
<label for="password">Enter the password:</label>
<input type="password" id="password" name="password" autocomplete="current-password" required>
<label class="remember"><input type="checkbox" name="remember" value="1"<?= get_input('remember') === '1' ? ' checked' : '' ?>> Remember me for a year</label>
<?php endif; ?>
<input type="submit" value="Shorten">
</form>
@ -356,13 +430,20 @@ if ($key !== '' && !$from_form) {
autocapitalize="off" autocorrect="off" spellcheck="false">
<label for="test_count">How many keys to generate:</label>
<input type="number" id="test_count" name="test_count" min="1" inputmode="numeric" required>
<?php if ($password !== ''): ?>
<?php if ($password !== '' && !$has_cookie): ?>
<label for="test_password">Enter the password:</label>
<input type="password" id="test_password" name="password" autocomplete="current-password" required>
<label class="remember"><input type="checkbox" name="remember" value="1"<?= get_input('remember') === '1' ? ' checked' : '' ?>> Remember me for a year</label>
<?php endif; ?>
<input type="submit" value="Generate">
</form>
<?php endif; ?>
<?php if ($has_cookie): ?>
<form method="post" action="/" class="logout">
<button type="submit" name="logout" value="1">Log out</button>
</form>
<?php endif; ?>
</main>
</body>
</html>