Skip to content

Commit

Permalink
Merge pull request #404 from cloudflare/sven/fix-auth-relative-url
Browse files Browse the repository at this point in the history
handle relative URL on first-login
  • Loading branch information
xtuc committed Mar 27, 2023
2 parents 307800f + 3201e4a commit 42d2329
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
25 changes: 25 additions & 0 deletions backend/test/mastodon/oauth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,31 @@ describe('Mastodon APIs', () => {
assert((await getSigningKey(userKEK, db, actor as Actor)) instanceof CryptoKey)
})

test('first login redirect relative URLs', async () => {
const db = await makeDB()

const params = new URLSearchParams({
redirect_uri: '/a',
})

const formData = new FormData()
formData.set('username', 'username')
formData.set('name', 'name')

const req = new Request('https://example.com/first-login?' + params, {
method: 'POST',
body: formData,
headers: {
cookie: `CF_Authorization=${TEST_JWT}`,
},
})
const res = await first_login.handlePostRequest(req, db, userKEK, accessDomain, accessAud)
assert.equal(res.status, 302)

const location = res.headers.get('location')
assert.equal(location, 'https://example.com/a')
})

test('token error on unknown client', async () => {
const db = await makeDB()
const body = new URLSearchParams({ code: 'some-code' })
Expand Down
6 changes: 5 additions & 1 deletion functions/first-login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ export async function handlePostRequest(
return new Response('', { status: 400 })
}

const redirect_uri = decodeURIComponent(url.searchParams.get('redirect_uri') || '')
let redirect_uri = decodeURIComponent(url.searchParams.get('redirect_uri') || '')
if (redirect_uri.startsWith('/')) {
// URL is a relative URL, prepend the domain to it.
redirect_uri = 'https://' + url.hostname + redirect_uri
}
return Response.redirect(redirect_uri, 302)
}

0 comments on commit 42d2329

Please sign in to comment.