Commit b2c5ae5b authored by Aryan Noshirvnai's avatar Aryan Noshirvnai

Fix geolocation not working: dev server was on an insecure origin

Root cause of "won't ask for location permissions": the Geolocation
API refuses to run at all on a plain http:// origin (only https://
and localhost count as a "secure context") - Chrome fails immediately
with PERMISSION_DENIED before ever showing the actual permission
prompt, which is indistinguishable from a real user decline. The dev
webapp was served at http://144.76.210.145:5173, so this affected
every visitor, not just one browser/device.

Fixed by serving the dev server over https:// via a self-signed cert
(@vitejs/plugin-basic-ssl, zero config). You'll see a one-time "your
connection isn't private" warning in the browser the first time you
load it now - click through (Advanced -> Proceed) since it's a
self-signed cert; after that geolocation works normally. A real
production deploy will need a proper cert via an actual domain name
(Let's Encrypt or similar) - self-signed is only acceptable here
because we're the ones clicking through our own warning.

Also fixed a second, smaller bug found while diagnosing this: the
insecure-origin failure was being shown to users as "location access
was denied - allow it in your browser settings," which is wrong and
unactionable (no browser setting fixes an insecure origin). Nearby.tsx
now checks window.isSecureContext upfront and shows an accurate
message instead of letting it fail through as a fake permission
denial.

Verified live: confirmed the exact failure via a real (non-Playwright-
granted) browser session hitting the old http:// URL - isSecureContext
false, getCurrentPosition immediately erroring with "Only secure
origins are allowed" - then confirmed it resolves cleanly over the
new https:// URL (isSecureContext true, real geolocation result).
1 new test for the insecure-origin message; 84 webapp + 144 backend
tests passing overall.
Co-Authored-By: 's avatarClaude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019i3NAUkLkHNbsPg5vKfzF9
parent 543f0d38
Pipeline #1058 canceled with stage
......@@ -24,6 +24,7 @@
"@types/node": "^24.13.3",
"@types/react": "^19.2.17",
"@types/react-dom": "^19.2.3",
"@vitejs/plugin-basic-ssl": "^2.3.0",
"@vitejs/plugin-react": "^6.0.4",
"jsdom": "^29.1.1",
"oxlint": "^1.75.0",
......@@ -1648,6 +1649,19 @@
"@types/react": "^19.2.0"
}
},
"node_modules/@vitejs/plugin-basic-ssl": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/@vitejs/plugin-basic-ssl/-/plugin-basic-ssl-2.3.0.tgz",
"integrity": "sha512-bdyo8rB3NnQbikdMpHaML9Z1OZPBu6fFOBo+OtxsBlvMJtysWskmBcnbIDhUqgC8tcxNv/a+BcV5U+2nQMm1OQ==",
"dev": true,
"license": "MIT",
"engines": {
"node": "^18.0.0 || ^20.0.0 || >=22.0.0"
},
"peerDependencies": {
"vite": "^6.0.0 || ^7.0.0 || ^8.0.0"
}
},
"node_modules/@vitejs/plugin-react": {
"version": "6.0.5",
"resolved": "https://registry.npmjs.org/@vitejs/plugin-react/-/plugin-react-6.0.5.tgz",
......
......@@ -28,6 +28,7 @@
"@types/node": "^24.13.3",
"@types/react": "^19.2.17",
"@types/react-dom": "^19.2.3",
"@vitejs/plugin-basic-ssl": "^2.3.0",
"@vitejs/plugin-react": "^6.0.4",
"jsdom": "^29.1.1",
"oxlint": "^1.75.0",
......
......@@ -70,6 +70,10 @@ describe('Nearby', () => {
beforeEach(() => {
vi.stubEnv('VITE_DEV_ACCESS_TOKEN', '')
vi.stubEnv('VITE_DEV_REFRESH_TOKEN', '')
// jsdom's default test origin doesn't count as a secure context, unlike the real
// https:// dev URL this page actually runs on - stub it so these tests exercise the
// geolocation flow itself; the insecure-origin guard gets its own dedicated test below.
vi.stubGlobal('isSecureContext', true)
// Safety net so tests that don't care about proposals don't issue a real, unmocked fetch -
// proposal-specific behavior gets its own dedicated tests.
vi.spyOn(soundmarksApi, 'getProposals').mockResolvedValue({ status: 'success', proposals: [] })
......@@ -249,4 +253,17 @@ describe('Nearby', () => {
await waitFor(() => expect(propose).toHaveBeenCalledWith(FOUND_MARK.id, 55))
})
it('shows a clear, accurate message on an insecure origin instead of a misleading "permission denied"', async () => {
vi.stubGlobal('isSecureContext', false)
const geolocation = mockGeolocation({ succeed: true })
renderNearby()
await userEvent.click(screen.getByRole('button', { name: 'Find soundmarks near me' }))
expect(await screen.findByText(/requires a secure \(https:\/\/\) connection/)).toBeInTheDocument()
// Never even attempted - the whole point is failing fast with an accurate reason instead
// of letting the browser reject it with the same code a real user decline would produce.
expect(geolocation.getCurrentPosition).not.toHaveBeenCalled()
})
})
......@@ -69,6 +69,15 @@ export function Nearby() {
setError("Your browser doesn't support location services.")
return
}
// The Geolocation API refuses to run at all on a plain http:// origin (only https:// and
// localhost qualify as a "secure context") - it fails with the same PERMISSION_DENIED code
// as a real user decline, so without this check we'd tell people to "check your browser
// settings" for something no setting can fix.
if (!window.isSecureContext) {
setStatus('error')
setError('Location access requires a secure (https://) connection - this page was loaded over an insecure one.')
return
}
setStatus('locating')
setError(null)
// getCurrentPosition for a fast first result, watchPosition to keep updating as you move -
......
......@@ -2,10 +2,17 @@
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import tailwindcss from '@tailwindcss/vite'
import basicSsl from '@vitejs/plugin-basic-ssl'
// https://vite.dev/config/
export default defineConfig({
plugins: [react(), tailwindcss()],
// The Geolocation API (and other browser APIs) refuse to run at all on a plain http://
// origin that isn't localhost - "secure context" is a hard browser requirement, not a
// permission you can grant around. basicSsl generates a self-signed cert automatically so
// dev can run over https:// (production still needs a real cert via a real domain - a
// self-signed one is only acceptable because we're the ones clicking through the browser's
// one-time warning ourselves).
plugins: [react(), tailwindcss(), basicSsl()],
server: {
host: true,
port: 5173,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment