-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.py
More file actions
299 lines (263 loc) · 9.06 KB
/
Copy pathindex.py
File metadata and controls
299 lines (263 loc) · 9.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import re
from flask import Flask, request, send_file, render_template_string, jsonify
from collections import defaultdict, deque
from typing import Dict
import tempfile, os, time, hashlib, secrets
from flag import create_flag_with_footer, validate_name
app = Flask(__name__)
_ip_hits: Dict[str, deque] = defaultdict(deque)
RATE_LIMIT, TIME_WINDOW = 5, 10
cache_dir = os.path.join(tempfile.gettempdir(), "flag_cache")
os.makedirs(cache_dir, exist_ok=True)
image_cache: Dict[str, str] = {}
CACHE_TTL = 60 * 60 * 24
def is_rate_limited(ip: str) -> bool:
now = time.time()
hits = _ip_hits[ip]
while hits and now - hits[0] > TIME_WINDOW:
hits.popleft()
if len(hits) >= RATE_LIMIT:
return True
hits.append(now)
return False
def get_cache_key(name: str) -> str:
return hashlib.sha256(name.encode("utf-8")).hexdigest()
def clean_cache():
now = time.time()
for key, path in list(image_cache.items()):
if not os.path.exists(path) or now - os.path.getmtime(path) > CACHE_TTL:
image_cache.pop(key, None)
if os.path.exists(path):
os.remove(path)
@app.after_request
def set_security_headers(response):
nonce = getattr(request, "csp_nonce", "")
csp = (
f"default-src 'self'; "
f"style-src 'self' https://fonts.googleapis.com 'nonce-{nonce}'; "
f"font-src https://fonts.gstatic.com; "
f"script-src 'self' 'nonce-{nonce}'; "
f"img-src 'self' data: blob:; "
"object-src 'none'; base-uri 'self';"
)
response.headers.update({
"Content-Security-Policy": csp,
"X-Content-Type-Options": "nosniff",
"X-Frame-Options": "DENY",
"Referrer-Policy": "no-referrer",
"Cache-Control": "public, max-age=86400"
# "Strict-Transport-Security": "max-age=63072000; includeSubDomains; preload"
})
return response
@app.before_request
def generate_nonce():
request.csp_nonce = secrets.token_urlsafe(16)
HTML_FORM = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>🇮🇳 Indian Flag Generator</title>
<meta name="description" content="A special 🇮🇳 Happy Independence Day greeting for your friends and Family Members.">
<link rel="preconnect" href="https://fonts.googleapis.com" nonce="{{nonce}}">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin nonce="{{nonce}}">
<link href="https://fonts.googleapis.com/css2?family=Mozilla+Text:wght@200..700&display=swap" rel="stylesheet" nonce="{{nonce}}">
<style nonce="{{nonce}}">
body {
background: #ffe4e9;
color: #333;
font-family: "Mozilla Text", sans-serif;
font-weight: 600;
display: flex;
flex-direction: column;
align-items: center;
padding: 10px;
margin: 0;
}
h1 {
color: #c2185b;
text-align: center;
font-family: "Mozilla Text", sans-serif;
font-size: clamp(1.5rem, 5vw, 2.2rem);
}
.container {
background: #fff0f5;
padding: 20px;
border-radius: 12px;
box-shadow: 0 4px 20px rgba(194, 24, 91, 0.15);
max-width: 500px;
width: 100%;
box-sizing: border-box;
}
input {
width: 100%;
padding: 12px;
font-family: "Mozilla Text", sans-serif;
font-size: 16px;
background: #ffeef3;
border: 1px solid #f8cdd7;
border-radius: 6px;
color: #333;
margin-bottom: 12px;
box-sizing: border-box;
}
input:focus {
outline: none;
border-color: #f48fb1;
box-shadow: 0 0 0 2px rgba(244, 143, 177, 0.3);
}
button {
padding: 12px;
border: none;
border-radius: 6px;
font-family: "Mozilla Text", sans-serif;
font-size: 16px;
cursor: pointer;
font-weight: bold;
width: 100%;
margin-bottom: 10px;
transition: opacity 0.2s ease-in-out;
}
.primary {
background: linear-gradient(90deg, #f06292, #ec407a);
color: white;
}
.primary:hover { opacity: 0.9; }
.success {
background: linear-gradient(90deg, #81c784, #4caf50);
color: white;
display: none;
}
.terminal {
background: #fff5f8;
color: #ad1457;
font-family: monospace;
padding: 10px;
height: 200px;
overflow-y: auto;
border-radius: 6px;
font-size: 14px;
margin-top: 10px;
white-space: pre-wrap;
border: 1px solid #f8cdd7;
box-shadow: inset 0 0 8px rgba(0,0,0,0.05);
}
.terminal div::before {
content: "$ ";
color: #ec407a;
font-weight: bold;
}
img {
max-width: 100%;
margin-top: 12px;
border-radius: 8px;
display: none;
}
</style>
</head>
<body>
<h1>🇮🇳 Indian Flag Generator</h1>
<div class="container">
<input type="text" id="name" placeholder="Enter Your Name" maxlength="30">
<button id="generateBtn" class="primary">Generate Flag</button>
<button id="downloadBtn" class="success">Download Flag</button>
<div class="terminal" id="terminal">$ ./flag.py "your name"\n</div>
<img id="flagImage" alt="Generated Flag">
</div>
<script nonce="{{nonce}}">
const term = document.getElementById("terminal");
const generateBtn = document.getElementById("generateBtn");
const downloadBtn = document.getElementById("downloadBtn");
const flagImage = document.getElementById("flagImage");
function log(msg){
const line = document.createElement("div");
line.textContent = msg;
term.appendChild(line);
term.scrollTop = term.scrollHeight;
}
function generateFlag() {
const name = document.getElementById("name").value.trim();
term.textContent = `$ ./flag.py "${name || 'your name'}"\n`;
if (!name) {
log("❌ Please enter a name.");
return;
}
log("📦 Starting flag generation...");
log("🔍 Validating name...");
fetch(`/generate?name=${encodeURIComponent(name)}`)
.then(r => {
if (!r.ok) {
return r.json().then(data => {
throw new Error(data.error || "Server error");
});
}
if (r.headers.get("content-type")?.includes("application/json")) {
return r.json().then(data => { throw new Error(data.error); });
}
return r.blob();
})
.then(b => {
log("🎨 Drawing Indian flag...");
log("🖌 Adding footer text...");
const url = URL.createObjectURL(b);
flagImage.src = url;
flagImage.style.display = "block";
downloadBtn.onclick = () => {
const a = document.createElement("a");
a.href = url;
a.download = "indian_flag.png";
a.click();
};
downloadBtn.style.display = "block";
log("✅ Flag generated successfully!");
})
.catch(e => {
log("❌ " + e.message);
flagImage.style.display = "none";
downloadBtn.style.display = "none";
});
}
generateBtn.addEventListener("click", generateFlag);
</script>
</body>
</html>
"""
@app.route("/")
def index():
return render_template_string(HTML_FORM, nonce=request.csp_nonce)
@app.route("/generate")
def generate_flag():
client_ip = request.headers.get("X-Forwarded-For", request.remote_addr)
if is_rate_limited(client_ip):
print(f"⚠️ Rate limit hit: {client_ip}")
return jsonify({"error": "Too many requests"}), 429
raw_name = request.args.get("name", "").strip()
if not raw_name:
print("❌ Empty name received")
return jsonify({"error": "Name cannot be empty"}), 400
if any(ch in raw_name for ch in ['<', '>', '"', "'", '&', '`']):
print(f"⚠️ Possible XSS attempt from {client_ip}: {raw_name}")
return jsonify({"error": "Invalid characters in name"}), 400
if not re.match(r"^[\w\s\-\.\u00C0-\uFFFF]+$", raw_name):
print(f"⚠️ Disallowed characters from {client_ip}: {raw_name}")
return jsonify({"error": "Name contains unsupported characters"}), 400
try:
name = validate_name(raw_name)
clean_cache()
cache_key = get_cache_key(name)
if cache_key in image_cache and os.path.exists(image_cache[cache_key]):
return send_file(image_cache[cache_key], mimetype="image/png")
image_path = os.path.join(cache_dir, f"{cache_key}.png")
create_flag_with_footer(name, image_path)
image_cache[cache_key] = image_path
return send_file(image_path, mimetype="image/png")
except ValueError as ve:
print(f"❌ Validation error: {ve}")
return jsonify({"error": str(ve)}), 400
except Exception as e:
print(f"❌ Internal error: {e}")
return jsonify({"error": "An internal error occurred"}), 500
if __name__ == "__main__":
app.run()