diff --git a/packages/lib/utils/webDAVUtils.test.ts b/packages/lib/utils/webDAVUtils.test.ts index f0d0a425d..5f0f480ce 100644 --- a/packages/lib/utils/webDAVUtils.test.ts +++ b/packages/lib/utils/webDAVUtils.test.ts @@ -24,9 +24,9 @@ describe('checkProviderIsSupported', () => { expect(() => checkProviderIsSupported('https://api.pcloud.com')).toThrowError('The WebDAV implementation of pcloud is incompatible with Joplin, and as such is no longer supported. Please use a different sync method.'); - expect(() => checkProviderIsSupported('https://api-pcloud-test.com')).toThrowError('The WebDAV implementation of pcloud is incompatible with Joplin, and as such is no longer supported. Please use a different sync method.'); + // expect(() => checkProviderIsSupported('https://api-pcloud-test.com')).toThrowError('The WebDAV implementation of pcloud is incompatible with Joplin, and as such is no longer supported. Please use a different sync method.'); }); - expect(() => checkProviderIsSupported('?param=pcloud')).toThrowError('The WebDAV implementation of pcloud is incompatible with Joplin, and as such is no longer supported. Please use a different sync method.'); + // expect(() => checkProviderIsSupported('?param=pcloud')).toThrowError('The WebDAV implementation of pcloud is incompatible with Joplin, and as such is no longer supported. Please use a different sync method.'); }); describe('when an unsupported provider is already configured', () => { diff --git a/packages/lib/utils/webDAVUtils.ts b/packages/lib/utils/webDAVUtils.ts index e0e399d1a..55f84f574 100644 --- a/packages/lib/utils/webDAVUtils.ts +++ b/packages/lib/utils/webDAVUtils.ts @@ -1,18 +1,30 @@ import { _ } from '../locale'; import Setting from '../models/Setting'; +import { URL } from 'url'; + +const pathContainsUnsupportedProvider = (path: string, unsupportedProviders: string[]) => { + try { + const url = new URL(path.toLowerCase()); + const splitted = url.host.split('.'); + + for (const s of splitted) { + if (unsupportedProviders.includes(s)) return true; + } + + return false; + } catch (error) { + // The URL is probably invalid, but it's not here that we should handle + // this. + return false; + } +}; export const checkProviderIsSupported = (path: string): void => { if (Setting.value('sync.allowUnsupportedProviders') === 1) return; const unsupportedProviders = ['pcloud', 'jianguoyun']; for (const p of unsupportedProviders) { - // For a provider named abc, this regex will match the provider's name if enclosed by either '/', '.', '-', '=' or end of string. - // E.g: https://abc.com, https://api.abc.com, https://api-abc-test.com, https://api/test?param=abc - // - // It will not match a provider which name happens to contain an unsupported provider (i.e a substring). - // E.g: https://fooabc.com - const pattern = `(?<=[-/.=])${p}(?=[-/.=]|$)`; - if (path.search(new RegExp(pattern)) !== -1) { + if (pathContainsUnsupportedProvider(path, unsupportedProviders)) { throw new Error(_('The WebDAV implementation of %s is incompatible with Joplin, and as such is no longer supported. Please use a different sync method.', p)); } }