Skip to content

The agent that grows with you

desktop: validate the feed base baked into app-update.yml

ethernetb8c64260d867
diff --git a/apps/desktop/electron-builder.config.cjs b/apps/desktop/electron-builder.config.cjs
index 3e4e6a3d7e9..9eac889a38c 100644
--- a/apps/desktop/electron-builder.config.cjs
+++ b/apps/desktop/electron-builder.config.cjs
@@ -64,7 +64,7 @@ if (!/^\d+\.\d+\.\d+$/.test(electronVersion)) {
}
const macFeed = channelRequest ? null : feedContract.darwinFeed(channel === 'canary' || channel === 'light-canary' ? 'canary' : 'stable', light)
-const publicUrl = process.env.CLOUDFLARE_R2_PUBLIC_URL?.replace(/\/+$/, '')
+const publicUrl = feedContract.feedBaseUrl(process.env.CLOUDFLARE_R2_PUBLIC_URL)
/** @satisfies {Configuration} */
module.exports = {
@@ -93,8 +93,8 @@ module.exports = {
publish: channelRequest ? null : !channel
? null
: [
- process.env.CLOUDFLARE_R2_PUBLIC_URL
- ? { provider: 'generic', url: process.env.CLOUDFLARE_R2_PUBLIC_URL.replace(/\/+$/, ''), channel }
+ publicUrl
+ ? { provider: 'generic', url: publicUrl, channel }
: { provider: 'github', owner, repo, channel }
],
extraMetadata: {
diff --git a/apps/desktop/update-feed.cjs b/apps/desktop/update-feed.cjs
index ff829905858..ee693be1f38 100644
--- a/apps/desktop/update-feed.cjs
+++ b/apps/desktop/update-feed.cjs
@@ -15,4 +15,26 @@ function darwinFeed(channel, light = false) {
}
}
-module.exports = { darwinFeed }
+// The feed origin is baked into app-update.yml and trusted by every
+// installed client for the life of the build, so the CI var must be a
+// canonical https base: no credentials, query, or fragment, spelled exactly
+// as the URL parser re-serializes it (a trailing slash is tolerated, as the
+// Python release scripts do). Anything else is a misconfigured release
+// environment and must fail the build, not ship.
+/** @param {string | undefined} raw @returns {string | undefined} */
+function feedBaseUrl(raw) {
+ if (raw === undefined || raw === '') return undefined
+ let url
+ try {
+ url = new URL(raw)
+ } catch {
+ throw new TypeError(`CLOUDFLARE_R2_PUBLIC_URL is not a URL: ${raw}`)
+ }
+ const canonical = url.origin + url.pathname.replace(/\/+$/, '')
+ if (url.protocol !== 'https:' || url.username || url.password || url.search || url.hash || canonical !== raw.replace(/\/+$/, '')) {
+ throw new TypeError(`CLOUDFLARE_R2_PUBLIC_URL must be a canonical https base (got ${raw}, expected ${canonical})`)
+ }
+ return canonical
+}
+
+module.exports = { darwinFeed, feedBaseUrl }
diff --git a/tests-js/macos-publish-config.test.mjs b/tests-js/macos-publish-config.test.mjs
index b546377e145..66454f7b992 100644
--- a/tests-js/macos-publish-config.test.mjs
+++ b/tests-js/macos-publish-config.test.mjs
@@ -22,3 +22,18 @@ it.each([
expect(config.targets).toContain('zip')
expect(config.notarize).toBe(false)
})
+
+it('bakes only a canonical https feed base into the updater config', () => {
+ expect(feedContract.feedBaseUrl(undefined)).toBeUndefined()
+ expect(feedContract.feedBaseUrl('https://updates.example/releases/')).toBe('https://updates.example/releases')
+ for (const raw of ['http://updates.example', 'https://user:[email protected]', 'https://updates.example/?x=1',
+ 'https://updates.example/#frag', 'https://Updates.Example', 'updates.example', 'https://updates.example/a b']) {
+ expect(() => feedContract.feedBaseUrl(raw)).toThrow(TypeError)
+ }
+ const build = (url) => execFileSync(process.execPath, ['-e', "const c=require('./apps/desktop/electron-builder.config.cjs'); console.log(JSON.stringify(c.publish))"], {
+ cwd: root, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'],
+ env: { ...process.env, HERMES_DESKTOP_VARIANT: 'bundled', HERMES_PAYLOAD_TAG: 'v0.28.0', CLOUDFLARE_R2_PUBLIC_URL: url }
+ })
+ expect(JSON.parse(build('https://updates.example/'))).toEqual([{ provider: 'generic', url: 'https://updates.example', channel: 'latest' }])
+ expect(() => build('http://updates.example')).toThrow(/canonical https/)
+})