Skip to content

Commit 26cab72

Browse files
committed
Fix vulnerability, lint
1 parent b096030 commit 26cab72

File tree

14 files changed

+265
-232
lines changed

14 files changed

+265
-232
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
make:
2-
python3 -m venv ./.venv
3-
./.venv/bin/python3 -m pip install -r requirements.txt
2+
python3 -m venv venv
3+
./venv/bin/python3 -m pip install -r requirements.txt

SECURITY.md

Lines changed: 0 additions & 50 deletions
This file was deleted.

app.py

Lines changed: 56 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -40,72 +40,17 @@
4040
import yaml
4141
import requests
4242

43-
with open("config.yaml","r") as stream:
43+
import exceptions
44+
from utils import verifyRecaptcha, generateInvite, verifyConfig
45+
46+
with open("config.yaml", "r") as f:
4447
try:
45-
config = yaml.safe_load(stream)
48+
config = yaml.safe_load(f)
4649
except yaml.YAMLError as exc:
4750
print(exc)
4851
quit(1)
49-
50-
51-
if "dark_theme" not in config:
52-
print("!! Theme not defined")
53-
if "recaptcha" in config:
54-
if config["recaptcha"]["public"] == None:
55-
print("!! Recaptcha public key is not defined, exiting")
56-
quit(1)
57-
if config["recaptcha"]["private"] == None:
58-
print("!! Recaptcha private key is not defined, exiting")
59-
quit(1)
60-
else:
61-
print("!! Recaptcha config doesnt exist, exiting")
62-
quit(1)
63-
64-
if "discord" in config:
65-
if config["discord"]["welcome_room"] == None:
66-
print("!! Discord welcome room not defined, exiting")
67-
quit(1)
68-
if config["discord"]["private"] == None:
69-
print("!! Discord private key is not defined, exiting")
70-
quit(1)
71-
else:
72-
print("!! Discord config doesnt exist, exiting")
73-
quit(1)
74-
75-
if "server" in config:
76-
if config["server"]["port"] == None:
77-
print("!! Server port not defined, exiting")
78-
quit(1)
79-
else:
80-
print("!! Sever config not defined, exiting")
81-
quit(1)
82-
83-
def recaptcha(token):
84-
print(f"Verifying recaptcha {token[:15]}")
85-
recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify'
86-
payload = {
87-
'secret': config["recaptcha"]["private"],
88-
'response': token,
89-
'remoteip': request.remote_addr,
90-
}
91-
response = requests.post(recaptcha_url, data = payload)
92-
result = response.json()
93-
return result
94-
95-
def invite():
96-
print("Generating new invite!")
97-
resp = requests.post(
98-
'https://discordapp.com/api/channels/{}/invites'.format(config["discord"]["welcome_room"]),
99-
headers={'Authorization': 'Bot {}'.format(config["discord"]["private"])},
100-
json={'max_uses': 1, 'unique': True, 'max_age': 300}
101-
)
102-
i = resp.json()
103-
# error handling for invite creation
104-
if (i.get('code')):
105-
print("Generated new invite!")
10652
else:
107-
print(i)
108-
return i["code"]
53+
verifyConfig(config)
10954

11055
app = Flask(__name__)
11156

@@ -114,17 +59,56 @@ def invite():
11459
catpcha_theme = "dark" if config["dark_theme"] else "light"
11560

11661

