Repository navigation
Expand file tree
/
Copy pathanalyzer.py
More file actions
2585 lines (2134 loc) · 111 KB
/
Copy pathanalyzer.py
File metadata and controls
2585 lines (2134 loc) · 111 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import json
import os
import sys
import requests
import yaml
import argparse
import re
import hashlib
import math
from typing import Any, Dict, List, Optional, Tuple, Set
from urllib.parse import urljoin, urlparse
from datetime import datetime, timedelta
from collections import defaultdict, Counter
# --- Repository Information ---
def get_repository_info(owner: str, repo: str, token: Optional[str] = None) -> Dict[str, Any]:
"""Get repository information from GitHub API."""
headers = {}
if token:
headers["Authorization"] = f"token {token}"
api_url = f"https://api.github.com/repos/{owner}/{repo}"
try:
response = requests.get(api_url, headers=headers, timeout=30)
response.raise_for_status()
return response.json()
except requests.RequestException as e:
return {"error": f"Failed to fetch repository info: {e}"}
def find_openapi_files(owner: str, repo: str, token: Optional[str] = None) -> List[Dict[str, Any]]:
"""Find OpenAPI files in a GitHub repository."""
headers = {}
if token:
headers["Authorization"] = f"token {token}"
# Common OpenAPI file patterns
openapi_patterns = [
"**/openapi.json",
"**/openapi.yaml",
"**/openapi.yml",
"**/swagger.json",
"**/swagger.yaml",
"**/swagger.yml",
"**/api.json",
"**/api.yaml",
"**/api.yml",
"**/spec.json",
"**/spec.yaml",
"**/spec.yml"
]
found_files = []
for pattern in openapi_patterns:
search_url = f"https://api.github.com/search/code"
params = {
"q": f"repo:{owner}/{repo} filename:{pattern}",
"per_page": 100
}
try:
response = requests.get(search_url, headers=headers, params=params, timeout=30)
response.raise_for_status()
data = response.json()
for item in data.get("items", []):
file_info = {
"name": item["name"],
"path": item["path"],
"url": item["html_url"],
"download_url": item["download_url"],
"size": item.get("size", 0),
"type": "file"
}
found_files.append(file_info)
except requests.RequestException as e:
print(f"Warning: Failed to search for {pattern}: {e}")
continue
return found_files
def get_repository_contents(owner: str, repo: str, path: str = "", token: Optional[str] = None) -> List[Dict[str, Any]]:
"""Get repository contents from a specific path."""
headers = {}
if token:
headers["Authorization"] = f"token {token}"
api_url = f"https://api.github.com/repos/{owner}/{repo}/contents/{path}"
try:
response = requests.get(api_url, headers=headers, timeout=30)
response.raise_for_status()
return response.json()
except requests.RequestException as e:
return []
def analyze_repository_openapi(owner: str, repo: str, token: Optional[str] = None) -> Dict[str, Any]:
"""Analyze all OpenAPI files found in a repository."""
print(f"Analyzing repository: {owner}/{repo}")
repo_info = get_repository_info(owner, repo, token)
if "error" in repo_info:
return {"status": "error", "message": repo_info["error"]}
openapi_files = find_openapi_files(owner, repo, token)
if not openapi_files:
return {
"status": "success",
"repository": {
"name": repo_info["name"],
"full_name": repo_info["full_name"],
"description": repo_info.get("description", ""),
"url": repo_info["html_url"],
"language": repo_info.get("language", ""),
"stars": repo_info.get("stargazers_count", 0),
"forks": repo_info.get("forks_count", 0)
},
"openapi_files": [],
"message": "No OpenAPI files found in repository"
}
analysis_results = []
for file_info in openapi_files:
print(f"Analyzing: {file_info['path']}")
try:
response = requests.get(file_info["download_url"], timeout=30)
response.raise_for_status()
spec, parse_suggestions = _load_openapi_from_bytes(response.content, file_path=file_info.get("path"))
if spec:
result = analyze_openapi_spec(spec)
result["file_info"] = file_info
analysis_results.append(result)
else:
analysis_results.append({
"file_info": file_info,
"status": "error",
"message": "Failed to parse OpenAPI spec",
"suggestions": parse_suggestions
})
except requests.RequestException as e:
analysis_results.append({
"file_info": file_info,
"status": "error",
"message": f"Failed to download file: {e}"
})
return {
"status": "success",
"repository": {
"name": repo_info["name"],
"full_name": repo_info["full_name"],
"description": repo_info.get("description", ""),
"url": repo_info["html_url"],
"language": repo_info.get("language", ""),
"stars": repo_info.get("stargazers_count", 0),
"forks": repo_info.get("forks_count", 0)
},
"openapi_files": analysis_results
}
# --- Helpers ---
def _load_openapi_from_bytes(data: bytes, file_path: Optional[str] = None) -> Tuple[Optional[dict], List[str]]:
"""Load OpenAPI spec from bytes, supporting both JSON and YAML formats.
Args:
data: The file content as bytes
file_path: Optional file path to help determine format from extension
Returns:
Tuple of (parsed_spec_dict, suggestions_list)
"""
suggestions: List[str] = []
text = data.decode("utf-8", errors="replace").strip()
# Determine format preference based on file extension if provided
prefer_yaml = False
if file_path:
file_lower = file_path.lower()
if file_lower.endswith(('.yaml', '.yml')):
prefer_yaml = True
elif file_lower.endswith('.json'):
prefer_yaml = False
else:
# Content-based detection: check if it looks like YAML
# YAML files often start with --- or contain YAML-specific syntax
if text.startswith('---') or (not text.startswith('{') and not text.startswith('[')):
prefer_yaml = True
else:
# Content-based detection when no file path provided
if text.startswith('---') or (not text.startswith('{') and not text.startswith('[')):
prefer_yaml = True
loaded: Optional[dict] = None
# Try parsing based on detected/preferred format
if prefer_yaml:
try:
loaded = yaml.safe_load(text)
if loaded is None:
# Empty YAML file or only comments
suggestions.append("YAML file appears to be empty or contains only comments.")
except yaml.YAMLError as e:
# If YAML fails, try JSON as fallback
try:
loaded = json.loads(text)
except json.JSONDecodeError:
suggestions.append(f"Failed to parse as YAML: {e}")
loaded = None
else:
try:
loaded = json.loads(text)
except json.JSONDecodeError:
# If JSON fails, try YAML as fallback
try:
loaded = yaml.safe_load(text)
except yaml.YAMLError as e:
suggestions.append(f"Failed to parse as JSON or YAML: {e}")
loaded = None
if not isinstance(loaded, dict):
suggestions.append("OpenAPI content is not a valid JSON/YAML object.")
return None, suggestions
return loaded, suggestions
def _is_openapi_v2(spec: dict) -> bool:
"""Return True if spec looks like Swagger/OpenAPI v2.0."""
swagger = spec.get("swagger")
if isinstance(swagger, str) and swagger.startswith("2."):
return True
return False
def _normalize_v2_to_v3ish(spec: dict) -> dict:
"""Normalize a Swagger 2.0 spec into a v3-like shape so downstream rules work.
This does not attempt full fidelity conversion; it maps common fields used by
our analyzer: components.schemas, components.securitySchemes, servers,
requestBody/content, and response.content schemas.
"""
import copy
v2 = copy.deepcopy(spec)
normalized: Dict[str, Any] = copy.deepcopy(spec)
# Mark original version and normalized hint
normalized.setdefault("x-original-version", v2.get("swagger", "2.0"))
normalized["x-normalized"] = True
# components
components: Dict[str, Any] = normalized.get("components") or {}
if not isinstance(components, dict):
components = {}
# definitions -> components.schemas
if isinstance(v2.get("definitions"), dict):
components.setdefault("schemas", v2.get("definitions", {}))
# securityDefinitions -> components.securitySchemes
if isinstance(v2.get("securityDefinitions"), dict):
components.setdefault("securitySchemes", v2.get("securityDefinitions", {}))
normalized["components"] = components
# servers from host/basePath/schemes
if not normalized.get("servers"):
host = v2.get("host")
base_path = v2.get("basePath", "")
schemes = v2.get("schemes") or ["https"]
servers: List[Dict[str, Any]] = []
if host:
for sch in schemes:
url = f"{sch}://{host}{base_path}"
servers.append({"url": url})
elif base_path:
servers.append({"url": base_path})
if servers:
normalized["servers"] = servers
# paths: map body/formData parameters -> requestBody; map responses.schema -> content
paths = normalized.get("paths") or {}
if isinstance(paths, dict):
for path_item, methods in list(paths.items()):
if not isinstance(methods, dict):
continue
for method, details in list(methods.items()):
if not isinstance(details, dict):
continue
# Request body from parameters (in: body or formData)
params = details.get("parameters", [])
body_schema = None
body_required = False
media_types: List[str] = []
# Derive consumes for this operation (operation consumes overrides global)
op_consumes = details.get("consumes") or v2.get("consumes") or ["application/json"]
if isinstance(op_consumes, list):
media_types = op_consumes
new_params: List[Any] = []
if isinstance(params, list):
for p in params:
if not isinstance(p, dict):
continue
loc = p.get("in")
if loc == "body":
body_schema = p.get("schema")
body_required = bool(p.get("required"))
elif loc == "formData":
# Represent formData as application/x-www-form-urlencoded if no file, else multipart/form-data
mt = "application/x-www-form-urlencoded"
if p.get("type") == "file":
mt = "multipart/form-data"
if mt not in media_types:
media_types.append(mt)
# Synthesize a schema property for this field
if body_schema is None:
body_schema = {"type": "object", "properties": {}, "required": []}
if isinstance(body_schema, dict):
props = body_schema.setdefault("properties", {})
if isinstance(props, dict):
props[p.get("name", "field")] = {k: v for k, v in p.items() if k in ["type", "format", "description", "items"]}
if p.get("required"):
body_schema.setdefault("required", []).append(p.get("name", "field"))
else:
new_params.append(p)
# Write back filtered params
if params != new_params:
details["parameters"] = new_params
# Create requestBody if we found body/formData
if body_schema is not None and "requestBody" not in details:
content_obj: Dict[str, Any] = {}
for mt in media_types or ["application/json"]:
content_obj[mt] = {"schema": body_schema}
details["requestBody"] = {
"required": body_required,
"content": content_obj
}
# Responses: map schema+produces -> content
produces = details.get("produces") or v2.get("produces") or ["application/json"]
responses = details.get("responses", {})
if isinstance(responses, dict):
for code, resp in list(responses.items()):
if not isinstance(resp, dict):
continue
# If content already exists, skip
if "content" in resp:
continue
schema = resp.get("schema")
if schema is not None:
resp["content"] = {mt: {"schema": schema} for mt in produces}
return normalized
def _validate_with_openapi_spec_validator(spec: dict) -> List[str]:
suggestions: List[str] = []
try:
from openapi_spec_validator import validate_spec
validate_spec(spec)
except ImportError:
suggestions.append("Could not import openapi-spec-validator, skipping validation.")
except Exception as e:
suggestions.append(f"Spec validation: {e}")
return suggestions
def set_github_outputs(result: dict):
"""Set GitHub Action outputs."""
def _set(name: str, value: str):
print(f"::set-output name={name}::{value}")
_set("analysis", json.dumps(result))
_set("is_valid", str(result.get("is_valid", False)).lower())
# Calculate total suggestions from grouped suggestions
suggestions = result.get("suggestions", {})
total_suggestions = sum(len(suggestion_list) for suggestion_list in suggestions.values())
_set("suggestions_count", str(total_suggestions))
summary = result.get("summary", {})
_set("operations_count", str(summary.get("operations_count", 0)))
_set("paths_count", str(summary.get("paths_count", 0)))
_set("schemas_count", str(summary.get("schemas_count", 0)))
# Advanced analytics outputs
analytics = result.get("analytics", {})
_set("complexity_score", str(analytics.get("complexity_score", 0)))
_set("maintainability_score", str(analytics.get("maintainability_score", 0)))
# Analysis categories
categories = result.get("analysis_categories", {})
_set("security_issues", str(categories.get("security", 0)))
_set("performance_issues", str(categories.get("performance", 0)))
_set("design_pattern_issues", str(categories.get("design_patterns", 0)))
_set("versioning_issues", str(categories.get("versioning", 0)))
_set("documentation_issues", str(categories.get("documentation", 0)))
_set("compliance_issues", str(categories.get("compliance", 0)))
_set("testing_recommendations", str(categories.get("testing", 0)))
_set("monitoring_recommendations", str(categories.get("monitoring", 0)))
_set("code_generation_opportunities", str(categories.get("code_generation", 0)))
_set("governance_issues", str(categories.get("governance", 0)))
_set("user_actor", os.getenv("GITHUB_ACTOR", ""))
_set("user_repository", os.getenv("GITHUB_REPOSITORY", ""))
_set("user_workflow", os.getenv("GITHUB_WORKFLOW", ""))
_set("user_run_id", os.getenv("GITHUB_RUN_ID", ""))
def send_to_server(result: dict):
"""Send analysis + GitHub metadata to external server if configured."""
server_url = os.getenv("SERVER_URL")
token = os.getenv("SERVER_TOKEN")
if not server_url or not token:
print("Skipping server reporting (SERVER_URL or SERVER_TOKEN not set).")
return
payload = {
"actor": os.getenv("GITHUB_ACTOR"),
"repository": os.getenv("GITHUB_REPOSITORY"),
"commit_sha": os.getenv("GITHUB_SHA"),
"workflow": os.getenv("GITHUB_WORKFLOW"),
"run_id": os.getenv("GITHUB_RUN_ID"),
"result": result,
}
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {token}"
}
try:
resp = requests.post(server_url, headers=headers, json=payload, timeout=20)
resp.raise_for_status()
print("Successfully sent data to server.")
except Exception as e:
print(f"Failed to send data to server: {e}")
# --- Advanced Analysis Functions ---
def analyze_security_enhanced(spec: dict) -> List[str]:
"""Enhanced security analysis with OWASP API Security Top 10 checks."""
suggestions = []
# OWASP API Security Top 10 checks
security_schemes = spec.get("components", {}).get("securitySchemes", {})
global_security = spec.get("security", [])
# API1:2019 - Broken Object Level Authorization
suggestions.extend(_check_broken_object_authorization(spec))
# API2:2019 - Broken User Authentication
suggestions.extend(_check_broken_authentication(security_schemes, global_security))
suggestions.extend(_check_sensitive_endpoints_without_auth(spec))
# API3:2019 - Excessive Data Exposure
suggestions.extend(_check_excessive_data_exposure(spec))
suggestions.extend(_check_hardcoded_secrets(spec))
# API4:2019 - Lack of Resources & Rate Limiting
suggestions.extend(_check_rate_limiting(spec))
suggestions.extend(_check_unvalidated_input_parameters(spec))
# API5:2019 - Broken Function Level Authorization
suggestions.extend(_check_function_level_authorization(spec))
# API6:2019 - Mass Assignment
suggestions.extend(_check_mass_assignment(spec))
# API7:2019 - Security Misconfiguration
suggestions.extend(_check_security_misconfiguration(spec))
# API8:2019 - Injection
suggestions.extend(_check_injection_vulnerabilities(spec))
# API9:2019 - Improper Assets Management
suggestions.extend(_check_asset_management(spec))
# API10:2019 - Insufficient Logging & Monitoring
suggestions.extend(_check_logging_monitoring(spec))
return suggestions
def _check_broken_object_authorization(spec: dict) -> List[str]:
"""Check for broken object level authorization."""
suggestions = []
paths = spec.get("paths", {})
for path, methods in paths.items():
if not isinstance(methods, dict):
continue
for method, details in methods.items():
if not isinstance(details, dict):
continue
# Check for ID-based endpoints without proper authorization
if "{" in path and "id" in path.lower():
if not details.get("security"):
suggestions.append(f"ID-based endpoint {method.upper()} {path} should have explicit security requirements to prevent unauthorized access to other users' data.")
return suggestions
def _check_broken_authentication(security_schemes: dict, global_security: list) -> List[str]:
"""Check for broken authentication mechanisms."""
suggestions = []
if not security_schemes:
suggestions.append("No security schemes defined. Implement proper authentication mechanisms.")
return suggestions
for scheme_name, scheme in security_schemes.items():
if not isinstance(scheme, dict):
continue
scheme_type = scheme.get("type")
if scheme_type == "http":
scheme_name_val = scheme.get("scheme")
if scheme_name_val == "bearer":
if "bearerFormat" not in scheme:
suggestions.append(f"Bearer token scheme '{scheme_name}' should specify bearerFormat (e.g., 'JWT').")
elif scheme_name_val == "basic":
suggestions.append(f"Basic authentication '{scheme_name}' is insecure. Consider using OAuth2 or API keys.")
elif scheme_type == "apiKey":
if scheme.get("in") == "query":
suggestions.append(f"API key '{scheme_name}' in query parameter is insecure. Use header or cookie instead.")
elif scheme_type == "oauth2":
flows = scheme.get("flows", {})
if not flows:
suggestions.append(f"OAuth2 scheme '{scheme_name}' missing flows configuration.")
else:
for flow_name, flow_config in flows.items():
if not flow_config.get("authorizationUrl") and flow_name in ["implicit", "authorizationCode"]:
suggestions.append(f"OAuth2 {flow_name} flow in '{scheme_name}' missing authorizationUrl.")
if not flow_config.get("tokenUrl"):
suggestions.append(f"OAuth2 {flow_name} flow in '{scheme_name}' missing tokenUrl.")
if not global_security:
suggestions.append("No global security requirements defined. Consider adding default authentication.")
return suggestions
def _check_sensitive_endpoints_without_auth(spec: dict) -> List[str]:
"""Flag sensitive or destructive endpoints that lack authentication."""
suggestions = []
global_security = spec.get("security", [])
paths = spec.get("paths", {})
sensitive_keywords = ["user", "account", "profile", "admin", "login", "token", "password", "deleteall", "reset"]
destructive_methods = {"delete"}
for path, methods in paths.items():
if not isinstance(methods, dict):
continue
for method, details in methods.items():
if not isinstance(details, dict):
continue
method_lower = method.lower()
if method_lower not in ["get", "post", "put", "delete", "patch", "options", "head", "trace"]:
continue
op_security = details.get("security")
has_security = bool(global_security) if op_security is None else bool(op_security)
if has_security:
continue
path_text = path.lower()
description_text = f"{details.get('summary', '')} {details.get('description', '')}".lower()
is_sensitive_route = any(keyword in path_text for keyword in sensitive_keywords) or any(
keyword in description_text for keyword in sensitive_keywords
)
responses = details.get("responses", {})
exposes_sensitive_data = False
for response in responses.values():
if not isinstance(response, dict):
continue
content = response.get("content", {})
for content_spec in content.values():
if not isinstance(content_spec, dict):
continue
schema = content_spec.get("schema")
if schema and isinstance(schema, dict):
if _find_sensitive_fields(schema):
exposes_sensitive_data = True
break
if exposes_sensitive_data:
break
is_destructive = method_lower in destructive_methods or "delete all" in description_text
if is_sensitive_route or exposes_sensitive_data or is_destructive:
reason_parts = []
if is_sensitive_route:
reason_parts.append("is a sensitive endpoint")
if exposes_sensitive_data:
reason_parts.append("returns sensitive data")
if is_destructive:
reason_parts.append("performs destructive operations")
reason_text = " and ".join(reason_parts) if reason_parts else "is sensitive"
suggestions.append(f"{method.upper()} {path} {reason_text} but has no security requirements defined.")
return suggestions
def _check_excessive_data_exposure(spec: dict) -> List[str]:
"""Check for excessive data exposure in responses."""
suggestions = []
paths = spec.get("paths", {})
for path, methods in paths.items():
if not isinstance(methods, dict):
continue
for method, details in methods.items():
if not isinstance(details, dict):
continue
responses = details.get("responses", {})
for code, response in responses.items():
if not isinstance(response, dict):
continue
content = response.get("content", {})
for content_type, content_spec in content.items():
if not isinstance(content_spec, dict):
continue
schema = content_spec.get("schema")
if schema and isinstance(schema, dict):
# Check for sensitive fields
sensitive_fields = _find_sensitive_fields(schema)
if sensitive_fields:
suggestions.append(f"Response {code} in {method.upper()} {path} may expose sensitive fields: {', '.join(sensitive_fields)}. Consider filtering or using separate schemas.")
return suggestions
def _find_sensitive_fields(schema: dict, path: str = "") -> List[str]:
"""Recursively find potentially sensitive fields in schema."""
sensitive_patterns = [
r'password', r'secret', r'key', r'token', r'auth', r'credential',
r'ssn', r'social', r'credit', r'card', r'bank', r'account',
r'email', r'phone', r'address', r'personal', r'private'
]
sensitive_fields = []
if isinstance(schema, dict):
properties = schema.get("properties", {})
for prop_name, prop_schema in properties.items():
current_path = f"{path}.{prop_name}" if path else prop_name
# Check if field name matches sensitive patterns
for pattern in sensitive_patterns:
if re.search(pattern, prop_name, re.IGNORECASE):
sensitive_fields.append(current_path)
# Recursively check nested objects
if isinstance(prop_schema, dict):
sensitive_fields.extend(_find_sensitive_fields(prop_schema, current_path))
return sensitive_fields
def _check_rate_limiting(spec: dict) -> List[str]:
"""Check for rate limiting implementation."""
suggestions = []
paths = spec.get("paths", {})
rate_limit_extension = spec.get("x-rate-limit")
if isinstance(rate_limit_extension, str) and rate_limit_extension.lower() in ["none", "unlimited", "disabled"]:
suggestions.append("Global x-rate-limit extension indicates rate limiting is disabled. Implement rate limiting to prevent abuse.")
elif isinstance(rate_limit_extension, (int, float)) and rate_limit_extension <= 0:
suggestions.append("Global x-rate-limit value is non-positive, effectively disabling rate limiting. Configure a realistic limit.")
has_rate_limiting = False
for path, methods in paths.items():
if not isinstance(methods, dict):
continue
for method, details in methods.items():
if not isinstance(details, dict):
continue
responses = details.get("responses", {})
for code, response in responses.items():
if not isinstance(response, dict):
continue
headers = response.get("headers", {})
for header_name in headers.keys():
if any(keyword in header_name.lower() for keyword in ["rate", "limit", "quota", "throttle"]):
has_rate_limiting = True
break
if not has_rate_limiting:
suggestions.append("No rate limiting headers found. Implement rate limiting to prevent abuse and ensure fair usage.")
return suggestions
def _check_hardcoded_secrets(spec: dict) -> List[str]:
"""Detect hardcoded tokens or secrets in examples."""
suggestions = []
sensitive_keywords = ["token", "secret", "password", "apikey", "api_key", "credential"]
suspicious_markers = ["hardcoded", "changeme", "static", "dummy", "sample"]
def example_is_suspicious(value: str, context: str) -> bool:
lowered_value = value.lower()
if not any(marker in lowered_value for marker in suspicious_markers):
return False
lowered_context = context.lower()
return any(keyword in lowered_context for keyword in sensitive_keywords)
def scan_schema(schema: dict, context: str):
if not isinstance(schema, dict):
return
example = schema.get("example")
if isinstance(example, str) and example_is_suspicious(example, context):
suggestions.append(f"{context} uses a hardcoded example value '{example}'. Replace with a placeholder to avoid leaking secrets.")
default = schema.get("default")
if isinstance(default, str) and example_is_suspicious(default, context):
suggestions.append(f"{context} uses a hardcoded default value '{default}'. Avoid embedding secrets in specifications.")
examples = schema.get("examples", {})
if isinstance(examples, dict):
for ex_name, ex_value in examples.items():
if isinstance(ex_value, dict):
value = ex_value.get("value")
if isinstance(value, str) and example_is_suspicious(value, context):
suggestions.append(f"{context} example '{ex_name}' contains a hardcoded secret-like value '{value}'.")
properties = schema.get("properties", {})
if isinstance(properties, dict):
for prop_name, prop_schema in properties.items():
scan_schema(prop_schema, f"{context}.{prop_name}")
items = schema.get("items")
if items:
scan_schema(items, f"{context}.items")
paths = spec.get("paths", {})
for path, methods in paths.items():
if not isinstance(methods, dict):
continue
for method, details in methods.items():
if not isinstance(details, dict):
continue
request_body = details.get("requestBody", {})
if isinstance(request_body, dict):
content = request_body.get("content", {})
for ctype, content_spec in content.items():
if not isinstance(content_spec, dict):
continue
schema = content_spec.get("schema")
if schema:
scan_schema(schema, f"{method.upper()} {path} requestBody {ctype}")
responses = details.get("responses", {})
for code, response in responses.items():
if not isinstance(response, dict):
continue
content = response.get("content", {})
for ctype, content_spec in content.items():
if not isinstance(content_spec, dict):
continue
schema = content_spec.get("schema")
if schema:
scan_schema(schema, f"{method.upper()} {path} response {code} {ctype}")
return suggestions
def _check_unvalidated_input_parameters(spec: dict) -> List[str]:
"""Detect parameters that allow unvalidated or weakly validated input."""
suggestions = []
paths = spec.get("paths", {})
wildcard_patterns = {".*", "^.*$", "^.+$", ".+"}
for path, methods in paths.items():
if not isinstance(methods, dict):
continue
for method, details in methods.items():
if not isinstance(details, dict):
continue
parameters = details.get("parameters", [])
if not isinstance(parameters, list):
continue
for param in parameters:
if not isinstance(param, dict):
continue
schema = param.get("schema") or {}
if not isinstance(schema, dict):
schema = {}
param_name = param.get("name", "unnamed")
param_in = param.get("in", "unknown")
param_type = schema.get("type")
pattern = schema.get("pattern")
if isinstance(pattern, str) and pattern.strip() in wildcard_patterns:
suggestions.append(f"Parameter '{param_name}' in {param_in} for {method.upper()} {path} uses overly permissive pattern '{pattern}'. Provide a stricter pattern or validation rules.")
if param_name.lower().endswith("id"):
if param_type and param_type != "integer":
suggestions.append(f"Parameter '{param_name}' in {param_in} for {method.upper()} {path} should be an integer but is defined as {param_type}.")
elif not param_type:
suggestions.append(f"Parameter '{param_name}' in {param_in} for {method.upper()} {path} is missing a type declaration.")
if param_in == "path" and param_type == "string":
has_constraints = any(schema.get(key) is not None for key in ["pattern", "format", "minLength", "maxLength", "enum"])
if not has_constraints:
suggestions.append(f"Path parameter '{param_name}' in {method.upper()} {path} is a free-form string without validation constraints. Add pattern, format, or length restrictions.")
return suggestions
def _check_function_level_authorization(spec: dict) -> List[str]:
"""Check for function level authorization."""
suggestions = []
paths = spec.get("paths", {})
for path, methods in paths.items():
if not isinstance(methods, dict):
continue
for method, details in methods.items():
if not isinstance(details, dict):
continue
# Check for admin/privileged operations
admin_keywords = ["admin", "delete", "update", "create", "manage", "config"]
if any(keyword in path.lower() or keyword in method.lower() for keyword in admin_keywords):
if not details.get("security"):
suggestions.append(f"Privileged operation {method.upper()} {path} should have explicit security requirements.")
return suggestions
def _check_mass_assignment(spec: dict) -> List[str]:
"""Check for mass assignment vulnerabilities."""
suggestions = []
paths = spec.get("paths", {})
for path, methods in paths.items():
if not isinstance(methods, dict):
continue
for method, details in methods.items():
if not isinstance(details, dict):
continue
if method.lower() in ["post", "put", "patch"]:
request_body = details.get("requestBody", {})
if isinstance(request_body, dict):
content = request_body.get("content", {})
for content_type, content_spec in content.items():
if isinstance(content_spec, dict):
schema = content_spec.get("schema")
if schema and isinstance(schema, dict):
# Check for overly permissive schemas
if not schema.get("additionalProperties", True) is False:
suggestions.append(f"Request body for {method.upper()} {path} allows additional properties. Consider restricting to prevent mass assignment attacks.")
return suggestions
def _check_security_misconfiguration(spec: dict) -> List[str]:
"""Check for security misconfigurations."""
suggestions = []
# Check for HTTP instead of HTTPS
servers = spec.get("servers", [])
for server in servers:
if isinstance(server, dict):
url = server.get("url", "")
if url.startswith("http://") and not url.startswith("http://localhost"):
suggestions.append(f"Server URL '{url}' uses HTTP instead of HTTPS. This is insecure for production APIs.")
# Check for overly permissive CORS
if "x-cors" in spec:
cors_config = spec["x-cors"]
if isinstance(cors_config, dict):
if cors_config.get("allowOrigin") == "*":
suggestions.append("CORS configuration allows all origins (*). Consider restricting to specific domains.")
return suggestions
def _check_injection_vulnerabilities(spec: dict) -> List[str]:
"""Check for potential injection vulnerabilities."""
suggestions = []
paths = spec.get("paths", {})
for path, methods in paths.items():
if not isinstance(methods, dict):
continue
for method, details in methods.items():
if not isinstance(details, dict):
continue
parameters = details.get("parameters", [])
for param in parameters:
if not isinstance(param, dict):
continue
param_name = param.get("name", "")
param_type = param.get("schema", {}).get("type", "")
# Check for SQL injection patterns
if any(keyword in param_name.lower() for keyword in ["query", "sql", "search", "filter"]):
if param_type == "string":
suggestions.append(f"Parameter '{param_name}' in {method.upper()} {path} appears to accept SQL-like queries. Ensure proper input validation and parameterized queries.")
return suggestions
def _check_asset_management(spec: dict) -> List[str]:
"""Check for improper asset management."""
suggestions = []
# Check for version information
info = spec.get("info", {})
version = info.get("version")
if not version:
suggestions.append("API version not specified. Proper versioning is crucial for asset management.")
# Check for deprecated operations
paths = spec.get("paths", {})
deprecated_ops = []
for path, methods in paths.items():
if not isinstance(methods, dict):
continue
for method, details in methods.items():
if not isinstance(details, dict):
continue
if details.get("deprecated"):
deprecated_ops.append(f"{method.upper()} {path}")
if deprecated_ops:
suggestions.append(f"Deprecated operations found: {', '.join(deprecated_ops)}. Ensure proper deprecation timeline and migration path.")
return suggestions
def _check_logging_monitoring(spec: dict) -> List[str]:
"""Check for logging and monitoring capabilities."""
suggestions = []
# Check for health check endpoints
paths = spec.get("paths", {})
has_health_check = False
for path in paths.keys():
if any(keyword in path.lower() for keyword in ["health", "status", "ping", "ready", "live"]):
has_health_check = True
break
if not has_health_check:
suggestions.append("No health check endpoint found. Add /health or /status endpoint for monitoring.")
# Check for proper error responses
has_proper_errors = False
for path, methods in paths.items():
if not isinstance(methods, dict):
continue
for method, details in methods.items():
if not isinstance(details, dict):
continue
responses = details.get("responses", {})
if "500" in responses or "4xx" in responses:
has_proper_errors = True
break
if not has_proper_errors:
suggestions.append("Missing proper error response definitions. Add 4xx and 5xx error responses for better monitoring.")
return suggestions
def analyze_performance(spec: dict) -> List[str]:
"""Analyze API performance characteristics."""
suggestions = []
paths = spec.get("paths", {})
# Analyze response complexity
for path, methods in paths.items():
if not isinstance(methods, dict):
continue
for method, details in methods.items():
if not isinstance(details, dict):