Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi-domain support, ROOT_URL problem, use relative URLs in templates instead of absolute URLs #19345

Closed
wxiaoguang opened this issue Apr 8, 2022 · 20 comments · Fixed by #21986 or #22836
Labels
issue/confirmed Issue has been reviewed and confirmed to be present or accepted to be implemented topic/ui Change the appearance of the Gitea UI type/proposal The new feature has not been accepted yet but needs to be discussed first. type/refactoring Existing code has been cleaned up. There should be no new functionality.

Comments

@wxiaoguang
Copy link
Contributor

wxiaoguang commented Apr 8, 2022

At the moment, Gitea users must set ROOT_URL correctly in app.ini, otherwise many URLs on the web UI will be wrong, which makes the web UI doesn't work properly (CSRF tokens and cookies can not work across domains)

The ROOT_URL must be set to the full URL which the users visit, with correct scheme(eg: https://the-gitea-domain/) and correct sub path if it is used (eg: https://the-gitea-domain/my-gitea-path/).

New code should always use relative URLs for Web UI.


Many maintainers have answered the issues about incorrect ROOT_URL again and again.

Using HTMLURL(and other absolute URLs) in templates is the root case that Gitea can only serve web under ROOT_URL.

That's why we have to force users to set ROOT_URL correctly.

If all URLs in template can be refactored to use Relative URL correctly, then we do not need to force users set ROOT_URL correctly again and again.

The ROOT_URL is still needed because for example, some webhooks need the absolute URL for external systems, and Gitea should show absolute URL on the UI for clone link, etc.

Related:

Some more related:

The PR to detect the problem:

@wxiaoguang wxiaoguang added type/proposal The new feature has not been accepted yet but needs to be discussed first. type/refactoring Existing code has been cleaned up. There should be no new functionality. labels Apr 8, 2022
@6543 6543 added the topic/ui Change the appearance of the Gitea UI label Apr 8, 2022
@silverwind
Copy link
Member

silverwind commented Apr 8, 2022

+1, relative URLs may even enable use cases like running on multiple domains. Thought IIRC, ROOT_URL still needs to stay in the config because sometimes absolute URLs need to be built, like for PR link when pushing via SSH. But it's good nonetheless to remove dependencies on ROOT_URL where possible.

@lunny
Copy link
Member

lunny commented Apr 8, 2022

We need a new global variable ROOT_URL_PATH, it could be / or sub path like /gitea. For repository, we could have a new HTMLURLPath

@wxiaoguang wxiaoguang changed the title Use relative URLs in tempates, instead of absolute URLs Multidomain support, Use relative URLs in tempates, instead of absolute URLs Apr 14, 2022
@wxiaoguang wxiaoguang changed the title Multidomain support, Use relative URLs in tempates, instead of absolute URLs Multi-domain support, use relative URLs in tempates instead of absolute URLs Apr 14, 2022
@wxiaoguang wxiaoguang changed the title Multi-domain support, use relative URLs in tempates instead of absolute URLs Multi-domain support, use relative URLs in templates instead of absolute URLs Apr 20, 2022
@wxiaoguang wxiaoguang changed the title Multi-domain support, use relative URLs in templates instead of absolute URLs Multi-domain support, ROOT_URL problem, use relative URLs in templates instead of absolute URLs Jun 23, 2022
@silverwind
Copy link
Member

silverwind commented Jul 22, 2022

As seen in #20450, dashboard repo links are one example of incorrect absolute URLs.

@silverwind silverwind added the issue/confirmed Issue has been reviewed and confirmed to be present or accepted to be implemented label Jul 22, 2022
@k6Q28k5w5eWuRuuzTg9j7GrKCbjt7
Copy link

k6Q28k5w5eWuRuuzTg9j7GrKCbjt7 commented Aug 14, 2022

First time I have tried gitea and find it surprising that there is no mention of this at all on the docs. The docs give an impression that the user can set the environment, but this is incorrect. I'll give a subset of examples, there are several more:

Documentation: Install with docker
To bind the integrated OpenSSH daemon and the webserver on a different port, adjust the port section. It’s common to just change the host port and **keep the ports within the container like they are**.

Gitea explicitly overrides a lot of headers and links. The advice to differentiate the environment e.g. inside and outside a container, only makes sense if the application offers the configuration feature. E.g. docker mapping to port 3000, instead of 80 or 443, will make you really wonder what the source of the fault is then links dont work. At least by mimic'ing the env on standard ports, some subset will resolve properly.

Documentation: Managing deployments with environemt variables
In addition to the environment variables above, **any settings in app.ini can be set or overridden with an environment variable** of the form: GITEA__SECTION_NAME__KEY_NAME.

This is pure misinformation as the application never properly supported configuration in this manner. The docs seems to be describing a roadmap of how it should end up to be, rather than the factual.

Documentation: Recommended server configuration
NOTE: Many of the following directories can be configured using [Environment Variables](https://docs.gitea.io/en-us/environment-variables/) as well! Of note, configuring GITEA_WORK_DIR will tell Gitea where to base its working directory, as well as ease installation.

and

Documentation: reverse proxies

No, the application will override the proxy headers e.g. all of the repository links. This will break your env if you rely on the proxy with redirects URI also.

If you follow the docs you will end up with a broken system going in a loop until you find the source of the problem. Would it be possible to mention that this doesnt work on the docs? :)

@nwmcsween
Copy link

nwmcsween commented Aug 25, 2022

So what is the fix here? Refactor HTMLURL to return relative URLS and punt explicit absolute URLS to new config options like SSH? This seems to be an epic but all issues except one (but I think more is needed than JS here) is left open.

@lunny
Copy link
Member

lunny commented Sep 19, 2022

So I think we need an ALLOWED_ROOT_URLS, which could have a glob matching. When a request to a disallowed root url, Gitea should return 404 or redirect to 404.

I found it's difficult to get the right root url in the backend because maybe there are some reverse proxies. So it's only done by frontend.

But ROOT_URL cannot be removed because some places need that. i.e. When sending an email, the link needs an absolute url. We can reuse ROOT_URL as the default value of that.

@wxiaoguang
Copy link
Contributor Author

wxiaoguang commented Sep 19, 2022

I do not think so. No more option is needed. I do not agree with "when a request to a disallowed root url, Gitea should return 404 or redirect to 404.": it's not related to this problem, and it's never a real problem.

The proper solution is to refactor most HTMLURLs in templates to relative URLs.

And the global ROOT_URL is necessary and must, see the origin comment:

The ROOT_URL is still needed because for example, some webhooks need the absolute URL for external systems, and Gitea should show absolute URL on the UI for clone link, etc.

@wxiaoguang
Copy link
Contributor Author

wxiaoguang commented Sep 19, 2022

To explain why "mismatched ROOT_URL makes the web UI doesn't work properly": because the CSRF tokens and cookies can not work across origin. Users will be affected by: be redirected to a non-login site, fail to do ajax requests, fail to submit forms, etc. (I updated the origin comment)

I had a full plan about how to do the refactoring, but doing refactoring for Gitea is pretty difficult. Unless there are enough maintainers promising to support this refactoring, I do not have confidence to work on it.

@defkev
Copy link

defkev commented Sep 30, 2022

I just hit this warning after upgrading an instance to 1.17 (from 1.15?) with ROOT_URL set to its FQDN (e.g. http://git.domain.local:3000/)

Now most users on this network get a DNS-suffix (e.g. domain.local) provided by DHCP who access the frontend using just the hostname (http://git:3000) for convenience who now get the warning about the supposedly "wrong" ROOT_URL:

Your ROOT_URL in app.ini is http://git.domain.local:3000/ but you are visiting http://git:3000/
You should set ROOT_URL correctly, otherwise the web may not work correctly.

Which doesn't make a whole lot of sense to begin with:
a ) The network-stack will always resolve the hostname to its FQDN using the provided DNS-suffix so this doesn't affect CSRF
b) This should probably not be checked on the client-site (using e.g. window.location)

Now, as a workaround, i could pipe gitea through a reverse proxy to redirect git to git.domain.local pointing A to the reverse proxy to get rid of the warning but this would ultimately break ssh unless i also add an ssh proxy.

So either ignore the warning in this scenario (option in app.ini?) or add the redirect to the build in webserver shipping with gitea as this is only affecting the web part anyway.

Cheers

@wxiaoguang
Copy link
Contributor Author

wxiaoguang commented Sep 30, 2022

If you are sure you can ignore the warning, there is a bypass method to hide the warning, see the FAQ in #18971 .

There is a specialized CSS class "js-global-error" for the error message, then end users still have a chance to hide these error messages by customized CSS styles.


edited: ⚠️ please see the WARNING in other comments, do not do so ⚠️

@defkev
Copy link

defkev commented Sep 30, 2022

Thank you very much, i found the PR after posting the comment.

$CustomPath/public/css/header.css:

.js-global-error {
    display: none !important;
}

$CustomPath/templates/custom/header.tmpl:

<link rel="stylesheet" href="/assets/css/header.css">

Hacky but it works 😁


edited by wxiaoguang: ⚠️ please see the WARNING in other comments, do not do so ⚠️

@ddddavid-he

This comment was marked as outdated.

@wxiaoguang
Copy link
Contributor Author

wxiaoguang commented Oct 1, 2022

⚠️ WARNING about ignoring the "inconsistent domain" message

To be clear : if you see the error message but you ignore it, you will run into problem.

Please DO NOT do so before Gitea has a clear solution, unless that you are very very sure about what you are doing and you can resolve all your users' problems. That's why I didn't make an option or provide any detail steps for hiding it.

I will hide some unrelated comments and edit to put some warnings in some comments, because if many users ignore the warning, there will be a lot of duplicate issues here reporting that "something doesn't work" (it happened before).

For example, user who log in https://git/ can not do preview if the ROOT_URL is https://git.domain/. They can not do inline PR comment either, there could be more similar problems. You can only ignore the error message if you are sure your users won't use these affected features on different domains.

image

@ddddavid-he
Copy link

ddddavid-he commented Oct 1, 2022

Got it. Thanks for your very kind notice.

@badiku
Copy link

badiku commented Nov 7, 2022

hi, how to force bind to only one domain name?

e.g. allow http://localhost but disable directly access by ip: http://127.0.0.1

if the ROOT_URL is https://git.domain/. how to forbid other users to visit it by https://git/ , if they both point to same IP address?

@lunny
Copy link
Member

lunny commented Nov 7, 2022

hi, how to force bind to only one domain name?

e.g. allow http://localhost but disable directly access by ip: http://127.0.0.1

if the ROOT_URL is https://git.domain/. how to forbid other users to visit it by https://git/ , if they both point to same IP address?

You can do that in reverseproxy.

@badiku
Copy link

badiku commented Nov 7, 2022

thank you .

I want to achieve this without another proxy service like nginx.

there's a setting Http_addr to bind IP, but not for bind host.

maybe I can modify template .tmpl page file to display blank page if detected ip

@scubbo
Copy link

scubbo commented Jan 4, 2023

Another possibly-related issue here - I'm trying to set up my instance so that it's accessible independently in two ways:

  • via gitea.local, where DNS resolution happens directly on my router and no external Internet access is required
  • via gitea.my-domain-name.org, where DNS resolution and request routing happen via the wider Internet

AFAICT, that's not possible for Docker registry operations since the response to docker login references settings.APPUrl which is derived from ROOT_URL - that is, an attempt to docker login to gitea.local will fail.

jolheiser pushed a commit that referenced this issue Feb 6, 2023
…ontains an absolute url (#21986)

partially fix #19345

This PR add some `Link` methods for different objects. The `Link`
methods are not different from `HTMLURL`, they are lack of the absolute
URL. And most of UI `HTMLURL` have been replaced to `Link` so that users
can visit them from a different domain or IP.

This PR also introduces a new javascript configuration
`window.config.reqAppUrl` which is different from `appUrl` which is
still an absolute url but the domain has been replaced to the current
requested domain.
@jolheiser
Copy link
Member

Re-opening as linked PR only partially resolved this issue

@jolheiser jolheiser reopened this Feb 6, 2023
@wxiaoguang
Copy link
Contributor Author

wxiaoguang commented Feb 7, 2023

Most problems in this issue have been addressed by #21986, so I think it's good to:

  1. Remove most ROOT_URL error messages on the page, only show it to site admin.
  2. Close this issue
  3. Open a new issue to follow remaining and new problems.

Now the "Multidomain support" is about 50%-70%.

  • A correct main domain (ROOT_URL) is still necessary, because Gitea sends mails/notifications with no knowledge of "current domain"
  • There are still incorrect usages of AppURL(ROOT_URL) in code, which should be cleaned one by one in the future.

techknowlogick pushed a commit that referenced this issue Feb 9, 2023
After some PRs:
* #21986
* #22795
* #22808
* #22831
* #22839

Users won't be affected by the ROOT_URL problem in most cases. Close
#19345

This PR improves AppUrl/ROOT_URL checking, only check it on the admin
page, and the message is also updated.

Feel free to suggest about more English-native messages.



![image](https://user-images.githubusercontent.com/2114189/217811809-7d44ddb7-2c4a-46d0-a5db-8ae6ee65f8c3.png)
@go-gitea go-gitea locked and limited conversation to collaborators May 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
issue/confirmed Issue has been reviewed and confirmed to be present or accepted to be implemented topic/ui Change the appearance of the Gitea UI type/proposal The new feature has not been accepted yet but needs to be discussed first. type/refactoring Existing code has been cleaned up. There should be no new functionality.
Projects
None yet