Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Fix desktop OSM request identification

Anand Baburajan679a1bc841ec
diff --git a/desktop/src/main.ts b/desktop/src/main.ts
index 479a665ceb..9710704bd9 100644
--- a/desktop/src/main.ts
+++ b/desktop/src/main.ts
@@ -148,7 +148,7 @@ const main = () => {
allowExternalLinks(webContents);
handleBackOnStripeCheckout(mainWindow);
allowAllCORSOrigins(webContents);
- allowOpenStreetMapTileReferer(webContents);
+ allowOpenStreetMapRequestIdentification(webContents);
// Start loading the renderer.
void mainWindow.loadURL(rendererURL);
@@ -679,24 +679,40 @@ const allowAllCORSOrigins = (webContents: WebContents) =>
);
/**
- * OSM's standard tile servers require browser-based clients to send a valid
- * HTTP Referer. Our desktop renderer is served from the custom "ente://app"
- * origin, so Chromium does not send the kind of referrer OSM expects for these
- * tile requests.
- *
- * Inject a stable https referer for OSM tiles at the Electron session layer so
- * the desktop app identifies itself consistently without changing the renderer
- * code in each Leaflet map usage.
+ * OSM services expect requests to identify the originating app. Our desktop
+ * renderer is served from the custom "ente://app" origin, so requests from the
+ * Electron session do not naturally carry an app-specific browser identity.
+ *
+ * Prefix OSM requests with a truthful desktop app User-Agent token instead of
+ * forging a web Referer. This applies to both map tiles and Nominatim lookups.
*/
-const allowOpenStreetMapTileReferer = (webContents: WebContents) =>
+const allowOpenStreetMapRequestIdentification = (webContents: WebContents) =>
webContents.session.webRequest.onBeforeSendHeaders(
- { urls: ["https://tile.openstreetmap.org/*"] },
+ {
+ urls: [
+ "https://tile.openstreetmap.org/*",
+ "https://nominatim.openstreetmap.org/*",
+ ],
+ },
({ requestHeaders }, callback) => {
- requestHeaders.Referer = "https://ente.io/";
+ requestHeaders["User-Agent"] = openStreetMapUserAgent(
+ requestHeaders["User-Agent"],
+ );
+ delete requestHeaders.Referer;
+ delete requestHeaders.referer;
callback({ requestHeaders });
},
);
+const openStreetMapUserAgent = (existingUserAgent: string | undefined) => {
+ const appName = app.getName().trim() || "app";
+ const version = isDev ? "dev" : app.getVersion();
+ const appIdentifier = `${appName}-desktop/${version}`;
+ return existingUserAgent
+ ? `${appIdentifier} ${existingUserAgent}`
+ : appIdentifier;
+};
+
/**
* Add an icon for our app in the system tray.
*
diff --git a/web/packages/new/photos/components/EditLocationDialog.tsx b/web/packages/new/photos/components/EditLocationDialog.tsx
index 27312e0322..4a2cd66e42 100644
--- a/web/packages/new/photos/components/EditLocationDialog.tsx
+++ b/web/packages/new/photos/components/EditLocationDialog.tsx
@@ -272,7 +272,6 @@ const EditableMap: React.FC<EditableMapProps> = ({
const [showResults, setShowResults] = useState(false);
const [isSearching, setIsSearching] = useState(false);
const searchInputRef = useRef<HTMLInputElement>(null);
- const searchTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
const searchRequestIdRef = useRef(0);
const searchAbortRef = useRef<AbortController | null>(null);
@@ -286,55 +285,77 @@ const EditableMap: React.FC<EditableMapProps> = ({
setManualLon(null);
}, [open, selectedLocation]);
- // Search using Nominatim API
- const handleSearch = (query: string) => {
+ const handleSearchChange = (query: string) => {
setSearchQuery(query);
-
- if (searchTimeoutRef.current) {
- clearTimeout(searchTimeoutRef.current);
- }
if (searchAbortRef.current) {
searchAbortRef.current.abort();
searchAbortRef.current = null;
}
+ setIsSearching(false);
+ setShowResults(false);
if (query.trim().length < 3) {
setSearchResults([]);
- setShowResults(false);
- setIsSearching(false);
- return;
}
+ };
- searchTimeoutRef.current = setTimeout(async () => {
- const requestId = searchRequestIdRef.current + 1;
- searchRequestIdRef.current = requestId;
- const abortController = new AbortController();
- searchAbortRef.current = abortController;
- setIsSearching(true);
- try {
- const response = await fetch(
- `https://nominatim.openstreetmap.org/search?format=json&q=${encodeURIComponent(query)}&limit=5`,
- {
- headers: { "Accept-Language": "en" },
- signal: abortController.signal,
- },
- );
- const data = (await response.json()) as NominatimResult[];
- if (searchRequestIdRef.current !== requestId) return;
- setSearchResults(data);
- setShowResults(data.length > 0);
- } catch (error) {
- if (abortController.signal.aborted) return;
- if (searchRequestIdRef.current !== requestId) return;
- console.error("Search failed:", error);
+ const handleSearchSubmit = async (
+ e?: React.FormEvent<HTMLFormElement> | React.MouseEvent,
+ ) => {
+ e?.preventDefault();
+
+ const query = searchInputRef.current?.value.trim() ?? searchQuery.trim();
+ if (query.length < 3 || isSearching) {
+ if (query.length < 3) {
setSearchResults([]);
setShowResults(false);
- } finally {
- if (searchRequestIdRef.current === requestId) {
- setIsSearching(false);
- }
}
- }, 300);
+ return;
+ }
+
+ setSearchQuery(query);
+
+ if (searchAbortRef.current) {
+ searchAbortRef.current.abort();
+ }
+
+ const requestId = searchRequestIdRef.current + 1;
+ searchRequestIdRef.current = requestId;
+ const abortController = new AbortController();
+ searchAbortRef.current = abortController;
+ setIsSearching(true);
+
+ try {
+ await new Promise((resolve) => setTimeout(resolve, 750));
+ if (
+ searchRequestIdRef.current !== requestId ||
+ abortController.signal.aborted
+ ) {
+ return;
+ }
+
+ const response = await fetch(
+ `https://nominatim.openstreetmap.org/search?format=json&q=${encodeURIComponent(query)}&limit=5`,
+ {
+ headers: { "Accept-Language": "en" },
+ signal: abortController.signal,
+ },
+ );
+ const data = (await response.json()) as NominatimResult[];
+ if (searchRequestIdRef.current !== requestId) return;
+ setSearchResults(data);
+ setShowResults(data.length > 0);
+ } catch (error) {
+ if (abortController.signal.aborted) return;
+ if (searchRequestIdRef.current !== requestId) return;
+ console.error("Search failed:", error);
+ setSearchResults([]);
+ setShowResults(false);
+ } finally {
+ if (searchRequestIdRef.current === requestId) {
+ setIsSearching(false);
+ }
+ }
};
const handleSelectResult = (result: NominatimResult) => {
@@ -406,9 +427,6 @@ const EditableMap: React.FC<EditableMapProps> = ({
useEffect(() => {
return () => {
- if (searchTimeoutRef.current) {
- clearTimeout(searchTimeoutRef.current);
- }
if (searchAbortRef.current) {
searchAbortRef.current.abort();
}
@@ -541,27 +559,82 @@ const EditableMap: React.FC<EditableMapProps> = ({
<MapContainer ref={mapContainerRef} />
<SearchContainer>
<ClickAwayListener onClickAway={() => setShowResults(false)}>
- <Box sx={{ position: "relative", width: "100%" }}>
+ <Box
+ component="form"
+ onSubmit={handleSearchSubmit}
+ sx={{ position: "relative", width: "100%" }}
+ >
<SearchInputWrapper>
- <SearchIcon
- sx={{ color: "text.muted", fontSize: 20 }}
- />
<InputBase
placeholder={t("search_location")}
value={searchQuery}
- onChange={(e) => handleSearch(e.target.value)}
+ onChange={(e) =>
+ handleSearchChange(e.target.value)
+ }
onFocus={() =>
searchResults.length > 0 &&
setShowResults(true)
}
inputRef={searchInputRef}
- sx={{ ml: 1 }}
- inputProps={{ style: { padding: 0 } }}
- />
- <CircularProgress
- size={16}
- sx={{ ml: 1, opacity: isSearching ? 1 : 0 }}
+ sx={{ flex: 1 }}
+ inputProps={{
+ style: {
+ padding: 0,
+ paddingLeft: 8,
+ },
+ }}
/>
+ <IconButton
+ type="submit"
+ size="small"
+ disabled={
+ isSearching || searchQuery.trim().length < 3
+ }
+ aria-label={t("search_location")}
+ sx={(theme) => ({
+ ml: 1,
+ p: 0,
+ width: 32,
+ height: 32,
+ backgroundColor:
+ theme.vars.palette.fill.faint,
+ color: theme.vars.palette.text.base,
+ transition:
+ "background-color 0.2s ease, transform 0.2s ease",
+ "&:hover": {
+ backgroundColor:
+ theme.vars.palette.fill.faintHover,
+ },
+ "&:active": {
+ backgroundColor:
+ theme.vars.palette.fill.faintHover,
+ transform: "scale(0.96)",
+ },
+ "&.Mui-disabled": {
+ backgroundColor:
+ theme.vars.palette.background.elevatedPaper,
+ color: theme.vars.palette.text.faint,
+ },
+ })}
+ >
+ {isSearching ? (
+ <CircularProgress
+ size={18}
+ thickness={5}
+ sx={{
+ display: "block",
+ color: "inherit",
+ }}
+ />
+ ) : (
+ <SearchIcon
+ sx={{
+ color: "inherit",
+ fontSize: 20,
+ }}
+ />
+ )}
+ </IconButton>
</SearchInputWrapper>
{showResults && (
<SearchResultsList>
@@ -682,15 +755,17 @@ const CoordinateInput = styled(InputBase)(({ theme }) => ({
}));
const GoToLocationButton = styled(IconButton)(({ theme }) => ({
- backgroundColor: theme.vars.palette.text.base,
- color: theme.vars.palette.background.default,
+ backgroundColor: theme.vars.palette.background.paper,
+ color: theme.vars.palette.text.base,
width: 36,
height: 36,
boxShadow: theme.shadows[4],
- "&:hover": { backgroundColor: theme.vars.palette.text.muted },
+ "&:hover": {
+ backgroundColor: theme.vars.palette.background.elevatedPaper,
+ },
"&.Mui-disabled": {
- backgroundColor: theme.vars.palette.text.faint,
- color: theme.vars.palette.background.default,
+ backgroundColor: theme.vars.palette.background.elevatedPaper,
+ color: theme.vars.palette.text.faint,
},
}));
@@ -728,9 +803,9 @@ const SearchContainer = styled("div")(({ theme }) => ({
const SearchInputWrapper = styled("div")(({ theme }) => ({
display: "inline-flex",
alignItems: "center",
+ width: "min(100%, 360px)",
backgroundColor: theme.vars.palette.background.paper,
padding: theme.spacing(1),
- paddingLeft: theme.spacing(1.5),
borderRadius: "9999px",
boxShadow: theme.shadows[4],
}));
diff --git a/web/packages/new/photos/components/SelectedFileOptions.tsx b/web/packages/new/photos/components/SelectedFileOptions.tsx
index 27ff419ece..b272fa5ede 100644
--- a/web/packages/new/photos/components/SelectedFileOptions.tsx
+++ b/web/packages/new/photos/components/SelectedFileOptions.tsx
@@ -10,6 +10,7 @@ import {
RemoveCircleIcon,
Time04Icon,
Unarchive03Icon,
+ UserAdd02Icon,
ViewIcon,
ViewOffSlashIcon,
} from "@hugeicons/core-free-icons";
@@ -17,7 +18,6 @@ import { HugeiconsIcon } from "@hugeicons/react";
import CheckCircleIcon from "@mui/icons-material/CheckCircle";
import CheckCircleOutlineIcon from "@mui/icons-material/CheckCircleOutline";
import CloseIcon from "@mui/icons-material/Close";
-import PersonAddIcon from "@mui/icons-material/PersonAdd";
import { Box, Button, IconButton, Tooltip, Typography } from "@mui/material";
import { SpacedRow } from "ente-base/components/containers";
import type { ButtonishProps } from "ente-base/components/mui";
@@ -615,7 +615,7 @@ const AddToCollectionButton: React.FC<ButtonishProps> = ({ onClick }) => (
const AddPersonButton: React.FC<ButtonishProps> = ({ onClick }) => (
<Tooltip title={t("add_a_person")}>
<IconButton {...{ onClick }}>
- <PersonAddIcon />
+ <HugeiconsIcon icon={UserAdd02Icon} />
</IconButton>
</Tooltip>
);