-
Notifications
You must be signed in to change notification settings - Fork 729
Expand file tree
/
Copy pathmain.py
More file actions
41 lines (30 loc) · 1.05 KB
/
main.py
File metadata and controls
41 lines (30 loc) · 1.05 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
import posixpath
from flask import Flask, Response, abort
from flask_cors import CORS
from google.cloud import storage
app = Flask(__name__)
CORS(
app,
origins=["https://ui.perfetto.dev", "http://localhost:10000"],
supports_credentials=True)
BUCKET_NAME = "perfetto-ui-internal"
BASE_PREFIX = "extension-server-v1"
gcs_client = storage.Client()
bucket = gcs_client.bucket(BUCKET_NAME)
@app.route("/", defaults={"path": ""})
@app.route("/<path:path>")
def serve(path):
# Normalize and reject any path traversal attempts.
normalized = posixpath.normpath(path) if path else ""
if normalized.startswith(
"..") or "/../" in normalized or normalized.startswith("/"):
abort(403)
blob_path = f"{BASE_PREFIX}/{normalized}" if normalized else BASE_PREFIX + "/"
blob = bucket.blob(blob_path)
if not blob.exists():
abort(404)
data = blob.download_as_bytes()
content_type = blob.content_type or "application/octet-stream"
return Response(data, content_type=content_type)
if __name__ == "__main__":
app.run(host="127.0.0.1", port=8080)