fix(codebase audit): preserve cleanup without regressions

Fix codebase audit regressions while preserving UI/UX behavior and adding review-driven hardening.
This commit is contained in:
Tommaso Casaburi
2026-04-24 15:48:07 +07:00
committed by GitHub
parent 5df994b2c7
commit 5dc5408a15
70 changed files with 1478 additions and 518 deletions
+39 -1
View File
@@ -160,6 +160,20 @@ describe('media-utils', () => {
type: 'image',
url: 'https://example.com/file.png',
});
expect(getCommentMediaInfo('https://example.com/file.png', 'http://127.0.0.1/thumb.png', 320, 240)).toEqual({
linkHeight: 240,
linkWidth: 320,
thumbnail: undefined,
type: 'image',
url: 'https://example.com/file.png',
});
expect(getCommentMediaInfo('https://example.com/post', '//192.168.1.1/thumb.png', 320, 240)).toEqual({
linkHeight: 240,
linkWidth: 320,
thumbnail: undefined,
type: 'webpage',
url: 'https://example.com/post',
});
expect(getCommentMediaInfo('https://x.com/post/123', 'https://example.com/thumb.png', 100, 50)).toEqual({
linkHeight: 50,
linkWidth: 100,
@@ -220,7 +234,7 @@ describe('media-utils', () => {
url: 'https://example.com/og-page',
});
expect(testState.fetchMock).toHaveBeenCalledWith('https://example.com/og-page', expect.objectContaining({ headers: { Accept: 'text/html' } }));
expect(testState.fetchMock).toHaveBeenCalledWith('https://example.com/og-page', expect.objectContaining({ headers: { Accept: 'text/html' }, redirect: 'manual' }));
expect(testState.localForageSetItemMock).toHaveBeenCalledWith('https://example.com/og-page', 'https://cdn.example/og.png');
expect(result).toEqual({
thumbnail: 'https://cdn.example/og.png',
@@ -229,6 +243,29 @@ describe('media-utils', () => {
});
});
it('falls back to the first image when og:image is not allowed', async () => {
testState.fetchMock.mockResolvedValue(
createFetchResponse(`
<html>
<head><meta property="og:image" content="http://127.0.0.1/og.png" /></head>
<body><img src="https://cdn.example/fallback.png" /></body>
</html>
`),
);
const result = await fetchWebpageThumbnailIfNeeded({
type: 'webpage',
url: 'https://example.com/fallback-page',
});
expect(testState.localForageSetItemMock).toHaveBeenCalledWith('https://example.com/fallback-page', 'https://cdn.example/fallback.png');
expect(result).toEqual({
thumbnail: 'https://cdn.example/fallback.png',
type: 'webpage',
url: 'https://example.com/fallback-page',
});
});
it('fetches first-image thumbnails on native and resolves relative urls', async () => {
testState.isNativePlatform = true;
testState.capacitorHttpGetMock.mockResolvedValue({
@@ -247,6 +284,7 @@ describe('media-utils', () => {
expect(testState.capacitorHttpGetMock).toHaveBeenCalledWith(
expect.objectContaining({
connectTimeout: 5000,
disableRedirects: true,
headers: { Accept: 'text/html', Range: 'bytes=0-1048575' },
readTimeout: 5000,
responseType: 'text',
+19
View File
@@ -12,6 +12,7 @@ import {
copyShareLinkToClipboard,
getHostname,
is5chanLink,
isPrivateNetworkHostname,
isValidCrossboardPattern,
isValidPublishURL,
isValidURL,
@@ -28,9 +29,27 @@ describe('url-utils', () => {
expect(getHostname('https://www.5chan.app/#/music.eth')).toBe('5chan.app');
expect(getHostname('not-a-url')).toBe('');
expect(isValidURL('https://5chan.app')).toBe(true);
expect(isValidURL('http://5chan.app')).toBe(true);
expect(isValidURL('javascript:alert(1)')).toBe(false);
expect(isValidURL('data:text/html,hello')).toBe(false);
expect(isValidURL('file:///tmp/pic.png')).toBe(false);
expect(isValidURL('not-a-url')).toBe(false);
});
it('detects private network hostnames used by URL safety checks', () => {
expect(isPrivateNetworkHostname('localhost')).toBe(true);
expect(isPrivateNetworkHostname('branch.localhost')).toBe(true);
expect(isPrivateNetworkHostname('127.0.0.1')).toBe(true);
expect(isPrivateNetworkHostname('192.168.1.1')).toBe(true);
expect(isPrivateNetworkHostname('[::1]')).toBe(true);
expect(isPrivateNetworkHostname('[::ffff:7f00:1]')).toBe(true);
expect(isPrivateNetworkHostname('fc00::1')).toBe(true);
expect(isPrivateNetworkHostname('fd12:3456:789a::1')).toBe(true);
expect(isPrivateNetworkHostname('fcbarcelona.com')).toBe(false);
expect(isPrivateNetworkHostname('fdic.gov')).toBe(false);
expect(isPrivateNetworkHostname('example.com')).toBe(false);
});
it('normalizes publish links to the https URLs accepted by communities', () => {
expect(normalizePublishURL(' http://i.imgur.com/YpB7qfa.jpg ')).toBe('https://i.imgur.com/YpB7qfa.jpg');
expect(normalizePublishURL('https://i.imgur.com/YpB7qfa.jpg')).toBe('https://i.imgur.com/YpB7qfa.jpg');