From 4e6020b88cdba8334ee97ea14f8fc7d782f1998a Mon Sep 17 00:00:00 2001 From: Hailey Date: Wed, 6 Mar 2024 11:34:48 -0800 Subject: fix internal links showing up as external (#3128) * fix internal links showing up as external * fix internal links showing up as external --- src/lib/strings/url-helpers.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src/lib/strings/url-helpers.ts') diff --git a/src/lib/strings/url-helpers.ts b/src/lib/strings/url-helpers.ts index ba2cdb39b..ee88225ee 100644 --- a/src/lib/strings/url-helpers.ts +++ b/src/lib/strings/url-helpers.ts @@ -158,7 +158,12 @@ export function linkRequiresWarning(uri: string, label: string) { const host = urip.hostname.toLowerCase() // Hosts that end with bsky.app or bsky.social should be trusted by default. - if (host.endsWith('bsky.app') || host.endsWith('bsky.social')) { + if ( + uri.startsWith('/') || + host.endsWith('bsky.app') || + host.endsWith('bsky.social') || + host.endsWith('blueskyweb.xyz') + ) { // if this is a link to internal content, // warn if it represents itself as a URL to another app return !!labelDomain && labelDomain !== host && isPossiblyAUrl(labelDomain) -- cgit 1.4.1 From 800eecbfe4746c887933ecd34c20c862220d07ee Mon Sep 17 00:00:00 2001 From: Hailey Date: Wed, 6 Mar 2024 11:40:10 -0800 Subject: fix internal links showing up as external pt2 (#3129) * fix internal links showing up as external pt2 * fix internal links showing up as external pt2 --- src/lib/strings/url-helpers.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src/lib/strings/url-helpers.ts') diff --git a/src/lib/strings/url-helpers.ts b/src/lib/strings/url-helpers.ts index ee88225ee..7729e4a38 100644 --- a/src/lib/strings/url-helpers.ts +++ b/src/lib/strings/url-helpers.ts @@ -148,6 +148,11 @@ export function feedUriToHref(url: string): string { export function linkRequiresWarning(uri: string, label: string) { const labelDomain = labelToDomain(label) + // If the uri started with a / we know it is internal. + if (uri.startsWith('/')) { + return false + } + let urip try { urip = new URL(uri) @@ -156,10 +161,8 @@ export function linkRequiresWarning(uri: string, label: string) { } const host = urip.hostname.toLowerCase() - // Hosts that end with bsky.app or bsky.social should be trusted by default. if ( - uri.startsWith('/') || host.endsWith('bsky.app') || host.endsWith('bsky.social') || host.endsWith('blueskyweb.xyz') -- cgit 1.4.1 From 594958c6dc2a69155c19bcd108f19fe9c64f98be Mon Sep 17 00:00:00 2001 From: Eric Bailey Date: Sat, 9 Mar 2024 10:35:23 -0600 Subject: Fix RSS URLs treated as internal (#3156) * Fix RSS URLs treated as internal * Add utils to patch relative RSS external links * modify router to support multiple paths --------- Co-authored-by: Hailey --- src/lib/routes/router.ts | 10 ++++++++-- src/lib/strings/url-helpers.ts | 24 ++++++++++++++++++++++-- src/routes.ts | 2 +- src/state/preferences/in-app-browser.tsx | 9 +++++++++ 4 files changed, 40 insertions(+), 5 deletions(-) (limited to 'src/lib/strings/url-helpers.ts') diff --git a/src/lib/routes/router.ts b/src/lib/routes/router.ts index 00defaeda..8c8be3739 100644 --- a/src/lib/routes/router.ts +++ b/src/lib/routes/router.ts @@ -2,9 +2,15 @@ import {RouteParams, Route} from './types' export class Router { routes: [string, Route][] = [] - constructor(description: Record) { + constructor(description: Record) { for (const [screen, pattern] of Object.entries(description)) { - this.routes.push([screen, createRoute(pattern)]) + if (typeof pattern === 'string') { + this.routes.push([screen, createRoute(pattern)]) + } else { + pattern.forEach(subPattern => { + this.routes.push([screen, createRoute(subPattern)]) + }) + } } } diff --git a/src/lib/strings/url-helpers.ts b/src/lib/strings/url-helpers.ts index 7729e4a38..820311e4e 100644 --- a/src/lib/strings/url-helpers.ts +++ b/src/lib/strings/url-helpers.ts @@ -3,6 +3,8 @@ import {BSKY_SERVICE} from 'lib/constants' import TLDs from 'tlds' import psl from 'psl' +export const BSKY_APP_HOST = 'https://bsky.app' + export function isValidDomain(str: string): boolean { return !!TLDs.find(tld => { let i = str.lastIndexOf(tld) @@ -67,8 +69,21 @@ export function isBskyAppUrl(url: string): boolean { return url.startsWith('https://bsky.app/') } +export function isRelativeUrl(url: string): boolean { + return /^\/[^/]/.test(url) +} + +export function isBskyRSSUrl(url: string): boolean { + return ( + (url.startsWith('https://bsky.app/') || isRelativeUrl(url)) && + /\/rss\/?$/.test(url) + ) +} + export function isExternalUrl(url: string): boolean { - return !isBskyAppUrl(url) && url.startsWith('http') + const external = !isBskyAppUrl(url) && url.startsWith('http') + const rss = isBskyRSSUrl(url) + return external || rss } export function isBskyPostUrl(url: string): boolean { @@ -149,7 +164,7 @@ export function linkRequiresWarning(uri: string, label: string) { const labelDomain = labelToDomain(label) // If the uri started with a / we know it is internal. - if (uri.startsWith('/')) { + if (isRelativeUrl(uri)) { return false } @@ -222,3 +237,8 @@ export function splitApexDomain(hostname: string): [string, string] { hostnamep.domain, ] } + +export function createBskyAppAbsoluteUrl(path: string): string { + const sanitizedPath = path.replace(BSKY_APP_HOST, '').replace(/^\/+/, '') + return `${BSKY_APP_HOST.replace(/\/$/, '')}/${sanitizedPath}` +} diff --git a/src/routes.ts b/src/routes.ts index 3fc908b48..5c263fd6f 100644 --- a/src/routes.ts +++ b/src/routes.ts @@ -12,7 +12,7 @@ export const router = new Router({ ModerationModlists: '/moderation/modlists', ModerationMutedAccounts: '/moderation/muted-accounts', ModerationBlockedAccounts: '/moderation/blocked-accounts', - Profile: '/profile/:name', + Profile: ['/profile/:name', '/profile/:name/rss'], ProfileFollowers: '/profile/:name/followers', ProfileFollows: '/profile/:name/follows', ProfileList: '/profile/:name/lists/:rkey', diff --git a/src/state/preferences/in-app-browser.tsx b/src/state/preferences/in-app-browser.tsx index 4f033db65..2398f1f81 100644 --- a/src/state/preferences/in-app-browser.tsx +++ b/src/state/preferences/in-app-browser.tsx @@ -5,6 +5,11 @@ import * as WebBrowser from 'expo-web-browser' import {isNative} from '#/platform/detection' import {useModalControls} from '../modals' import {usePalette} from 'lib/hooks/usePalette' +import { + isBskyRSSUrl, + isRelativeUrl, + createBskyAppAbsoluteUrl, +} from 'lib/strings/url-helpers' type StateContext = persisted.Schema['useInAppBrowser'] type SetContext = (v: persisted.Schema['useInAppBrowser']) => void @@ -57,6 +62,10 @@ export function useOpenLink() { const openLink = React.useCallback( (url: string, override?: boolean) => { + if (isBskyRSSUrl(url) && isRelativeUrl(url)) { + url = createBskyAppAbsoluteUrl(url) + } + if (isNative && !url.startsWith('mailto:')) { if (override === undefined && enabled === undefined) { openModal({ -- cgit 1.4.1