2022-11-29 23:50:32 +00:00
|
|
|
import secrets
|
|
|
|
|
2022-12-11 23:44:34 +00:00
|
|
|
from files.helpers.config.const import *
|
2022-11-15 09:19:08 +00:00
|
|
|
from files.helpers.settings import get_setting
|
|
|
|
from files.helpers.cloudflare import CLOUDFLARE_AVAILABLE
|
|
|
|
from files.routes.wrappers import *
|
2022-11-30 01:29:06 +00:00
|
|
|
from files.__main__ import app, limiter
|
2022-11-15 09:19:08 +00:00
|
|
|
|
2022-11-29 23:50:32 +00:00
|
|
|
def session_init():
|
|
|
|
if not session.get("session_id"):
|
|
|
|
session.permanent = True
|
|
|
|
session["session_id"] = secrets.token_hex(49)
|
|
|
|
|
2022-11-15 09:19:08 +00:00
|
|
|
@app.before_request
|
|
|
|
def before_request():
|
2022-12-27 01:22:39 +00:00
|
|
|
if request.host != SITE:
|
|
|
|
abort(403, "Unauthorized host provided!")
|
|
|
|
|
2022-11-15 09:19:08 +00:00
|
|
|
if SITE == 'marsey.world' and request.path != '/kofi':
|
|
|
|
abort(404)
|
|
|
|
|
2022-12-27 01:22:39 +00:00
|
|
|
if request.headers.get("CF-Worker"):
|
|
|
|
abort(403, "Cloudflare workers are not allowed to access this website.")
|
|
|
|
|
2022-11-30 01:31:28 +00:00
|
|
|
g.agent = request.headers.get("User-Agent", "")
|
2022-11-15 09:19:08 +00:00
|
|
|
if not g.agent and request.path != '/kofi':
|
2022-12-27 01:22:39 +00:00
|
|
|
abort(403, 'Please use a "User-Agent" header!')
|
2022-11-15 09:19:08 +00:00
|
|
|
|
2022-12-27 01:22:39 +00:00
|
|
|
if not get_setting('bots') and request.headers.get("Authorization"):
|
|
|
|
abort(403)
|
2022-12-05 14:25:25 +00:00
|
|
|
|
2022-12-27 01:22:39 +00:00
|
|
|
g.desires_auth = False
|
|
|
|
if not IS_LOCALHOST:
|
|
|
|
app.config["COOKIE_DOMAIN"] = f".{request.host}"
|
|
|
|
app.config["SESSION_COOKIE_DOMAIN"] = app.config["COOKIE_DOMAIN"]
|
2022-11-15 09:19:08 +00:00
|
|
|
|
2022-12-27 01:22:39 +00:00
|
|
|
ua = g.agent.lower()
|
2022-11-15 09:19:08 +00:00
|
|
|
|
2022-12-05 14:25:25 +00:00
|
|
|
if '; wv) ' in ua:
|
2022-12-04 19:02:22 +00:00
|
|
|
g.browser = 'webview'
|
2022-12-05 14:25:25 +00:00
|
|
|
elif ' firefox/' in ua:
|
2022-12-04 19:02:22 +00:00
|
|
|
g.browser = 'firefox'
|
2022-12-05 14:25:25 +00:00
|
|
|
elif 'iphone' in ua or 'ipad' in ua or 'ipod' in ua or 'mac os' in ua:
|
2022-12-04 19:02:22 +00:00
|
|
|
g.browser = 'apple'
|
|
|
|
else:
|
|
|
|
g.browser = 'chromium'
|
2022-11-20 23:31:26 +00:00
|
|
|
|
2022-11-15 09:19:08 +00:00
|
|
|
g.is_tor = request.headers.get("cf-ipcountry") == "T1"
|
|
|
|
|
|
|
|
request.path = request.path.rstrip('/')
|
|
|
|
if not request.path: request.path = '/'
|
|
|
|
request.full_path = request.full_path.rstrip('?').rstrip('/')
|
|
|
|
if not request.full_path: request.full_path = '/'
|
|
|
|
|
2022-11-27 01:01:02 +00:00
|
|
|
session_init()
|
2022-11-30 01:29:06 +00:00
|
|
|
limiter.check()
|
|
|
|
g.db = db_session()
|
2022-11-22 23:37:55 +00:00
|
|
|
|
2022-12-27 01:22:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
CSP = {
|
|
|
|
"upgrade-insecure-requests": "",
|
|
|
|
|
|
|
|
"default-src": "'none'",
|
|
|
|
"frame-ancestors": "'none'",
|
|
|
|
|
|
|
|
"form-action": "'self'",
|
|
|
|
"manifest-src": "'self'",
|
|
|
|
"worker-src": "'self'",
|
|
|
|
"base-uri": "'self'",
|
|
|
|
"font-src": "'self'",
|
|
|
|
|
2022-12-29 14:20:27 +00:00
|
|
|
"style-src-elem": "'self'",
|
2022-12-27 01:22:39 +00:00
|
|
|
"style-src-attr": "'unsafe-inline'",
|
|
|
|
"style-src": "'self' 'unsafe-inline'",
|
|
|
|
|
2022-12-29 14:20:27 +00:00
|
|
|
"script-src-elem": "'self' challenges.cloudflare.com",
|
2022-12-27 01:22:39 +00:00
|
|
|
"script-src-attr": "'unsafe-inline'",
|
|
|
|
"script-src": "'self' 'unsafe-inline' challenges.cloudflare.com",
|
|
|
|
|
2022-12-28 14:37:26 +00:00
|
|
|
"media-src": "https:",
|
2022-12-27 03:40:03 +00:00
|
|
|
"img-src": "https: data:",
|
2022-12-27 01:22:39 +00:00
|
|
|
"frame-src": "challenges.cloudflare.com www.youtube-nocookie.com platform.twitter.com",
|
|
|
|
"connect-src": "'self' tls-use1.fpapi.io api.fpjs.io",
|
|
|
|
|
|
|
|
"report-to": "csp",
|
|
|
|
"report-uri": "/csp_violations",
|
|
|
|
}
|
|
|
|
|
|
|
|
if IS_LOCALHOST:
|
|
|
|
CSP["style-src-elem"] += " rdrama.net"
|
|
|
|
CSP["script-src-elem"] += " rdrama.net"
|
|
|
|
CSP["img-src"] += " http:"
|
|
|
|
|
|
|
|
CSP_str = ''
|
|
|
|
|
|
|
|
for k, val in CSP.items():
|
|
|
|
CSP_str += f'{k} {val}; '
|
|
|
|
|
2022-11-15 09:19:08 +00:00
|
|
|
@app.after_request
|
band-aid fix for frozen session issue on signup (#50)
through some reason or another, people are somehow getting cookies that aren't prepended with a dot.
this is a problem because both sessions at, as best as I can tell, mix so it tries to read from a different cookie than we write to. this essentially "freezes" the session in place. users are unable to login, logout, signup, toggle poor mode, toggle NSFW, etc.
~~this attempts to delete bad session cookies (i.e. cookies with a domain that don't start with a dot).~~
~~we don't do this on "dotless" domains (and by extension localhost) because browser support for setting cookies on FQDNs that only have one dot has tenuous support among browsers anyway).~~
~~this *may* log some people out, but... their days of being able to do stuff on the site were numbered anyway.~~
**edit: as amazing as this thought was, browsers just wipe the entire cookies completely and there's no way to specifically target dotless cookies. for an issue that affects a few users, better to just tell them to clear their cookies. if *this* doesn't work, delete service-worker.js and be done with the whole service worker crap. forever. permanently. this PR also includes some QOL improvements.**
Co-authored-by: justcool393 <justcool393@gmail.com>
Reviewed-on: https://fsdfsd.net/rDrama/rDrama/pulls/50
Co-authored-by: justcool393 <justcool393@noreply.fsdfsd.net>
Co-committed-by: justcool393 <justcool393@noreply.fsdfsd.net>
2022-12-06 22:07:12 +00:00
|
|
|
def after_request(response:Response):
|
2022-11-15 09:19:08 +00:00
|
|
|
if response.status_code < 400:
|
band-aid fix for frozen session issue on signup (#50)
through some reason or another, people are somehow getting cookies that aren't prepended with a dot.
this is a problem because both sessions at, as best as I can tell, mix so it tries to read from a different cookie than we write to. this essentially "freezes" the session in place. users are unable to login, logout, signup, toggle poor mode, toggle NSFW, etc.
~~this attempts to delete bad session cookies (i.e. cookies with a domain that don't start with a dot).~~
~~we don't do this on "dotless" domains (and by extension localhost) because browser support for setting cookies on FQDNs that only have one dot has tenuous support among browsers anyway).~~
~~this *may* log some people out, but... their days of being able to do stuff on the site were numbered anyway.~~
**edit: as amazing as this thought was, browsers just wipe the entire cookies completely and there's no way to specifically target dotless cookies. for an issue that affects a few users, better to just tell them to clear their cookies. if *this* doesn't work, delete service-worker.js and be done with the whole service worker crap. forever. permanently. this PR also includes some QOL improvements.**
Co-authored-by: justcool393 <justcool393@gmail.com>
Reviewed-on: https://fsdfsd.net/rDrama/rDrama/pulls/50
Co-authored-by: justcool393 <justcool393@noreply.fsdfsd.net>
Co-committed-by: justcool393 <justcool393@noreply.fsdfsd.net>
2022-12-06 22:07:12 +00:00
|
|
|
_set_cloudflare_cookie(response)
|
|
|
|
_commit_and_close_db()
|
2022-12-27 01:22:39 +00:00
|
|
|
|
2022-12-29 14:20:27 +00:00
|
|
|
response.headers.add("Report-To", {"group":"csp","max_age":10886400,"endpoints":[{"url":"/csp_violations"}]})
|
|
|
|
response.headers.add("Content-Security-Policy", CSP_str)
|
2022-12-27 01:22:39 +00:00
|
|
|
|
2022-11-15 09:19:08 +00:00
|
|
|
return response
|
|
|
|
|
2022-11-22 23:37:55 +00:00
|
|
|
|
2022-11-15 09:19:08 +00:00
|
|
|
@app.teardown_appcontext
|
|
|
|
def teardown_request(error):
|
band-aid fix for frozen session issue on signup (#50)
through some reason or another, people are somehow getting cookies that aren't prepended with a dot.
this is a problem because both sessions at, as best as I can tell, mix so it tries to read from a different cookie than we write to. this essentially "freezes" the session in place. users are unable to login, logout, signup, toggle poor mode, toggle NSFW, etc.
~~this attempts to delete bad session cookies (i.e. cookies with a domain that don't start with a dot).~~
~~we don't do this on "dotless" domains (and by extension localhost) because browser support for setting cookies on FQDNs that only have one dot has tenuous support among browsers anyway).~~
~~this *may* log some people out, but... their days of being able to do stuff on the site were numbered anyway.~~
**edit: as amazing as this thought was, browsers just wipe the entire cookies completely and there's no way to specifically target dotless cookies. for an issue that affects a few users, better to just tell them to clear their cookies. if *this* doesn't work, delete service-worker.js and be done with the whole service worker crap. forever. permanently. this PR also includes some QOL improvements.**
Co-authored-by: justcool393 <justcool393@gmail.com>
Reviewed-on: https://fsdfsd.net/rDrama/rDrama/pulls/50
Co-authored-by: justcool393 <justcool393@noreply.fsdfsd.net>
Co-committed-by: justcool393 <justcool393@noreply.fsdfsd.net>
2022-12-06 22:07:12 +00:00
|
|
|
_rollback_and_close_db()
|
2022-11-15 09:19:08 +00:00
|
|
|
stdout.flush()
|
band-aid fix for frozen session issue on signup (#50)
through some reason or another, people are somehow getting cookies that aren't prepended with a dot.
this is a problem because both sessions at, as best as I can tell, mix so it tries to read from a different cookie than we write to. this essentially "freezes" the session in place. users are unable to login, logout, signup, toggle poor mode, toggle NSFW, etc.
~~this attempts to delete bad session cookies (i.e. cookies with a domain that don't start with a dot).~~
~~we don't do this on "dotless" domains (and by extension localhost) because browser support for setting cookies on FQDNs that only have one dot has tenuous support among browsers anyway).~~
~~this *may* log some people out, but... their days of being able to do stuff on the site were numbered anyway.~~
**edit: as amazing as this thought was, browsers just wipe the entire cookies completely and there's no way to specifically target dotless cookies. for an issue that affects a few users, better to just tell them to clear their cookies. if *this* doesn't work, delete service-worker.js and be done with the whole service worker crap. forever. permanently. this PR also includes some QOL improvements.**
Co-authored-by: justcool393 <justcool393@gmail.com>
Reviewed-on: https://fsdfsd.net/rDrama/rDrama/pulls/50
Co-authored-by: justcool393 <justcool393@noreply.fsdfsd.net>
Co-committed-by: justcool393 <justcool393@noreply.fsdfsd.net>
2022-12-06 22:07:12 +00:00
|
|
|
|
|
|
|
def _set_cloudflare_cookie(response:Response) -> None:
|
|
|
|
'''
|
|
|
|
Sets a cookie that can be used by an upstream DDoS protection and caching provider
|
|
|
|
'''
|
|
|
|
if not g.desires_auth: return
|
|
|
|
if not CLOUDFLARE_AVAILABLE or not CLOUDFLARE_COOKIE_VALUE: return
|
|
|
|
logged_in = bool(getattr(g, 'v', None))
|
|
|
|
if not logged_in and request.cookies.get("lo"):
|
|
|
|
response.delete_cookie("lo", domain=app.config["COOKIE_DOMAIN"], samesite="Lax")
|
|
|
|
elif logged_in and not request.cookies.get("lo"):
|
|
|
|
response.set_cookie("lo", CLOUDFLARE_COOKIE_VALUE if logged_in else '',
|
|
|
|
max_age=SESSION_LIFETIME, samesite="Lax",
|
|
|
|
domain=app.config["COOKIE_DOMAIN"])
|
|
|
|
|
|
|
|
def _commit_and_close_db() -> bool:
|
|
|
|
if not getattr(g, 'db', None): return False
|
|
|
|
g.db.commit()
|
|
|
|
g.db.close()
|
|
|
|
del g.db
|
|
|
|
return True
|
|
|
|
|
|
|
|
def _rollback_and_close_db() -> bool:
|
|
|
|
if not getattr(g, 'db', None): return False
|
|
|
|
g.db.rollback()
|
|
|
|
g.db.close()
|
|
|
|
del g.db
|
|
|
|
return True
|