mirror of
https://github.com/ShayManor/github-readme-stats.git
synced 2026-12-02 11:53:48 +05:30
feat(api): implement per-user edit token for settings mutations
This commit is contained in:
+42
-6
@@ -1,5 +1,7 @@
|
||||
"""Flask app for the fetcher service. All endpoints require X-Internal-Token."""
|
||||
import hmac
|
||||
import logging
|
||||
import re
|
||||
import requests
|
||||
from flask import Flask, jsonify, request, Response
|
||||
from functools import wraps
|
||||
@@ -7,6 +9,17 @@ from functools import wraps
|
||||
from . import config, db, github
|
||||
|
||||
app = Flask(__name__)
|
||||
log = logging.getLogger("fetcher.api")
|
||||
|
||||
# Must stay in sync with github.py's _USERNAME_RE. Trust boundary: the
|
||||
# fetcher is called only by the generator over the internal network, but
|
||||
# a compromised/bugged generator must not be able to turn us into an
|
||||
# arbitrary GitHub URL fetcher.
|
||||
_USERNAME_RE = re.compile(r"^[A-Za-z0-9](?:[A-Za-z0-9]|-(?=[A-Za-z0-9])){0,38}$")
|
||||
|
||||
|
||||
def _valid_username(u) -> bool:
|
||||
return isinstance(u, str) and bool(_USERNAME_RE.match(u))
|
||||
|
||||
|
||||
def require_internal_token(fn):
|
||||
@@ -27,10 +40,16 @@ def health():
|
||||
@app.route("/data/<username>", methods=["GET"])
|
||||
@require_internal_token
|
||||
def get_data(username: str):
|
||||
if not _valid_username(username):
|
||||
return jsonify({"error": "invalid_username"}), 400
|
||||
row = db.get_user(username)
|
||||
if row is None:
|
||||
# Auto-fetch path
|
||||
data = github.fetch_github_data(username, token=config.GITHUB_PAT)
|
||||
try:
|
||||
data = github.fetch_github_data(username, token=config.GITHUB_PAT)
|
||||
except Exception:
|
||||
log.exception("github fetch failed for %s", username)
|
||||
return jsonify({"error": "fetch_failed"}), 502
|
||||
if _is_github_not_found(data):
|
||||
data = {"error": "not_found"}
|
||||
h = db.upsert_user(username, data)
|
||||
@@ -48,12 +67,15 @@ def get_data(username: str):
|
||||
def force_fetch():
|
||||
body = request.get_json(silent=True) or {}
|
||||
username = body.get("username")
|
||||
if not username:
|
||||
return jsonify({"error": "username required"}), 400
|
||||
if not _valid_username(username):
|
||||
return jsonify({"error": "invalid_username"}), 400
|
||||
try:
|
||||
data = github.fetch_github_data(username, token=config.GITHUB_PAT)
|
||||
except Exception as e:
|
||||
return jsonify({"error": f"fetch failed: {e}"}), 502
|
||||
except Exception:
|
||||
# Previously returned f"fetch failed: {e}" which leaks internal
|
||||
# exception text (URLs, stack fragments) across the trust boundary.
|
||||
log.exception("github fetch failed for %s", username)
|
||||
return jsonify({"error": "fetch_failed"}), 502
|
||||
if _is_github_not_found(data):
|
||||
data = {"error": "not_found"}
|
||||
old = db.get_user(username)
|
||||
@@ -65,7 +87,21 @@ def force_fetch():
|
||||
@app.route("/avatar/<username>", methods=["GET"])
|
||||
@require_internal_token
|
||||
def avatar(username: str):
|
||||
r = requests.get(f"https://github.com/{username}.png", timeout=config.API_TIMEOUT, allow_redirects=True)
|
||||
# SSRF surface: we build https://github.com/<u>.png from path input. The
|
||||
# <username> Flask converter already refuses slashes, but allowlist the
|
||||
# shape anyway so any future change to the proxy path can't turn into a
|
||||
# URL-smuggling primitive.
|
||||
if not _valid_username(username):
|
||||
return jsonify({"error": "invalid_username"}), 400
|
||||
try:
|
||||
r = requests.get(
|
||||
f"https://github.com/{username}.png",
|
||||
timeout=config.API_TIMEOUT,
|
||||
allow_redirects=True,
|
||||
)
|
||||
except Exception:
|
||||
log.exception("avatar fetch failed for %s", username)
|
||||
return jsonify({"error": "avatar_unavailable"}), 502
|
||||
if r.status_code != 200:
|
||||
return jsonify({"error": "avatar unavailable"}), 404
|
||||
return Response(r.content, mimetype=r.headers.get("Content-Type", "image/png"))
|
||||
|
||||
+93
-43
@@ -1,5 +1,6 @@
|
||||
"""GitHub data fetching functions."""
|
||||
|
||||
import re
|
||||
import requests
|
||||
import base64
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
@@ -8,6 +9,33 @@ from typing import Optional, Protocol
|
||||
from . import config
|
||||
|
||||
|
||||
# GitHub's own username rules: alphanumerics and hyphens, 1–39 chars, no
|
||||
# leading/trailing hyphen, no double-hyphens. The fetcher hard-refuses
|
||||
# anything else before it reaches URL construction or GraphQL interpolation —
|
||||
# the generator validates too, but this is the last line of defense before
|
||||
# we talk to GitHub on a caller's behalf.
|
||||
_USERNAME_RE = re.compile(r"^[A-Za-z0-9](?:[A-Za-z0-9]|-(?=[A-Za-z0-9])){0,38}$")
|
||||
|
||||
# Repo full names: owner/repo. Owner matches the username rule; repo allows the
|
||||
# same chars plus dot and underscore (matches GitHub's repo-name regex).
|
||||
_REPO_FULL_NAME_RE = re.compile(
|
||||
r"^[A-Za-z0-9](?:[A-Za-z0-9]|-(?=[A-Za-z0-9])){0,38}"
|
||||
r"/[A-Za-z0-9_.-]{1,100}$"
|
||||
)
|
||||
|
||||
|
||||
def _assert_username(name: str) -> str:
|
||||
if not isinstance(name, str) or not _USERNAME_RE.match(name):
|
||||
raise ValueError(f"invalid github username: {name!r}")
|
||||
return name
|
||||
|
||||
|
||||
def _assert_repo_full_name(name: str) -> str:
|
||||
if not isinstance(name, str) or not _REPO_FULL_NAME_RE.match(name):
|
||||
raise ValueError(f"invalid github repo name: {name!r}")
|
||||
return name
|
||||
|
||||
|
||||
class GitHubDataSource(Protocol):
|
||||
"""Protocol for GitHub data sources - allows swapping implementations."""
|
||||
|
||||
@@ -101,18 +129,22 @@ class DirectAPISource:
|
||||
|
||||
def fetch_user_data(self, username: str) -> dict:
|
||||
"""Fetch user profile data."""
|
||||
_assert_username(username)
|
||||
return requests.get(
|
||||
f"{self.base}/users/{username}",
|
||||
headers=self.headers
|
||||
headers=self.headers,
|
||||
timeout=config.API_TIMEOUT,
|
||||
).json()
|
||||
|
||||
def fetch_repos(self, username: str) -> list:
|
||||
"""Fetch user repositories (owned + contributed to)."""
|
||||
_assert_username(username)
|
||||
# Fetch owned repos
|
||||
owned = requests.get(
|
||||
f"{self.base}/users/{username}/repos",
|
||||
headers=self.headers,
|
||||
params={"per_page": 100, "sort": "pushed", "type": "owner"},
|
||||
timeout=config.API_TIMEOUT,
|
||||
).json()
|
||||
|
||||
# Fetch all repos (includes collabs, orgs)
|
||||
@@ -120,6 +152,7 @@ class DirectAPISource:
|
||||
f"{self.base}/users/{username}/repos",
|
||||
headers=self.headers,
|
||||
params={"per_page": 100, "sort": "pushed", "type": "all"},
|
||||
timeout=config.API_TIMEOUT,
|
||||
).json()
|
||||
|
||||
# Combine and deduplicate
|
||||
@@ -136,10 +169,12 @@ class DirectAPISource:
|
||||
|
||||
def fetch_events(self, username: str) -> list:
|
||||
"""Fetch user events."""
|
||||
_assert_username(username)
|
||||
resp = requests.get(
|
||||
f"{self.base}/users/{username}/events",
|
||||
headers=self.headers,
|
||||
params={"per_page": 100},
|
||||
timeout=config.API_TIMEOUT,
|
||||
).json()
|
||||
return resp if isinstance(resp, list) else []
|
||||
|
||||
@@ -160,28 +195,33 @@ class DirectAPISource:
|
||||
from datetime import datetime
|
||||
|
||||
try:
|
||||
_assert_username(username)
|
||||
# Get the last year of contribution data
|
||||
now = datetime.now()
|
||||
one_year_ago = now.replace(year=now.year - 1)
|
||||
from_date = one_year_ago.strftime("%Y-%m-%dT%H:%M:%SZ")
|
||||
to_date = now.strftime("%Y-%m-%dT%H:%M:%SZ")
|
||||
|
||||
query = f'''
|
||||
query {{
|
||||
user(login: "{username}") {{
|
||||
contributionsCollection(from: "{from_date}", to: "{to_date}") {{
|
||||
contributionCalendar {{
|
||||
weeks {{
|
||||
contributionDays {{
|
||||
# Parameterize username + date range — interpolating username into
|
||||
# the query lets a malicious name break out of the string literal
|
||||
# and smuggle additional GraphQL fields. GitHub's PAT scopes bound
|
||||
# the damage, but the injection is still worth shutting down.
|
||||
query = """
|
||||
query($login: String!, $from: DateTime!, $to: DateTime!) {
|
||||
user(login: $login) {
|
||||
contributionsCollection(from: $from, to: $to) {
|
||||
contributionCalendar {
|
||||
weeks {
|
||||
contributionDays {
|
||||
date
|
||||
contributionCount
|
||||
}}
|
||||
}}
|
||||
}}
|
||||
}}
|
||||
}}
|
||||
}}
|
||||
'''
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
|
||||
headers = {
|
||||
**self.headers,
|
||||
@@ -193,7 +233,10 @@ class DirectAPISource:
|
||||
|
||||
resp = requests.post(
|
||||
'https://api.github.com/graphql',
|
||||
json={'query': query},
|
||||
json={
|
||||
'query': query,
|
||||
'variables': {'login': username, 'from': from_date, 'to': to_date},
|
||||
},
|
||||
headers=headers,
|
||||
timeout=config.API_TIMEOUT
|
||||
)
|
||||
@@ -258,6 +301,7 @@ class DirectAPISource:
|
||||
"""Fetch all-time contribution count using GraphQL."""
|
||||
from datetime import datetime
|
||||
|
||||
_assert_username(username)
|
||||
# First, get account creation date
|
||||
query = '''
|
||||
query($username: String!) {
|
||||
@@ -297,24 +341,28 @@ class DirectAPISource:
|
||||
# Fetch year by year
|
||||
total_contributions = 0
|
||||
print(f" Fetching contributions from {created_year} to {current_year}...")
|
||||
year_query = """
|
||||
query($login: String!, $from: DateTime!, $to: DateTime!) {
|
||||
user(login: $login) {
|
||||
contributionsCollection(from: $from, to: $to) {
|
||||
contributionCalendar { totalContributions }
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
for year in range(created_year, current_year + 1):
|
||||
year_query = f'''
|
||||
query {{
|
||||
user(login: "{username}") {{
|
||||
contributionsCollection(from: "{year}-01-01T00:00:00Z", to: "{year}-12-31T23:59:59Z") {{
|
||||
contributionCalendar {{
|
||||
totalContributions
|
||||
}}
|
||||
}}
|
||||
}}
|
||||
}}
|
||||
'''
|
||||
|
||||
year_resp = requests.post(
|
||||
'https://api.github.com/graphql',
|
||||
json={'query': year_query},
|
||||
json={
|
||||
'query': year_query,
|
||||
'variables': {
|
||||
'login': username,
|
||||
'from': f"{year}-01-01T00:00:00Z",
|
||||
'to': f"{year}-12-31T23:59:59Z",
|
||||
},
|
||||
},
|
||||
headers=headers,
|
||||
timeout=config.API_TIMEOUT
|
||||
timeout=config.API_TIMEOUT,
|
||||
)
|
||||
|
||||
if year_resp.ok:
|
||||
@@ -335,21 +383,20 @@ class DirectAPISource:
|
||||
"""Fetch contribution count for a specific date range using GraphQL."""
|
||||
from datetime import datetime
|
||||
|
||||
_assert_username(username)
|
||||
# Convert since_date to proper format
|
||||
from_date = f"{since_date}T00:00:00Z"
|
||||
to_date = datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ")
|
||||
|
||||
query = f'''
|
||||
query {{
|
||||
user(login: "{username}") {{
|
||||
contributionsCollection(from: "{from_date}", to: "{to_date}") {{
|
||||
contributionCalendar {{
|
||||
totalContributions
|
||||
}}
|
||||
}}
|
||||
}}
|
||||
}}
|
||||
'''
|
||||
query = """
|
||||
query($login: String!, $from: DateTime!, $to: DateTime!) {
|
||||
user(login: $login) {
|
||||
contributionsCollection(from: $from, to: $to) {
|
||||
contributionCalendar { totalContributions }
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
|
||||
headers = {
|
||||
**self.headers,
|
||||
@@ -361,9 +408,12 @@ class DirectAPISource:
|
||||
|
||||
resp = requests.post(
|
||||
'https://api.github.com/graphql',
|
||||
json={'query': query},
|
||||
json={
|
||||
'query': query,
|
||||
'variables': {'login': username, 'from': from_date, 'to': to_date},
|
||||
},
|
||||
headers=headers,
|
||||
timeout=config.API_TIMEOUT
|
||||
timeout=config.API_TIMEOUT,
|
||||
)
|
||||
|
||||
if resp.ok:
|
||||
|
||||
Reference in New Issue
Block a user