Skip to content

Commit

Permalink
šŸ› Fix validation issue for invalid server URLs in /config-server page (ā€¦
Browse files Browse the repository at this point in the history
ā€¦#3837)

* Fix breaking /config-server page logic for invalid server URLs

* Handle missing url in 'subscribe-needs-bootstrap' handler
  • Loading branch information
shb9019 authored Nov 15, 2024
1 parent 224d445 commit dad702e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 17 deletions.
25 changes: 9 additions & 16 deletions packages/desktop-client/src/components/manager/ConfigServer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,16 @@ export function ConfigServer() {

setError(null);
setLoading(true);
const { error } = await setServerUrl(url);

if (
['network-failure', 'get-server-failure'].includes(error) &&
!url.startsWith('http://') &&
!url.startsWith('https://')
) {
const { error } = await setServerUrl('https://' + url);
if (error) {
setUrl('https://' + url);
setError(error);
} else {
await signOut();
navigate('/');
}
setLoading(false);
} else if (error) {
let httpUrl = url;
if (!url.startsWith('http://') && !url.startsWith('https://')) {
httpUrl = 'https://' + url;
}

const { error } = await setServerUrl(httpUrl);
setUrl(httpUrl);

if (error) {
setLoading(false);
setError(error);
} else {
Expand Down
6 changes: 5 additions & 1 deletion packages/loot-core/src/server/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ import * as prefs from './prefs';
import { app as reportsApp } from './reports/app';
import { app as rulesApp } from './rules/app';
import { app as schedulesApp } from './schedules/app';
import { getServer, setServer } from './server-config';
import { getServer, isValidBaseURL, setServer } from './server-config';
import * as sheet from './sheet';
import { resolveName, unresolveName } from './spreadsheet/util';
import {
Expand Down Expand Up @@ -1513,6 +1513,10 @@ handlers['get-did-bootstrap'] = async function () {
handlers['subscribe-needs-bootstrap'] = async function ({
url,
}: { url? } = {}) {
if (url && !isValidBaseURL(url)) {
return { error: 'get-server-failure' };
}

try {
if (!getServer(url)) {
return { bootstrapped: true, hasServer: false };
Expand Down
8 changes: 8 additions & 0 deletions packages/loot-core/src/server/server-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ function joinURL(base: string | URL, ...paths: string[]): string {
return url.toString();
}

export function isValidBaseURL(base: string): boolean {
try {
return Boolean(new URL(base));
} catch (error) {
return false;
}
}

export function setServer(url: string): void {
if (url == null) {
config = null;
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/3837.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Bugfix
authors: [shb9019]
---

Fix validation issue for invalid server URLs in /config-server page

0 comments on commit dad702e

Please sign in to comment.