Private
Public Access
Add URL previews for chat messages (#43)
Slack/Discord-style link unfurling: the first http(s) URL in a message's content gets a small preview card (title/description/image/site name) fetched from the page's Open Graph tags. Backend: - Message.preview_url (extracted at create/edit time, cheap regex, no I/O) points at a link_previews cache row keyed by URL -- the same URL posted in different messages/rooms fetches once, and a failed fetch is cached too so a dead URL isn't retried on every reference. - The actual fetch runs in a background asyncio.create_task from broadcast_new_message/broadcast_message_update, on its own DB session, so a slow third-party site never delays message delivery. A separate "link_preview" WS envelope carries the result once it resolves. - SSRF protection reuses app/services/ssrf.py's validate_target_url (renamed from UnsafeWebhookUrlError to UnsafeUrlError now that it's shared with webhooks), but re-validates before every hop of a redirect chain rather than once up front -- redirects are followed manually so each intermediate URL is checked before it's ever connected to. - Parsed with stdlib html.parser -- no new dependency. Frontend: a LinkPreviewCard rendered under message content when present, patched into state live via the new WS envelope and included in message history for reloads. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -10,22 +10,22 @@ from app.models import PushSubscription
|
||||
from app.schemas.user import UserCreate
|
||||
from app.services.auth_service import register_user
|
||||
from app.services.room_service import join_room
|
||||
from app.services.ssrf import UnsafeWebhookUrlError, validate_target_url
|
||||
from app.services.ssrf import UnsafeUrlError, validate_target_url
|
||||
from tests.conftest import register_and_login
|
||||
|
||||
|
||||
def test_validate_target_url_rejects_loopback():
|
||||
with pytest.raises(UnsafeWebhookUrlError):
|
||||
with pytest.raises(UnsafeUrlError):
|
||||
validate_target_url("http://127.0.0.1/hook")
|
||||
|
||||
|
||||
def test_validate_target_url_rejects_private_range():
|
||||
with pytest.raises(UnsafeWebhookUrlError):
|
||||
with pytest.raises(UnsafeUrlError):
|
||||
validate_target_url("http://10.0.0.5/hook")
|
||||
|
||||
|
||||
def test_validate_target_url_rejects_non_http_scheme():
|
||||
with pytest.raises(UnsafeWebhookUrlError):
|
||||
with pytest.raises(UnsafeUrlError):
|
||||
validate_target_url("ftp://8.8.8.8/hook")
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user