Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions packages/shared/src/components/profile/SocialLinksInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,36 @@ describe('SocialLinksInput', () => {
);
});

it('stores unrecognized domains as generic links', async () => {
const onSubmit = jest.fn();
render(<TestForm onSubmit={onSubmit} />);

await userEvent.type(
screen.getByPlaceholderText('Paste a URL (e.g., github.com/username)'),
'https://dabworx.com',
);

expect(screen.queryByText('X detected')).not.toBeInTheDocument();

await userEvent.click(screen.getByRole('button', { name: 'Add' }));

expect(await screen.findByText('Link')).toBeVisible();
expect(screen.queryByText('X')).not.toBeInTheDocument();

await userEvent.click(screen.getByRole('button', { name: 'Save' }));

await waitFor(() =>
expect(onSubmit).toHaveBeenCalledWith({
socialLinks: [
{
platform: 'other',
url: 'https://dabworx.com',
},
],
}),
);
});

it('blocks submit and renders an inline error for invalid pending text', async () => {
const onSubmit = jest.fn();
render(<TestForm onSubmit={onSubmit} />);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { OrganizationLinkType } from '../types';
import { detectPlatform } from './platformDetection';

describe('detectPlatform', () => {
it('should detect TechCrunch press links by exact domain', () => {
expect(detectPlatform('https://techcrunch.com/2026/09/21/story')).toEqual({
platform: 'TechCrunch',
socialType: null,
linkType: OrganizationLinkType.Press,
defaultLabel: 'Press Release',
});
});

it('should not detect press domains by substring', () => {
expect(
detectPlatform('https://mytechcrunch.com/2026/09/21/story'),
).toBeNull();
expect(
detectPlatform('https://techcrunch.com.attacker.example/story'),
).toBeNull();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
detectPlatformFromUrl,
getPlatformIconElement,
getPlatformLabel as getGenericPlatformLabel,
matchesDomain,
} from '../../../lib/platforms';

export type LinkItem = {
Expand Down Expand Up @@ -89,7 +90,7 @@ const isPressUrl = (url: string): boolean => {
try {
const parsed = new URL(url.startsWith('http') ? url : `https://${url}`);
const hostname = parsed.hostname.replace(/^(www\.|m\.|mobile\.)/, '');
return PRESS_DOMAINS.some((domain) => hostname.includes(domain));
return PRESS_DOMAINS.some((domain) => matchesDomain(hostname, domain));
} catch {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export function AboutMe({
{shouldShowSocialLinks && (
<div className="flex flex-wrap items-center gap-2">
{socialLinks.map((link) => (
<SimpleTooltip key={link.id} content={link.label}>
<SimpleTooltip key={link.url} content={link.label}>
<Button
variant={ButtonVariant.Subtle}
size={ButtonSize.Small}
Expand Down
50 changes: 49 additions & 1 deletion packages/shared/src/lib/platforms.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { detectPlatformFromUrl, USER_PLATFORMS } from './platforms';
import {
detectPlatformFromUrl,
ORG_PLATFORMS,
USER_PLATFORMS,
} from './platforms';

describe('detectPlatformFromUrl', () => {
it('should detect medium.com as medium, not mastodon', () => {
Expand Down Expand Up @@ -52,6 +56,50 @@ describe('detectPlatformFromUrl', () => {
).toBe('discord');
});

it('should detect exact domains and subdomains only', () => {
expect(detectPlatformFromUrl('https://x.com/handle', USER_PLATFORMS)).toBe(
'twitter',
);
expect(
detectPlatformFromUrl('https://www.x.com/handle', USER_PLATFORMS),
).toBe('twitter');
expect(detectPlatformFromUrl('https://dev.to/user', ORG_PLATFORMS)).toBe(
'devto',
);
expect(
detectPlatformFromUrl('https://m.youtube.com/@channel', USER_PLATFORMS),
).toBe('youtube');
expect(
detectPlatformFromUrl('https://youtu.be/video123', USER_PLATFORMS),
).toBe('youtube');
expect(
detectPlatformFromUrl('https://blog.hashnode.dev/post', USER_PLATFORMS),
).toBe('hashnode');
});

it.each([
['https://dabworx.com', USER_PLATFORMS],
['https://netflix.com', USER_PLATFORMS],
['https://linux.com', USER_PLATFORMS],
['https://mygithub.com', USER_PLATFORMS],
['https://dev.tools', ORG_PLATFORMS],
['https://github.com.attacker.example/foo', USER_PLATFORMS],
])('should not detect platform domains inside %s', (url, platforms) => {
expect(detectPlatformFromUrl(url, platforms)).toBeNull();
});

it('should not match invalid raw input by substring after URL parsing fails', () => {
expect(detectPlatformFromUrl('x.com bad', USER_PLATFORMS)).toBeNull();
});

it('should detect Mastodon instances whose hostnames contain a known domain substring', () => {
expect(
detectPlatformFromUrl('https://socialx.com/@user', {
mastodon: USER_PLATFORMS.mastodon,
}),
).toBe('mastodon');
});

it('should return null for unknown URLs', () => {
expect(
detectPlatformFromUrl('https://example.com/profile', USER_PLATFORMS),
Expand Down
7 changes: 5 additions & 2 deletions packages/shared/src/lib/platforms.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,9 @@ const normalizeHostname = (url: string): string => {
}
};

export const matchesDomain = (hostname: string, domain: string): boolean =>
hostname === domain || hostname.endsWith(`.${domain}`);

/**
* Check if URL matches Mastodon pattern (/@username)
*/
Expand Down Expand Up @@ -306,7 +309,7 @@ export function detectPlatformFromUrl<T extends Record<string, PlatformConfig>>(

// Check each platform's domains
const matchedEntry = Object.entries(platforms).find(([, config]) =>
config.domains.some((domain) => hostname.includes(domain)),
config.domains.some((domain) => matchesDomain(hostname, domain)),
);

if (matchedEntry) {
Expand All @@ -326,7 +329,7 @@ export function detectPlatformFromUrl<T extends Record<string, PlatformConfig>>(
].flatMap((config) => config.domains);

const isKnownDomain = allKnownDomains.some((domain) =>
hostname.includes(domain),
matchesDomain(hostname, domain),
);

if (!isKnownDomain) {
Expand Down
Loading