Skip to content

Commit

Permalink
fix(proxy): extract uri for favicon proxying to improve privacy and i…
Browse files Browse the repository at this point in the history
…ncrease cache hit
  • Loading branch information
aleksasiriski committed Jun 25, 2024
1 parent f831f9d commit d220ba9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/lib/components/results/general/single.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script>
import Logo from '$lib/assets/logo.svg';
import { proxyImageLink } from '$lib/functions/api/proxyimage';
import { proxyFaviconLink } from '$lib/functions/api/proxyimage';
/**
* @typedef {object} Props
Expand All @@ -12,7 +12,7 @@
const favicon =
result.favicon_hash && result.favicon_hash != ''
? proxyImageLink(result.url, result.favicon_hash, true)
? proxyFaviconLink(result.url, result.favicon_hash)
: Logo;
const shortDesc =
Expand Down
17 changes: 17 additions & 0 deletions src/lib/functions/api/proxyimage.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,20 @@ export function proxyImageLink(url, hash, favicon = false) {

return apiUrl.toString();
}

/**
* Create a public API URL for the proxy favicon image endpoint
* @param {string} url
* @param {string} hash
* @returns {string}
*/
export function proxyFaviconLink(url, hash) {
const uriPattern = '^(http(s?))(://)([^/]+)';
const uriRegex = new RegExp(uriPattern);
const uriMatch = url.match(uriRegex);
if (!uriMatch || uriMatch.length == 0) {
throw error(400, 'Invalid URL');
}
const uri = uriMatch[0];
return proxyImageLink(uri, hash, true);
}

0 comments on commit d220ba9

Please sign in to comment.