Only warn on links to bsky.app if it represents itself as another url (#1662)

* Only warn on links to bsky.app if it represents itself as another url (close #1652)

* Clean up
zio/stable
Paul Frazee 2023-10-10 14:02:50 -07:00 committed by GitHub
parent e878da04a1
commit 4d450da194
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 5 deletions

View File

@ -27,6 +27,42 @@ describe('linkRequiresWarning', () => {
['http://site.pages', 'http://site.pages.dev', true], ['http://site.pages', 'http://site.pages.dev', true],
['http://site.pages.dev', 'site.pages', true], ['http://site.pages.dev', 'site.pages', true],
['http://site.pages', 'site.pages.dev', true], ['http://site.pages', 'site.pages.dev', true],
['http://bsky.app/profile/bob.test/post/3kbeuduu7m22v', 'my post', false],
['https://bsky.app/profile/bob.test/post/3kbeuduu7m22v', 'my post', false],
['http://bsky.app/', 'bluesky', false],
['https://bsky.app/', 'bluesky', false],
[
'http://bsky.app/profile/bob.test/post/3kbeuduu7m22v',
'http://bsky.app/profile/bob.test/post/3kbeuduu7m22v',
false,
],
[
'https://bsky.app/profile/bob.test/post/3kbeuduu7m22v',
'http://bsky.app/profile/bob.test/post/3kbeuduu7m22v',
false,
],
[
'http://bsky.app/',
'http://bsky.app/profile/bob.test/post/3kbeuduu7m22v',
false,
],
[
'https://bsky.app/',
'http://bsky.app/profile/bob.test/post/3kbeuduu7m22v',
false,
],
[
'http://bsky.app/profile/bob.test/post/3kbeuduu7m22v',
'https://google.com',
true,
],
[
'https://bsky.app/profile/bob.test/post/3kbeuduu7m22v',
'https://google.com',
true,
],
['http://bsky.app/', 'https://google.com', true],
['https://bsky.app/', 'https://google.com', true],
// bad uri inputs, default to true // bad uri inputs, default to true
['', '', true], ['', '', true],

View File

@ -170,14 +170,31 @@ export function getYoutubeVideoId(link: string): string | undefined {
export function linkRequiresWarning(uri: string, label: string) { export function linkRequiresWarning(uri: string, label: string) {
const labelDomain = labelToDomain(label) const labelDomain = labelToDomain(label)
let urip
try {
urip = new URL(uri)
} catch {
return true
}
if (urip.hostname === 'bsky.app') {
// if this is a link to internal content,
// warn if it represents itself as a URL to another app
if (
labelDomain &&
labelDomain !== 'bsky.app' &&
isPossiblyAUrl(labelDomain)
) {
return true
}
return false
} else {
// if this is a link to external content,
// warn if the label doesnt match the target
if (!labelDomain) { if (!labelDomain) {
return true return true
} }
try {
const urip = new URL(uri)
return labelDomain !== urip.hostname return labelDomain !== urip.hostname
} catch {
return true
} }
} }