From d4a45ee4fc7400eeed4ff3f8d615dbe402171d95 Mon Sep 17 00:00:00 2001 From: f1ames Date: Fri, 1 Dec 2023 10:56:15 +0100 Subject: [PATCH 1/4] fix(synchronizer): make sure https is default proto --- packages/synchronizer/src/handlers/configHandler.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/synchronizer/src/handlers/configHandler.ts b/packages/synchronizer/src/handlers/configHandler.ts index 0c74e80e8..cabe658cf 100644 --- a/packages/synchronizer/src/handlers/configHandler.ts +++ b/packages/synchronizer/src/handlers/configHandler.ts @@ -26,7 +26,7 @@ export async function fetchOriginConfig(origin: string) { } try { - const configUrl = normalizeUrl(`${origin}/config.js`); + const configUrl = normalize(`${origin}/config.js`); const response = await fetch(configUrl); const responseText = await response.text(); @@ -41,7 +41,7 @@ export async function fetchOriginConfig(origin: string) { ); if (values) { - values.origin = normalizeUrl(origin); + values.origin = normalize(origin); values.apiOrigin = values.API_ORIGIN; values.authOrigin = values.OIDC_DISCOVERY_URL; values.schemasOrigin = values.SCHEMA_BASE_URL; @@ -59,3 +59,10 @@ export async function fetchOriginConfig(origin: string) { throw error; } } + +function normalize(url: string) { + return normalizeUrl(url, { + defaultProtocol: 'https:', + normalizeProtocol: true, + }); +} From 950529d6ad2f822f16a7467f6ba4229deb7f8d1f Mon Sep 17 00:00:00 2001 From: f1ames Date: Fri, 1 Dec 2023 10:56:44 +0100 Subject: [PATCH 2/4] fix(synchronizer): check origin config response status --- packages/synchronizer/src/handlers/configHandler.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/synchronizer/src/handlers/configHandler.ts b/packages/synchronizer/src/handlers/configHandler.ts index cabe658cf..4606239a5 100644 --- a/packages/synchronizer/src/handlers/configHandler.ts +++ b/packages/synchronizer/src/handlers/configHandler.ts @@ -28,6 +28,11 @@ export async function fetchOriginConfig(origin: string) { try { const configUrl = normalize(`${origin}/config.js`); const response = await fetch(configUrl); + + if (!response.ok) { + throw new Error(`Failed to fetch config from ${configUrl} with status ${response.status}: ${response.statusText}`); + } + const responseText = await response.text(); const values = Array.from(responseText.matchAll(/([A-Z_]+)\s*:\s*"(.*?)"/gm)).reduce( From cdfe62db4be0f6158a2f7769a2916296efcf6a62 Mon Sep 17 00:00:00 2001 From: f1ames Date: Fri, 1 Dec 2023 10:57:47 +0100 Subject: [PATCH 3/4] chore: add changeset --- .changeset/polite-rockets-train.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/polite-rockets-train.md diff --git a/.changeset/polite-rockets-train.md b/.changeset/polite-rockets-train.md new file mode 100644 index 000000000..6039a5411 --- /dev/null +++ b/.changeset/polite-rockets-train.md @@ -0,0 +1,5 @@ +--- +"@monokle/synchronizer": patch +--- + +Imporve origin config fetching by assuring response status and using https as default proto From f9780f9883df3a53b76b7f17b270cdb08f49b8a1 Mon Sep 17 00:00:00 2001 From: f1ames Date: Fri, 1 Dec 2023 11:02:24 +0100 Subject: [PATCH 4/4] test: fix test with explicit URL --- packages/synchronizer/src/__tests__/fetcher.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/synchronizer/src/__tests__/fetcher.spec.ts b/packages/synchronizer/src/__tests__/fetcher.spec.ts index cc3880937..c8267cd80 100644 --- a/packages/synchronizer/src/__tests__/fetcher.spec.ts +++ b/packages/synchronizer/src/__tests__/fetcher.spec.ts @@ -143,7 +143,7 @@ describe('Fetcher Tests', () => { const server = app.listen(13000, async () => { try { - const originData = await fetchOriginConfig('localhost:13000'); + const originData = await fetchOriginConfig('http://localhost:13000'); assert.equal(originData?.origin, 'http://localhost:13000'); assert.equal(originData?.apiOrigin, 'https://api.monokle.local');