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

Use X-Forwarded-Host as url_root when present #799

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from app.models.endpoint import Endpoint
from app.request import Request, TorError
from app.utils.bangs import resolve_bang
from app.utils.misc import get_proxy_host_url
from app.filter import Filter
from app.utils.misc import read_config_bool, get_client_ip, get_request_url, \
check_for_update
Expand Down Expand Up @@ -144,10 +145,13 @@ def before_request_func():
if (not Endpoint.autocomplete.in_path(request.path) and
not Endpoint.healthz.in_path(request.path) and
not Endpoint.opensearch.in_path(request.path)):
# reconstruct url if X-Forwarded-Host header present
request_url = get_proxy_host_url(request,
get_request_url(request.url))
return redirect(url_for(
'session_check',
session_id=session['uuid'],
follow=get_request_url(request.url)), code=307)
follow=request_url), code=307)
else:
g.user_config = Config(**session['config'])
elif 'cookies_disabled' not in request.args:
Expand Down
9 changes: 9 additions & 0 deletions app/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ def get_request_url(url: str) -> str:
return url


def get_proxy_host_url(r: Request, default: str) -> str:
scheme = r.headers.get('X-Forwarded-Proto', 'http')
http_host = r.headers.get('X-Forwarded-Host')
if http_host:
return f'{scheme}://{http_host}/'

return default


def check_for_update(version_url: str, current: str) -> int:
# Check for the latest version of Whoogle
try:
Expand Down
5 changes: 4 additions & 1 deletion app/utils/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from app.filter import Filter
from app.request import gen_query
from app.utils.misc import get_proxy_host_url
from app.utils.results import get_first_link
from bs4 import BeautifulSoup as bsoup
from cryptography.fernet import Fernet, InvalidToken
Expand Down Expand Up @@ -115,9 +116,11 @@ def generate_response(self) -> str:

"""
mobile = 'Android' in self.user_agent or 'iPhone' in self.user_agent
# reconstruct url if X-Forwarded-Host header present
root_url = get_proxy_host_url(self.request, self.request.url_root)

content_filter = Filter(self.session_key,
root_url=self.request.url_root,
root_url=root_url,
mobile=mobile,
config=self.config,
query=self.query)
Expand Down