Codeberg/Community
54
324
Fork
You've already forked Community
12

Missing CORS prevents OIDC Authorization Code Flow with PKCE for applications #2278

Closed
opened 2025年12月30日 09:19:47 +01:00 by silwol · 4 comments

Comment

I'm implementing a single page app that will run in the browser. The development happens on codeberg, and we will host our own instance of the app. Authentication happens through OIDC in an Authorization Code Flow with PKCE.

The authorization procedure appears to work as expected, e.g. when using GitLab as an OIDC provider. The plan is to use Codeberg as an OIDC provider for our own instance of the app though (because that's where the project lives anyway), and there the request from the browser to the https://codeberg.org/login/oauth/access_token POST endpoint fails with Reason: CORS Missing Allow Origin.

I set up a local Forgejo instance with very permissive CORS headers for testing (in this case a reverse proxy hands out the CORS headers), and the access token fetch works well there as well.

Not sure where exactly the CORS headers for the codeberg.org instance are added, whether it's Forgejo itself or the reverse proxy. I hope my use case is not too unusual, but still I would be surprised if I'm the first person to be affected by this. Please let me know if this is something that should be addressed directly to the Forgejo project. At a first glance this appears to be related to configuration more than the Forgejo implementation, because e.g. on https://v14.next.forgejo.org the browser already prevents access to the /.well-known/openid-configuration GET endpoint due to missing CORS headers.

### Comment I'm implementing a single page app that will run in the browser. The development [happens on codeberg](https://codeberg.org/pib), and we will host our own instance of the app. Authentication happens through OIDC in an Authorization Code Flow with PKCE. The authorization procedure appears to work as expected, e.g. when using GitLab as an OIDC provider. The plan is to use Codeberg as an OIDC provider for our own instance of the app though (because that's where the project lives anyway), and there the request from the browser to the `https://codeberg.org/login/oauth/access_token` **POST** endpoint fails with **Reason: CORS Missing Allow Origin**. I set up a local Forgejo instance with very permissive CORS headers for testing (in this case a reverse proxy hands out the CORS headers), and the access token fetch works well there as well. Not sure where exactly the CORS headers for the codeberg.org instance are added, whether it's Forgejo itself or the reverse proxy. I hope my use case is not too unusual, but still I would be surprised if I'm the first person to be affected by this. Please let me know if this is something that should be addressed directly to the Forgejo project. At a first glance this appears to be related to configuration more than the Forgejo implementation, because e.g. on <https://v14.next.forgejo.org> the browser already prevents access to the `/.well-known/openid-configuration` **GET** endpoint due to missing CORS headers.
fetch("https://codeberg.org/login/oauth/access_token", {method: 'POST'});

In the browser console just works. I would need more information from you on how you are doing these requests to properly help you, but CORS is definitely enabled for this endpoint.

```js fetch("https://codeberg.org/login/oauth/access_token", {method: 'POST'}); ``` In the browser console just works. I would need more information from you on how you are doing these requests to properly help you, but CORS is definitely enabled for this endpoint.
Author
Copy link

Thanks for looking into this!

Attempting to build a reproducer, I discovered that the POST endpoint works just fine when sent with the code in your example.

I found a reproducer though, it shows that the failure happens when the browser sends a pre-flight CORS request with the OPTIONS method.

It appears that this pre-flight request is triggered if requests have certain properties (in this case, it appears to be the Authorization header).

Reproducer:

fetch("https://codeberg.org/login/oauth/access_token", { method: 'POST', headers: { "Authorization": "Bearer abcdef" }});
Thanks for looking into this! Attempting to build a reproducer, I discovered that the `POST` endpoint works just fine when sent with the code in your example. I found a reproducer though, it shows that the failure happens when the browser sends a [pre-flight CORS request with the `OPTIONS` method](https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request). It appears that this pre-flight request is triggered if requests have [certain properties](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CORS#preflighted_requests) (in this case, it appears to be the `Authorization` header). Reproducer: ```js fetch("https://codeberg.org/login/oauth/access_token", { method: 'POST', headers: { "Authorization": "Bearer abcdef" }}); ```

And in your code you set This Authorization? If so, why? This will indeed not work.

And in your code you set This Authorization? If so, why? This will indeed not work.
Author
Copy link

Well, I didn't intentionally, but it looks like the OIDC library I'm using does, and I didn't question that... 🤦

A quick test injecting a client into the request function that strips out the Authorization header shows that the authentication flow works just fine and does not trigger the pre-flight request. Time to look for a clean solution to that.

Thanks a lot for your patience, your support, and finally for the Codeberg service!

Closing this issue.

Well, I didn't intentionally, but it looks like the OIDC library I'm using does, and I didn't question that... 🤦 A quick test injecting a client into the request function that strips out the `Authorization` header shows that the authentication flow works just fine and does not trigger the pre-flight request. Time to look for a clean solution to that. Thanks a lot for your patience, your support, and finally for the Codeberg service! Closing this issue.
Sign in to join this conversation.
No Branch/Tag specified
main
No results found.
Labels
Clear labels
accessibility

Reduces accessibility and is thus a "bug" for certain user groups on Codeberg.
bug

Something is not working the way it should. Does not concern outages.
bug
infrastructure

Errors evidently caused by infrastructure malfunctions or outages
Codeberg

This issue involves Codeberg's downstream modifications and settings and/or Codeberg's structures.
contributions welcome

Please join the discussion and consider contributing a PR!
docs

No bug, but an improvement to the docs or UI description will help
duplicate

This issue or pull request already exists
enhancement

New feature
infrastructure

Involves changes to the server setups, use `bug/infrastructure` for infrastructure-related user errors.
legal

An issue directly involving legal compliance
licence / ToS

involving questions about the ToS, especially licencing compliance
please chill
we are volunteers

Please consider editing your posts and remember that there is a human on the other side. We get that you are frustrated, but it's harder for us to help you this way.
public relations

Things related to Codeberg's external communication
question

More information is needed
question
user support

This issue contains a clearly stated problem. However, it is not clear whether we have to fix anything on Codeberg's end, but we're helping them fix it and/or find the cause.
s/Forgejo

Related to Forgejo. Please also check Forgejo's issue tracker.
s/Forgejo/migration

Migration related issues in Forgejo
s/Pages

Issues related to the Codeberg Pages feature
s/Weblate

Issue is related to the Weblate instance at https://translate.codeberg.org
s/Woodpecker

Woodpecker CI related issue
security

involves improvements to the sites security
service

Add a new service to the Codeberg ecosystem (instead of implementing into Gitea)
upstream

An open issue or pull request to an upstream repository to fix this issue (partially or completely) exists (i.e. Gitea, Forgejo, etc.)
wontfix

Codeberg's current set of contributors are not planning to spend time on delegating this issue.
Milestone
Clear milestone
No items
No milestone
Projects
Clear projects
No items
No project
Assignees
Clear assignees
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Codeberg/Community#2278
Reference in a new issue
Codeberg/Community
No description provided.
Delete branch "%!s()"

Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?