|
301 | 301 | "server = JupyUvi(app, port=port)"
|
302 | 302 | ]
|
303 | 303 | },
|
| 304 | + { |
| 305 | + "cell_type": "code", |
| 306 | + "execution_count": null, |
| 307 | + "id": "551454f9", |
| 308 | + "metadata": {}, |
| 309 | + "outputs": [], |
| 310 | + "source": [ |
| 311 | + "#|export\n", |
| 312 | + "def get_host(request):\n", |
| 313 | + " \"\"\"Get the host, preferring X-Forwarded-Host if available\"\"\"\n", |
| 314 | + " forwarded_host = request.headers.get('x-forwarded-host')\n", |
| 315 | + " return forwarded_host if forwarded_host else request.url.netloc" |
| 316 | + ] |
| 317 | + }, |
| 318 | + { |
| 319 | + "cell_type": "code", |
| 320 | + "execution_count": null, |
| 321 | + "id": "3cc9b978", |
| 322 | + "metadata": {}, |
| 323 | + "outputs": [ |
| 324 | + { |
| 325 | + "name": "stdout", |
| 326 | + "output_type": "stream", |
| 327 | + "text": [ |
| 328 | + "Without X-Forwarded-Host: localhost:8000\n", |
| 329 | + "With X-Forwarded-Host: example.com\n" |
| 330 | + ] |
| 331 | + } |
| 332 | + ], |
| 333 | + "source": [ |
| 334 | + "from types import SimpleNamespace\n", |
| 335 | + "from urllib.parse import urlparse\n", |
| 336 | + "\n", |
| 337 | + "mock_request_localhost = SimpleNamespace(headers={}, url=SimpleNamespace(netloc='localhost:8000'))\n", |
| 338 | + "mock_request_with_forward = SimpleNamespace(\n", |
| 339 | + " headers={'x-forwarded-host': 'example.com'}, \n", |
| 340 | + " url=SimpleNamespace(netloc='localhost:8000', hostname='localhost')\n", |
| 341 | + ")\n", |
| 342 | + "\n", |
| 343 | + "print(\"Without X-Forwarded-Host:\", get_host(mock_request_localhost))\n", |
| 344 | + "print(\"With X-Forwarded-Host:\", get_host(mock_request_with_forward))" |
| 345 | + ] |
| 346 | + }, |
304 | 347 | {
|
305 | 348 | "cell_type": "code",
|
306 | 349 | "execution_count": null,
|
|
311 | 354 | "#| export\n",
|
312 | 355 | "def redir_url(request, redir_path, scheme=None):\n",
|
313 | 356 | " \"Get the redir url for the host in `request`\"\n",
|
314 |
| - " scheme = 'http' if request.url.hostname in (\"localhost\", \"127.0.0.1\") else 'https'\n", |
315 |
| - " return f\"{scheme}://{request.url.netloc}{redir_path}\"" |
| 357 | + " host = get_host(request)\n", |
| 358 | + " scheme = 'http' if host.split(':')[0] in (\"localhost\", \"127.0.0.1\") else 'https'\n", |
| 359 | + " return f\"{scheme}://{host}{redir_path}\"" |
| 360 | + ] |
| 361 | + }, |
| 362 | + { |
| 363 | + "cell_type": "code", |
| 364 | + "execution_count": null, |
| 365 | + "id": "fee2db6c", |
| 366 | + "metadata": {}, |
| 367 | + "outputs": [ |
| 368 | + { |
| 369 | + "name": "stdout", |
| 370 | + "output_type": "stream", |
| 371 | + "text": [ |
| 372 | + "Localhost: http://localhost:8000/redirect\n", |
| 373 | + "With X-Forwarded-Host: https://example.com/redirect\n", |
| 374 | + "Production: https://myapp.com/redirect\n" |
| 375 | + ] |
| 376 | + } |
| 377 | + ], |
| 378 | + "source": [ |
| 379 | + "from types import SimpleNamespace\n", |
| 380 | + "from urllib.parse import urlparse\n", |
| 381 | + "\n", |
| 382 | + "mock_request_prod = SimpleNamespace(headers={}, url=SimpleNamespace(netloc='myapp.com', hostname='myapp.com'))\n", |
| 383 | + "\n", |
| 384 | + "print(\"Localhost:\", redir_url(mock_request_localhost, '/redirect'))\n", |
| 385 | + "print(\"With X-Forwarded-Host:\", redir_url(mock_request_with_forward, '/redirect'))\n", |
| 386 | + "print(\"Production:\", redir_url(mock_request_prod, '/redirect'))" |
316 | 387 | ]
|
317 | 388 | },
|
318 | 389 | {
|
|
447 | 518 | "source": [
|
448 | 519 | "#| export\n",
|
449 | 520 | "http_patterns = (r'^(localhost|127\\.0\\.0\\.1)(:\\d+)?$',)\n",
|
450 |
| - "def url_match(url, patterns=http_patterns):\n", |
451 |
| - " return any(re.match(pattern, url.netloc.split(':')[0]) for pattern in patterns)" |
| 521 | + "def url_match(request, patterns=http_patterns):\n", |
| 522 | + " return any(re.match(pattern, get_host(request).split(':')[0]) for pattern in patterns)" |
452 | 523 | ]
|
453 | 524 | },
|
454 | 525 | {
|
|
475 | 546 | " def redirect(req, session, code:str=None, error:str=None, state:str=None):\n",
|
476 | 547 | " if not code: session['oauth_error']=error; return RedirectResponse(self.error_path, status_code=303)\n",
|
477 | 548 | " scheme = 'http' if url_match(req.url,self.http_patterns) or not self.https else 'https'\n",
|
478 |
| - " base_url = f\"{scheme}://{req.url.netloc}\"\n", |
| 549 | + " base_url = f\"{scheme}://{get_host(req)}\"\n", |
479 | 550 | " info = AttrDictDefault(cli.retr_info(code, base_url+redir_path))\n",
|
480 | 551 | " ident = info.get(self.cli.id_key)\n",
|
481 | 552 | " if not ident: return self.redir_login(session)\n",
|
|
0 commit comments