). Enforce server-side so malicious
+// clients can't poison the DB with traversal-shaped names that later
+// surface in UIs or get forwarded to filesystem APIs.
+function collabAttachmentFilenameValid(string $fn): bool {
+ return (bool)preg_match('/^[a-f0-9]{8,64}\.[a-z0-9]{1,8}$/', $fn);
+}
+
if ($route === '/api/collab-attachments' && $method === 'POST') {
if (!$clientTenant) jsonResponse(['error' => 'unauthorized'], 401);
$input = json_decode(file_get_contents('php://input'), true) ?: [];
@@ -250,6 +258,7 @@ if ($route === '/api/collab-attachments' && $method === 'POST') {
$filename = (string)($input['filename'] ?? '');
$data = (string)($input['data'] ?? '');
if (!$bucketUid || !$filename || !$data) jsonResponse(['error' => 'bucket_uid, filename, and data required'], 400);
+ if (!collabAttachmentFilenameValid($filename)) jsonResponse(['error' => 'invalid filename'], 400);
if (!collabCardsIsMember($db, $bucketUid, (int)$clientTenant['id'])) {
jsonResponse(['error' => 'not a member of this bucket'], 403);
}
@@ -277,6 +286,7 @@ if ($route === '/api/collab-attachments' && $method === 'GET') {
$bucketUid = trim($_GET['bucket_uid'] ?? '');
$filename = trim($_GET['filename'] ?? '');
if (!$bucketUid) jsonResponse(['error' => 'bucket_uid required'], 400);
+ if ($filename !== '' && !collabAttachmentFilenameValid($filename)) jsonResponse(['error' => 'invalid filename'], 400);
if (!collabCardsIsMember($db, $bucketUid, (int)$clientTenant['id'])) {
jsonResponse(['error' => 'not a member of this bucket'], 403);
}
diff --git a/relay/inc/routes/collab-locks.php b/relay/inc/routes/collab-locks.php
index 953e8c6..18c1370 100644
--- a/relay/inc/routes/collab-locks.php
+++ b/relay/inc/routes/collab-locks.php
@@ -20,6 +20,13 @@ function collabLocksIsMember(PDO $db, string $bucketUid, int $tenantId): bool {
// ── POST: acquire/refresh ────────────────────────────────────────
+// Maximum time a single holder can keep refreshing the same lock. Without a
+// cap a malicious member could heartbeat forever, blocking every other
+// member's writes. 10 minutes is long enough for any real edit session and
+// short enough to time-box grief. When the cap is hit the lock is force-
+// released and the client must re-acquire — giving everyone else a window.
+const COLLAB_LOCK_MAX_HOLD_SECONDS = 600;
+
if ($route === '/api/collab-locks' && $method === 'POST') {
if (!$clientTenant) jsonResponse(['error' => 'unauthorized'], 401);
if (isRateLimited($db, 'collab-locks-post', 240)) jsonResponse(['error' => 'rate limited'], 429);
@@ -35,8 +42,12 @@ if ($route === '/api/collab-locks' && $method === 'POST') {
// narrow window between cleanup and this query.
$db->prepare("DELETE FROM collab_card_locks WHERE bucket_uid = :b AND card_uid = :c AND expires_at <= datetime('now')")
->execute(['b' => $bucketUid, 'c' => $cardUid]);
+ // Also drop locks that have been refreshed past the max hold window —
+ // cap enforcement for grief prevention.
+ $db->prepare("DELETE FROM collab_card_locks WHERE bucket_uid = :b AND card_uid = :c AND acquired_at <= datetime('now', '-" . COLLAB_LOCK_MAX_HOLD_SECONDS . " seconds')")
+ ->execute(['b' => $bucketUid, 'c' => $cardUid]);
// Existing live lock? If a different tenant holds it, refuse.
- $cur = $db->prepare('SELECT holder_id, holder_username, expires_at FROM collab_card_locks WHERE bucket_uid = :b AND card_uid = :c');
+ $cur = $db->prepare('SELECT holder_id, holder_username, expires_at, acquired_at FROM collab_card_locks WHERE bucket_uid = :b AND card_uid = :c');
$cur->execute(['b' => $bucketUid, 'c' => $cardUid]);
$existing = $cur->fetch();
if ($existing && (int)$existing['holder_id'] !== (int)$clientTenant['id']) {
diff --git a/relay/inc/routes/messages.php b/relay/inc/routes/messages.php
index bf3f901..6536f59 100644
--- a/relay/inc/routes/messages.php
+++ b/relay/inc/routes/messages.php
@@ -21,7 +21,7 @@ if ($route === '/api/messages' && $method === 'POST' && $sessionTenant && empty(
if ($body === '') jsonResponse(['error' => 'body required'], 400);
$recipient = getTenantByUsername($db, $recipientName);
- if (!$recipient) jsonResponse(['error' => "no such user: $recipientName"], 404);
+ if (!$recipient) jsonResponse(['error' => 'recipient not found'], 404);
$interKey = getInterTenantKey($db, $MASTER_KEY);
if (!$interKey) jsonResponse(['error' => 'relay super-shared key not set'], 500);
@@ -54,7 +54,7 @@ if ($route === '/api/attachments/upload' && $method === 'POST' && $sessionTenant
if (!$relayUid) jsonResponse(['error' => 'relay_uid required'], 400);
$recipient = getTenantByUsername($db, $recipientName);
- if (!$recipient) jsonResponse(['error' => "no such user: $recipientName"], 404);
+ if (!$recipient) jsonResponse(['error' => 'recipient not found'], 404);
$interKey = getInterTenantKey($db, $MASTER_KEY);
if (!$interKey) jsonResponse(['error' => 'relay super-shared key not set'], 500);
@@ -142,7 +142,7 @@ if ($route === '/api/messages' && $method === 'POST') {
if ($encryptedBody === '') jsonResponse(['error' => 'body required'], 400);
$recipient = getTenantByUsername($db, $recipientName);
- if (!$recipient) jsonResponse(['error' => "no such user: $recipientName"], 404);
+ if (!$recipient) jsonResponse(['error' => 'recipient not found'], 404);
// Decrypt with sender's secret
if (empty($clientTenant['secret_encrypted'])) {
@@ -225,9 +225,12 @@ if ($route === '/api/attachments' && $method === 'POST') {
$data = (string)($input['data'] ?? '');
$recipientName = trim((string)($input['recipient'] ?? $clientTenant['username']));
if (!$relayUid || !$filename || !$data) jsonResponse(['error' => 'relay_uid, filename, and data required'], 400);
+ // Enforce the hyprcrd upload naming scheme server-side so traversal-shaped
+ // names never enter storage or get forwarded back to other clients.
+ if (!preg_match('/^[a-f0-9]{8,64}\.[a-z0-9]{1,8}$/', $filename)) jsonResponse(['error' => 'invalid filename'], 400);
$recipient = getTenantByUsername($db, $recipientName);
- if (!$recipient) jsonResponse(['error' => "no such user: $recipientName"], 404);
+ if (!$recipient) jsonResponse(['error' => 'recipient not found'], 404);
// Decrypt-reencrypt hop, same shape as messages: arrives encrypted with
// sender's secret, gets stored at-rest under the inter-tenant key.
diff --git a/relay/inc/routes/public.php b/relay/inc/routes/public.php
index ad677c8..1a83ca5 100644
--- a/relay/inc/routes/public.php
+++ b/relay/inc/routes/public.php
@@ -49,7 +49,7 @@ if ($route === '/api/setup' && $method === 'POST') {
if (!preg_match('/^[a-zA-Z0-9_-]{2,32}$/', $username)) {
jsonResponse(['error' => 'username must be 2-32 chars: letters, digits, _ or -'], 400);
}
- if (strlen($password) < 4) jsonResponse(['error' => 'password must be at least 4 chars'], 400);
+ if (strlen($password) < 10) jsonResponse(['error' => 'password must be at least 10 chars'], 400);
if (strlen($superSecret) < 12) jsonResponse(['error' => 'super-shared secret must be at least 12 chars'], 400);
// Create the admin tenant. NO client secret yet — the admin (and every
diff --git a/relay/index.php b/relay/index.php
index d832f1a..dd92dd8 100644
--- a/relay/index.php
+++ b/relay/index.php
@@ -40,10 +40,25 @@ $isSetup = tenantCount($db) === 0;
if (str_starts_with($route, '/api/')) {
header('Content-Type: application/json');
+ // CORS: the hyprcrd client hits us server-side (no browser CORS in play)
+ // and the relay UI lives same-origin, so the only legitimate cross-origin
+ // callers are other tenant UIs on the same relay host. We allow the
+ // relay's own scheme+host, plus an opt-in allowlist via RELAY_CORS_ORIGINS
+ // (comma-separated). Credentials are intentionally NOT allowed — API auth
+ // is header-based (X-Relay-User/Key) and the UI is same-origin.
$origin = $_SERVER['HTTP_ORIGIN'] ?? '';
- if ($origin) {
+ $ownScheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || ($_SERVER['HTTP_X_FORWARDED_PROTO'] ?? '') === 'https' ? 'https' : 'http';
+ $ownOrigin = $ownScheme . '://' . ($_SERVER['HTTP_HOST'] ?? '');
+ $allowed = [$ownOrigin];
+ $extra = getenv('RELAY_CORS_ORIGINS');
+ if ($extra) {
+ foreach (explode(',', $extra) as $o) {
+ $o = trim($o);
+ if ($o !== '') $allowed[] = $o;
+ }
+ }
+ if ($origin && in_array($origin, $allowed, true)) {
header('Access-Control-Allow-Origin: ' . $origin);
- header('Access-Control-Allow-Credentials: true');
header('Vary: Origin');
}
header('Access-Control-Allow-Headers: Content-Type, X-Relay-User, X-Relay-Key');
diff --git a/relay/ui/app.js b/relay/ui/app.js
index f3d745e..ec755b3 100644
--- a/relay/ui/app.js
+++ b/relay/ui/app.js
@@ -199,7 +199,7 @@ async function doChangePassword() {
const err = $('cp-err');
err.textContent = '';
if (!current || !newPw) { err.textContent = 'all fields required'; return; }
- if (newPw.length < 4) { err.textContent = 'new password too short'; return; }
+ if (newPw.length < 10) { err.textContent = 'new password must be at least 10 chars'; return; }
if (newPw !== newPw2) { err.textContent = 'new passwords don\'t match'; return; }
if (current === newPw) { err.textContent = 'new password must differ from current'; return; }
@@ -231,7 +231,7 @@ async function doSetup() {
const err = $('setup-err');
err.textContent = '';
if (!username) { err.textContent = 'username required'; return; }
- if (password.length < 4) { err.textContent = 'password too short'; return; }
+ if (password.length < 10) { err.textContent = 'password must be at least 10 chars'; return; }
if (superSecret.length < 12) { err.textContent = 'super-shared secret needs at least 12 chars'; return; }
const res = await api('api/setup', { method: 'POST', body: { username, password, super_secret: superSecret } });
if (res.ok) {
@@ -1039,8 +1039,8 @@ async function createUser() {
}
async function resetUserPassword(id, username) {
- const newPw = prompt(`new password for ${username} (min 4 chars):`);
- if (!newPw || newPw.length < 4) { toast('password too short', true); return; }
+ const newPw = prompt(`new password for ${username} (min 10 chars):`);
+ if (!newPw || newPw.length < 10) { toast('password too short', true); return; }
const res = await api('api/users/' + id, { method: 'PATCH', body: { password: newPw } });
if (res.ok) toast('password updated');
else toast(res.error || 'failed', true);
diff --git a/relay/ui/layout.php b/relay/ui/layout.php
index 9cf75d1..23e37b6 100644
--- a/relay/ui/layout.php
+++ b/relay/ui/layout.php
@@ -47,7 +47,7 @@ if (!defined('RELAY_BOOTSTRAPPED')) { http_response_code(404); exit; }
-
+
@@ -144,7 +144,7 @@ if (!defined('RELAY_BOOTSTRAPPED')) { http_response_code(404); exit; }
-
+
@@ -205,7 +205,7 @@ if (!defined('RELAY_BOOTSTRAPPED')) { http_response_code(404); exit; }
-
+