117-
@app.route("/") # main function
62+
@app.route("/")
11863
def index():
119-
key = request.args.get('key') # get key parameter from URL
120-
if key: # if key set
121-
r = recaptcha(key) # confirm captcha
122-
if r.get("success"): # if ok
64+
key = request.args.get("key")
65+
if key: # User has submitted a captcha
66+
r = verifyRecaptcha(key, request, config)
67+
if r.get("success"): # Captcha is OK
12368
print(f"Recaptcha {key[:30]} verified!")
124-
i = invite() # generate new invite
125-
return redirect(f"https://discord.gg/{i}") # redirect user to new invite
126-
else: # if captcha invalid
69+
inviteCode = generateInvite(config)
70+
return redirect(f"https://discord.gg/{inviteCode}")
71+
else: # Captcha failed
12772
print(f"Recaptcha {key[:30]} failed!")
128-
return render_template("index.html", public=config["recaptcha"]["public"], failed=True, theme=theme, border=border, catpcha_theme=catpcha_theme) # return error page
129-
# if not key
130-
return render_template("index.html", public=config["recaptcha"]["public"], failed=False, theme=theme, border=border, catpcha_theme=catpcha_theme) # return normal page
73+
# Return error page
74+
return render_template(
75+
"index.html",
76+
public=config["recaptcha"]["public"],
77+
failed="Invalid captcha, try again",
78+
theme=theme,
79+
border=border,
80+
catpcha_theme=catpcha_theme,
81+
)
82+
83+
return render_template(
84+
"index.html",
85+
public=config["recaptcha"]["public"],
86+
failed=None,
87+
theme=theme,
88+
border=border,
89+
catpcha_theme=catpcha_theme,
90+
) # Return normal page
91+
92+
93+
@app.errorhandler(500)
94+
def internalError(error):
95+
return render_template(
96+
"index.html",
97+
public=config["recaptcha"]["public"],
98+
failed="Internal server error, please try again later",
99+
theme=theme,
100+
border=border,
101+
catpcha_theme=catpcha_theme,
102+
)
103+
104+
105+
@app.errorhandler(404)
106+
def notFound(error):
107+
return render_template(
108+
"index.html",
109+
public=config["recaptcha"]["public"],
110+
failed=None,
111+
theme=theme,
112+
border=border,
113+
catpcha_theme=catpcha_theme,
114+
)

config.example.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ discord:
2828
server:
2929
# the script will host the gateway on this port
3030
# defaults to 80
31-
port: 5000
31+
port: 5000

config.yaml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#
22
# Hello there, user
33
# this configuration file contains your credentials, make sure not to share it with ANYONE.
4-
# anyone with your Discord private key can controll your bot!
4+
# anyone with your Discord private key can control your bot!
55
#
66

77

@@ -10,21 +10,21 @@
1010
dark_theme: false
1111

1212
recaptcha:
13-
# put your public recaptcha key here!
14-
public:
13+
# put your public (site key) recaptcha key here!
14+
public:
1515

1616
# DO NOT LEAK THIS
17-
# put your private recapthca key here!
18-
private:
17+
# put your private (secret) recapthca key here!
18+
private:
1919

2020
discord:
2121
# users will be invited to this room, it should be public
2222
# put your welcome room ID here
23-
welcome_room:
23+
welcome_room:
2424

2525
# DO NOT LEAK THIS
2626
# put your Discord bot token here
27-
private:
27+
private:
2828

2929
server:
3030
# the script will host the gateway on this port

exceptions.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class InviteGenerationError(Exception):
2+
def __init__(self, message):
3+
super().__init__(message)

readme.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
> [!CAUTION]
2+
> UPGRADE TO VERSION >= 1.1.0 DUE TO SECURITY VULNERABILITY IN OLDER VERSIONS
3+
4+
---
5+
16
# f1rewall
27
*The sleek, simple and scalable invite gateway for your Discord community*
38

@@ -90,7 +95,7 @@ Congrats! Your recaptcha is now ready!
9095
1. Run `apt-get update -y && apt-get upgrade -y` to update your packages
9196
1. Run `apt-get install python3-dev -y && apt-get install python3-venv -y` to install the required dependencies for Python
9297
1. Run `sudo make` to install all dependencies
93-
2. Run `sh run.sh` to start the server
98+
2. Run `./venv/bin/python3 server.py` to start the server
9499
3. The script will now host your gateway on the port specified in config.yaml
95100

96101
#### Debugging

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ flask
22
requests
33
gevent
44
pyyaml
5+
black

run.sh

Lines changed: 0 additions & 1 deletion
This file was deleted.

server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from app import app
33
import yaml
44

5-
with open("config.yaml","r") as stream:
5+
with open("config.yaml", "r") as stream:
66
try:
77
config = yaml.safe_load(stream)
88
except yaml.YAMLError as exc:
@@ -11,5 +11,5 @@
1111

1212
print(f"Serving on port {config['server']['port']}")
1313

14-
http_server = WSGIServer(('', config["server"]["port"]), app)
14+
http_server = WSGIServer(("", config["server"]["port"]), app)
1515
http_server.serve_forever()

0 commit comments

Comments
 (0)