forked from omnomdombomb/license_uploader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
1049 lines (845 loc) · 36.7 KB
/
Copy pathapp.py
File metadata and controls
1049 lines (845 loc) · 36.7 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
"""
License Uploader Flask Application
"""
import os
import json
import tempfile
import time
import logging
from logging.handlers import RotatingFileHandler
from datetime import datetime
from pathlib import Path
from flask import Flask, render_template, request, jsonify, session, redirect, url_for
from flask_wtf.csrf import CSRFProtect, generate_csrf
from flask_talisman import Talisman
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
from werkzeug.utils import secure_filename
from config import Config
from document_parser import DocumentParser
from llm_extractor import LLMExtractor
from alma_api import AlmaAPI
from license_terms_data import LICENSE_TERMS
try:
import magic
MAGIC_AVAILABLE = True
except ImportError:
MAGIC_AVAILABLE = False
import logging
logging.warning("⚠️ WARNING: python-magic not installed. File content validation disabled!")
logging.info(" Install with: pip install python-magic")
app = Flask(__name__)
app.config.from_object(Config)
Config.init_app(app)
# Enable CSRF protection
csrf = CSRFProtect(app)
# Enable HTTPS enforcement and security headers (in production only)
if not app.debug and os.getenv('FLASK_ENV') != 'development':
talisman = Talisman(
app,
force_https=True,
strict_transport_security=True,
strict_transport_security_max_age=31536000, # 1 year
strict_transport_security_include_subdomains=True,
content_security_policy={
'default-src': "'self'",
'script-src': ["'self'", 'cdn.jsdelivr.net'],
'style-src': ["'self'", 'cdn.jsdelivr.net', "'unsafe-inline'"],
'img-src': ["'self'", 'data:'],
'font-src': ["'self'", 'cdn.jsdelivr.net'],
'connect-src': ["'self'"],
'frame-ancestors': "'none'"
},
content_security_policy_nonce_in=['script-src'],
feature_policy={
'geolocation': "'none'",
'camera': "'none'",
'microphone': "'none'"
}
)
app.logger.info("HTTPS enforcement enabled (Talisman)")
else:
app.logger.warning("Running in development mode - HTTPS enforcement disabled")
# Enable rate limiting
limiter = Limiter(
app=app,
key_func=get_remote_address,
default_limits=["200 per day", "50 per hour"],
storage_uri=Config.RATE_LIMIT_STORAGE_URI,
headers_enabled=True
)
app.logger.info(f"Rate limiting enabled (storage: {Config.RATE_LIMIT_STORAGE_URI})")
# Configure logging
def setup_logging():
"""Configure application logging with file rotation"""
# Create logs directory if it doesn't exist
log_dir = Path('logs')
log_dir.mkdir(exist_ok=True, parents=True)
# Configure logging format
log_format = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - [%(request_id)s] - %(message)s'
)
# Add filter to provide default request_id for non-request logs
class RequestIdFilter(logging.Filter):
def filter(self, record):
if not hasattr(record, 'request_id'):
# Try to get request_id from Flask request context
try:
from flask import request
if hasattr(request, 'request_id'):
record.request_id = request.request_id
else:
record.request_id = 'system'
except (RuntimeError, ImportError):
# No request context available
record.request_id = 'system'
return True
request_id_filter = RequestIdFilter()
# File handler with rotation (10MB max, keep 5 backups)
file_handler = RotatingFileHandler(
log_dir / 'license_uploader.log',
maxBytes=10 * 1024 * 1024, # 10MB
backupCount=5
)
file_handler.setLevel(logging.INFO)
file_handler.setFormatter(log_format)
file_handler.addFilter(request_id_filter)
# Console handler for development
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG if app.debug else logging.INFO)
console_handler.setFormatter(log_format)
console_handler.addFilter(request_id_filter)
# Configure root logger
app.logger.setLevel(logging.INFO)
app.logger.addHandler(file_handler)
app.logger.addHandler(console_handler)
# Log startup
app.logger.info('License Uploader application starting')
setup_logging()
# Add request ID to logs for tracking
@app.before_request
def before_request():
"""Add request ID to each request for logging"""
request.request_id = os.urandom(4).hex()
def cleanup_old_temp_files():
"""Clean up temporary session files older than 24 hours"""
temp_dir = Path(tempfile.gettempdir())
current_time = time.time()
max_age = 24 * 60 * 60 # 24 hours in seconds
# Use pathlib's glob instead of glob.glob
for filepath in temp_dir.glob('license_upload_*.json'):
try:
file_age = current_time - filepath.stat().st_mtime
if file_age > max_age:
filepath.unlink() # pathlib's delete method
except (OSError, PermissionError) as e:
# Log but don't fail - cleanup is best-effort
app.logger.debug(f"Could not clean up temp file {filepath}: {e}")
# Clean up old temp files on startup
cleanup_old_temp_files()
def allowed_file(filename):
"""Check if file extension is allowed"""
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in Config.ALLOWED_EXTENSIONS
# Allowed MIME types for file validation
ALLOWED_MIME_TYPES = {
'application/pdf',
'application/msword', # .doc (legacy)
'application/vnd.openxmlformats-officedocument.wordprocessingml.document', # .docx
'text/plain',
'application/octet-stream' # Sometimes used for DOCX files
}
def validate_file_content(filepath):
"""
Validate file content using magic numbers (MIME type detection)
Args:
filepath: Path to the file to validate
Returns:
tuple: (is_valid, error_message)
Raises:
None - returns (False, error) instead
"""
if not MAGIC_AVAILABLE:
# If python-magic is not available, skip validation
return True, None
try:
# Detect MIME type from file content
mime = magic.from_file(filepath, mime=True)
if mime not in ALLOWED_MIME_TYPES:
return False, f'Invalid file type detected: {mime}. Only PDF, DOCX, and TXT files are allowed.'
# Additional check for file size
file_size = Path(filepath).stat().st_size
if file_size > Config.MAX_CONTENT_LENGTH:
return False, f'File too large ({file_size} bytes). Maximum size is 16MB.'
return True, None
except Exception as e:
# If validation fails, log but don't block
app.logger.warning(f'File content validation error: {e}')
return True, None # Allow upload but log warning
def validate_session_file_path(session_file_path):
"""
Validate session file path to prevent path traversal attacks (VULN-010)
Args:
session_file_path: Path from session cookie
Returns:
Path: Validated absolute Path object
Raises:
ValueError: If path is invalid or outside allowed directory
"""
if not session_file_path:
raise ValueError("No session file specified")
try:
# Convert to Path object and resolve to absolute path
file_path = Path(session_file_path).resolve()
# Get temp directory as Path object
temp_dir = Path(tempfile.gettempdir()).resolve()
# Ensure path is within temp directory (prevent path traversal)
if not str(file_path).startswith(str(temp_dir)):
app.logger.warning(f"Path traversal attempt detected: {session_file_path}")
raise ValueError("Invalid session file path")
# Check filename pattern (additional security layer)
if not file_path.name.startswith('license_upload_'):
app.logger.warning(f"Invalid session file pattern: {file_path.name}")
raise ValueError("Invalid session file format")
# Check file exists
if not file_path.exists():
raise ValueError("Session file not found")
# Check file is a regular file (not symlink, device, etc.)
if not file_path.is_file():
raise ValueError("Session file is not a regular file")
return file_path
except (ValueError, OSError) as e:
# Re-raise ValueError, convert OSError to ValueError
if isinstance(e, ValueError):
raise
raise ValueError(f"Invalid session file path: {str(e)}")
@app.context_processor
def inject_asset_version():
"""Expose a static-asset cache-buster based on main.js mtime.
Prevents stale JS on clients (e.g. iOS Safari) after deploys.
"""
try:
mtime = int(Path(app.static_folder, 'js', 'main.js').stat().st_mtime)
except Exception:
mtime = 0
return {'asset_version': mtime}
@app.route('/')
def index():
"""Home page"""
return render_template('index.html')
@app.route('/health')
def health_check():
"""
Health check endpoint for monitoring and load balancers
Returns basic system status and timestamp
"""
from datetime import datetime
return jsonify({
'status': 'healthy',
'timestamp': datetime.now().isoformat(),
'version': '1.0.0',
'service': 'license_uploader'
})
@app.route('/api/models', methods=['GET'])
def list_models():
"""Return the live list of models available from the LiteLLM proxy.
Uses the session-saved LiteLLM key if present, otherwise falls back to
the env-configured key. Keeps the UI in sync when models are renamed.
"""
try:
session_cfg = session.get('api_config', {}) or {}
api_key = session_cfg.get('litellm_api_key') or Config.LITELLM_API_KEY
models = LLMExtractor.list_available_models(api_key=api_key)
configured = session_cfg.get('llm_model') or Config.LITELLM_MODEL
return jsonify({
'success': True,
'models': models,
'configured_model': configured,
'configured_model_available': configured in models,
})
except Exception as e:
app.logger.warning(f"Failed to list models from LiteLLM: {e}")
return jsonify({'success': False, 'error': str(e), 'models': []}), 502
@app.route('/api/csrf-token', methods=['GET'])
def get_csrf_token():
"""
Get CSRF token for AJAX requests
"""
return jsonify({'csrf_token': generate_csrf()})
@app.route('/api/config', methods=['POST'])
def save_api_config():
"""
Save API configuration in server-side session (not localStorage)
Security: Keeps sensitive API keys server-side instead of in browser
"""
data = request.json
if not data:
return jsonify({'error': 'No configuration provided'}), 400
# Store in session (server-side, httpOnly cookies)
session['api_config'] = {
'litellm_api_key': data.get('litellm_api_key', ''),
'alma_api_key': data.get('alma_api_key', ''),
'llm_model': data.get('llm_model', 'gpt-5')
}
session.permanent = False # Session expires when browser closes
app.logger.info('API configuration saved to session')
return jsonify({'success': True})
@app.route('/api/config', methods=['GET'])
def get_api_config():
"""
Retrieve API configuration from server-side session
Returns empty strings if not set
"""
config = session.get('api_config', {})
return jsonify({
'litellm_api_key': config.get('litellm_api_key', ''),
'alma_api_key': config.get('alma_api_key', ''),
'llm_model': config.get('llm_model', 'gpt-5')
})
@app.route('/upload', methods=['POST'])
@limiter.limit("10 per hour") # Strict limit: file processing + LLM API call
def upload_file():
"""Handle file upload and extraction"""
app.logger.info(f'File upload initiated from {request.remote_addr}')
if 'file' not in request.files:
app.logger.warning('Upload request missing file')
return jsonify({'error': 'No file uploaded'}), 400
file = request.files['file']
if file.filename == '':
app.logger.warning('Upload request with empty filename')
return jsonify({'error': 'No file selected'}), 400
if not allowed_file(file.filename):
app.logger.warning(f'Invalid file type attempted: {file.filename}')
return jsonify({'error': 'Invalid file type'}), 400
app.logger.info(f'Processing file: {file.filename}')
try:
# Save file
filename = secure_filename(file.filename)
filepath = Path(Config.UPLOAD_FOLDER) / filename
file.save(str(filepath))
# Check if file has content
if filepath.stat().st_size == 0:
filepath.unlink()
return jsonify({'error': 'Uploaded file is empty. Please upload a file with content.'}), 400
# Validate file content using magic numbers
is_valid, error_message = validate_file_content(str(filepath))
if not is_valid:
filepath.unlink()
app.logger.warning(f'File content validation failed: {error_message}')
return jsonify({'error': error_message}), 400
# Parse document with location tracking
parser = DocumentParser(str(filepath))
document_text = parser.parse_file()
# Check if parsed text has content
if not document_text or document_text.strip() == '':
filepath.unlink()
return jsonify({'error': 'No text content could be extracted from the file. Please check the file format.'}), 400
# Store data in temporary files instead of session
temp_dir = Path(tempfile.gettempdir())
session_id = os.urandom(16).hex()
data_file = temp_dir / f'license_upload_{session_id}.json'
# Get API configuration from server session (not headers/localStorage)
api_config = session.get('api_config', {})
litellm_api_key = api_config.get('litellm_api_key')
llm_model = api_config.get('llm_model')
# Extract terms using LLM (citation validation feature disabled)
extractor = LLMExtractor(
api_key=litellm_api_key if litellm_api_key else None,
model=llm_model if llm_model else None
)
# Note: document_parser parameter is no longer used for citation validation
extraction_result = extractor.extract_license_terms(document_text, document_parser=parser)
# Handle new format with truncation warning
extracted_terms = extraction_result.get('terms', extraction_result)
truncation_warning = extraction_result.get('truncation_warning')
# Save to file with explicit UTF-8 encoding
with open(data_file, 'w', encoding='utf-8') as f:
json.dump({
'document_text': document_text,
'filename': filename,
'extracted_terms': extracted_terms,
'truncation_warning': truncation_warning
}, f, ensure_ascii=False)
# Store only the session ID in session
session['data_file'] = str(data_file)
session['filename'] = filename
# Clean up uploaded file
filepath.unlink()
response_data = {
'success': True,
'message': 'Document processed successfully',
'redirect': url_for('review')
}
# Add truncation warning to response if present
if truncation_warning:
response_data['warning'] = truncation_warning['message']
return jsonify(response_data)
except Exception as e:
# Clean up temp file on error
if 'data_file' in locals():
try:
Path(data_file).unlink(missing_ok=True)
except Exception as cleanup_error:
app.logger.debug(f"Could not clean up temp file: {cleanup_error}")
# Clean up uploaded file on error
if 'filepath' in locals():
try:
Path(filepath).unlink(missing_ok=True)
except Exception as cleanup_error:
app.logger.debug(f"Could not clean up uploaded file: {cleanup_error}")
return jsonify({'error': str(e)}), 500
@app.route('/review')
def review():
"""Review and edit extracted terms"""
if 'data_file' not in session:
return redirect(url_for('index'))
# Load data from file with path traversal protection
try:
# Validate session file path (prevent path traversal - VULN-010)
file_path = validate_session_file_path(session['data_file'])
with open(file_path, 'r', encoding='utf-8') as f:
data = json.load(f)
extracted_terms = data.get('extracted_terms', {})
filename = data.get('filename', 'Unknown')
truncation_warning = data.get('truncation_warning')
except ValueError as e:
# Path validation error - potential security issue
app.logger.warning(f"Invalid session file access attempt from {request.remote_addr}: {e}")
session.clear()
return render_template('index.html',
error='Session expired or invalid. Please upload your document again.')
except FileNotFoundError:
# Handle case where file was deleted
app.logger.warning(f"Session file not found for {request.remote_addr}")
session.clear()
return render_template('index.html',
error='Session expired or data lost. Please upload your document again.')
except Exception as e:
app.logger.error(f"Error loading session data: {e}", exc_info=True)
session.clear()
return render_template('index.html',
error='Error loading session data. Please upload your document again.')
# Get vendors from Alma
try:
# Get custom API configuration from headers if available
api_config = session.get('api_config', {})
alma_api_key = api_config.get('alma_api_key')
alma = AlmaAPI(api_key=alma_api_key if alma_api_key else None)
vendors = alma.get_vendors()
except Exception as e:
app.logger.error(f"Error fetching vendors: {e}", exc_info=True)
vendors = []
return render_template(
'review.html',
extracted_terms=extracted_terms,
vendors=vendors,
filename=filename,
truncation_warning=truncation_warning
)
@app.route('/refine-term', methods=['POST'])
@limiter.limit("30 per hour") # Moderate limit: LLM API call
def refine_term():
"""Refine a specific term using LLM"""
data = request.json
term_code = data.get('term_code')
current_value = data.get('current_value')
if not term_code:
return jsonify({'error': 'Term code required'}), 400
if 'data_file' not in session:
return jsonify({'error': 'No document in session'}), 400
try:
# Validate session file path (prevent path traversal - VULN-010)
file_path = validate_session_file_path(session['data_file'])
# Load document text from file
with open(file_path, 'r', encoding='utf-8') as f:
data_stored = json.load(f)
document_text = data_stored.get('document_text')
if not document_text:
return jsonify({'error': 'No document text found'}), 400
# Get custom API configuration from server session
api_config = session.get('api_config', {})
litellm_api_key = api_config.get('litellm_api_key')
llm_model = api_config.get('llm_model')
extractor = LLMExtractor(
api_key=litellm_api_key if litellm_api_key else None,
model=llm_model if llm_model else None
)
refined_value = extractor.refine_term(document_text, term_code, current_value)
return jsonify({
'success': True,
'value': refined_value
})
except ValueError as e:
# Path validation error
app.logger.warning(f"Invalid session file access in refine-term from {request.remote_addr}: {e}")
return jsonify({'error': 'Session invalid. Please upload your document again.'}), 400
except Exception as e:
app.logger.error(f"Error refining term: {e}", exc_info=True)
return jsonify({'error': str(e)}), 500
def sanitize_license_field(value, max_length=255, allow_special=False):
"""
Sanitize license field input
Args:
value: Input value to sanitize
max_length: Maximum allowed length
allow_special: Whether to allow special characters
Returns:
Sanitized string
"""
if not value:
return value
# Convert to string and strip whitespace
sanitized = str(value).strip()
# Enforce maximum length
if len(sanitized) > max_length:
sanitized = sanitized[:max_length]
# Remove potentially dangerous characters if not allowing special chars
if not allow_special:
# Remove HTML/script tags and other dangerous characters
dangerous_chars = ['<', '>', '{', '}', '\\', '`']
for char in dangerous_chars:
sanitized = sanitized.replace(char, '')
return sanitized
@app.route('/submit-license', methods=['POST'])
@limiter.limit("20 per hour") # Moderate limit: Alma API write operation
def submit_license():
"""Submit license to Alma"""
data = request.json
try:
# Get custom API configuration from server session
api_config = session.get('api_config', {})
alma_api_key = api_config.get('alma_api_key')
# Get and sanitize basic info
basic_info = data.get('basic_info', {})
# Validate and sanitize required fields
if 'code' not in basic_info or not basic_info['code']:
return jsonify({'error': 'License code is required'}), 400
if 'name' not in basic_info or not basic_info['name']:
return jsonify({'error': 'License name is required'}), 400
if 'start_date' not in basic_info or not basic_info['start_date']:
return jsonify({'error': 'Start date is required'}), 400
# Sanitize all text fields
basic_info['code'] = sanitize_license_field(basic_info.get('code'), max_length=50)
basic_info['name'] = sanitize_license_field(basic_info.get('name'), max_length=255)
if 'vendor_code' in basic_info:
basic_info['vendor_code'] = sanitize_license_field(basic_info['vendor_code'], max_length=50)
# Validate dates
start_date = basic_info.get('start_date')
end_date = basic_info.get('end_date')
try:
# Parse start date (required)
start_dt = datetime.fromisoformat(start_date.replace('Z', '+00:00'))
basic_info['start_date'] = start_dt.strftime('%Y-%m-%d')
# Parse end date if present
if end_date:
end_dt = datetime.fromisoformat(end_date.replace('Z', '+00:00'))
basic_info['end_date'] = end_dt.strftime('%Y-%m-%d')
# Validate that end_date is after start_date
if end_dt <= start_dt:
return jsonify({'error': 'End date must be after start date'}), 400
except (ValueError, AttributeError) as e:
return jsonify({'error': f'Invalid date format. Please use YYYY-MM-DD format.'}), 400
# Build license payload
alma = AlmaAPI(api_key=alma_api_key if alma_api_key else None)
app.logger.info(f"Submitting license with info: {json.dumps(basic_info, indent=2)}")
# DEBUG: Log received terms data
terms_data = data.get('terms')
app.logger.debug("="*80)
app.logger.debug("Received terms data from request:")
app.logger.debug(json.dumps(terms_data, indent=2))
app.logger.debug("="*80)
license_payload = alma.build_license_payload(
basic_info=basic_info,
extracted_terms=terms_data
)
# DEBUG: Log the final Alma API license payload
app.logger.debug("="*80)
app.logger.debug("Final Alma API License Payload Being Sent:")
app.logger.debug(json.dumps(license_payload, indent=2))
app.logger.debug("="*80)
app.logger.debug(f"License payload: {json.dumps(license_payload, indent=2)}")
# Create license in Alma
app.logger.info(f'Creating license in Alma: {basic_info.get("code")}')
result = alma.create_license(license_payload)
app.logger.info(f'License created successfully: {result.get("code")}')
# Clean up temporary file and clear session
if 'data_file' in session:
try:
# Validate path before deletion (prevent path traversal)
file_path = validate_session_file_path(session['data_file'])
file_path.unlink(missing_ok=True)
app.logger.info('Cleaned up temporary session file')
except ValueError as e:
# Path validation error - potential security issue
app.logger.warning(f'Invalid session file path during cleanup: {e}')
except Exception as e:
app.logger.warning(f'Failed to clean up temp file: {e}')
session.clear()
return jsonify({
'success': True,
'message': 'License created successfully',
'license_code': result.get('code')
})
except Exception as e:
app.logger.error(f"Error submitting license: {str(e)}", exc_info=True)
return jsonify({'error': str(e)}), 500
@app.route('/test-connection')
def test_connection():
"""Test Alma API connection"""
try:
# Get custom API configuration from headers if available
api_config = session.get('api_config', {})
alma_api_key = api_config.get('alma_api_key')
alma = AlmaAPI(api_key=alma_api_key if alma_api_key else None)
if alma.test_connection():
return jsonify({'success': True, 'message': 'Connection successful'})
else:
return jsonify({'success': False, 'message': 'Connection failed'}), 500
except Exception as e:
return jsonify({'success': False, 'message': str(e)}), 500
@app.route('/get-vendors')
def get_vendors():
"""Get list of vendors from Alma"""
try:
# Get custom API configuration from headers if available
api_config = session.get('api_config', {})
alma_api_key = api_config.get('alma_api_key')
alma = AlmaAPI(api_key=alma_api_key if alma_api_key else None)
vendors = alma.get_vendors()
return jsonify({'success': True, 'vendors': vendors})
except Exception as e:
return jsonify({'success': False, 'error': str(e)}), 500
@app.route('/api/prompt', methods=['GET'])
def get_prompt():
"""Get the current extraction prompt template"""
try:
# Try to load custom prompt
custom_prompt = LLMExtractor.load_custom_prompt()
if custom_prompt:
return jsonify({
'success': True,
'prompt': custom_prompt,
'is_custom': True
})
else:
# Return default prompt
return jsonify({
'success': True,
'prompt': LLMExtractor.get_default_prompt_template(),
'is_custom': False
})
except Exception as e:
app.logger.error(f"Error getting prompt: {e}", exc_info=True)
return jsonify({'success': False, 'error': str(e)}), 500
@app.route('/api/prompt', methods=['POST'])
@limiter.limit("10 per hour") # Limit prompt updates
def update_prompt():
"""Update the extraction prompt template"""
try:
data = request.json
prompt_text = data.get('prompt', '').strip()
if not prompt_text:
return jsonify({'success': False, 'error': 'Prompt text is required'}), 400
# Validate that the prompt contains required placeholders
required_placeholders = ['{terms_description}', '{document_text}']
missing_placeholders = [p for p in required_placeholders if p not in prompt_text]
if missing_placeholders:
return jsonify({
'success': False,
'error': f'Prompt must contain placeholders: {", ".join(missing_placeholders)}'
}), 400
# Save the custom prompt
LLMExtractor.save_custom_prompt(prompt_text)
app.logger.info('Custom extraction prompt updated')
return jsonify({
'success': True,
'message': 'Prompt updated successfully'
})
except Exception as e:
app.logger.error(f"Error updating prompt: {e}", exc_info=True)
return jsonify({'success': False, 'error': str(e)}), 500
@app.route('/api/prompt', methods=['DELETE'])
@limiter.limit("10 per hour")
def reset_prompt():
"""Reset to default prompt by deleting custom prompt file"""
try:
prompt_file = Path(LLMExtractor.CUSTOM_PROMPT_FILE)
if prompt_file.exists():
prompt_file.unlink()
app.logger.info('Custom extraction prompt reset to default')
return jsonify({
'success': True,
'message': 'Prompt reset to default'
})
except Exception as e:
app.logger.error(f"Error resetting prompt: {e}", exc_info=True)
return jsonify({'success': False, 'error': str(e)}), 500
# ============================================================================
# TERM DESCRIPTIONS API
# ============================================================================
@app.route('/api/term-descriptions', methods=['GET'])
def get_term_descriptions():
"""Get all term descriptions (defaults merged with custom overrides)"""
try:
descriptions = LLMExtractor.get_effective_descriptions()
return jsonify({
'success': True,
'descriptions': descriptions
})
except Exception as e:
app.logger.error(f"Error getting term descriptions: {e}", exc_info=True)
return jsonify({'success': False, 'error': str(e)}), 500
@app.route('/api/term-descriptions', methods=['POST'])
@limiter.limit("20 per hour")
def update_term_descriptions():
"""Update one or more custom term descriptions"""
try:
data = request.json
updates = data.get('descriptions', {})
if not updates or not isinstance(updates, dict):
return jsonify({'success': False, 'error': 'No descriptions provided'}), 400
# Validate that all codes are valid term codes
valid_codes = {t['code'] for t in LICENSE_TERMS}
invalid_codes = [c for c in updates.keys() if c not in valid_codes]
if invalid_codes:
return jsonify({
'success': False,
'error': f'Invalid term codes: {", ".join(invalid_codes)}'
}), 400
# Validate description values
for code, desc in updates.items():
if not isinstance(desc, str) or len(desc.strip()) == 0:
return jsonify({
'success': False,
'error': f'Description for {code} must be a non-empty string'
}), 400
if len(desc) > 1000:
return jsonify({
'success': False,
'error': f'Description for {code} exceeds 1000 character limit'
}), 400
# Merge with existing custom descriptions
current = LLMExtractor.load_custom_descriptions()
current.update({k: v.strip() for k, v in updates.items()})
LLMExtractor.save_custom_descriptions(current)
app.logger.info(f'Custom term descriptions updated: {list(updates.keys())}')
return jsonify({
'success': True,
'message': f'{len(updates)} description(s) updated'
})
except Exception as e:
app.logger.error(f"Error updating term descriptions: {e}", exc_info=True)
return jsonify({'success': False, 'error': str(e)}), 500
@app.route('/api/term-descriptions', methods=['DELETE'])
@limiter.limit("10 per hour")
def reset_term_descriptions():
"""Reset term descriptions. Body can specify individual codes or reset all."""
try:
data = request.get_json(silent=True) or {}
codes_to_reset = data.get('codes', None)
if codes_to_reset is None:
# Reset all - delete the file
desc_file = Path(LLMExtractor.CUSTOM_DESCRIPTIONS_FILE)
if desc_file.exists():
desc_file.unlink()
app.logger.info('All custom term descriptions reset to defaults')
else:
# Reset specific codes
if not isinstance(codes_to_reset, list):
return jsonify({'success': False, 'error': 'codes must be a list'}), 400
current = LLMExtractor.load_custom_descriptions()
for code in codes_to_reset:
current.pop(code, None)
if current:
LLMExtractor.save_custom_descriptions(current)
else:
desc_file = Path(LLMExtractor.CUSTOM_DESCRIPTIONS_FILE)
if desc_file.exists():
desc_file.unlink()
app.logger.info(f'Custom term descriptions reset: {codes_to_reset}')
return jsonify({
'success': True,
'message': 'Description(s) reset to default'
})
except Exception as e:
app.logger.error(f"Error resetting term descriptions: {e}", exc_info=True)
return jsonify({'success': False, 'error': str(e)}), 500
# ============================================================================
# ERROR HANDLERS
# ============================================================================
@app.errorhandler(413)
def file_too_large(e):
"""Handle file too large error"""
app.logger.warning(f"File too large uploaded from {request.remote_addr}")
return jsonify({'error': 'File too large. Maximum size is 16MB.'}), 413
@app.errorhandler(429)
def ratelimit_handler(e):
"""Handle rate limit exceeded error"""
app.logger.warning(f"Rate limit exceeded for {request.remote_addr}: {request.path}")
return jsonify({
'error': 'Rate limit exceeded. Please slow down and try again later.',
'retry_after': e.description
}), 429
@app.errorhandler(500)
def internal_server_error(e):
"""Handle internal server errors"""
app.logger.error(f"Internal server error: {str(e)}", exc_info=True)
if app.debug:
return jsonify({'error': str(e)}), 500
else:
return jsonify({'error': 'An internal error occurred. Please try again.'}), 500
# ============================================================================
# SECURITY HEADERS (for development mode - Talisman handles production)
# ============================================================================
@app.after_request
def set_security_headers(response):
"""Add security headers to all responses"""
# In development mode, manually add security headers (Talisman handles production)
if app.debug or os.getenv('FLASK_ENV') == 'development':
# Content Security Policy
response.headers['Content-Security-Policy'] = (
"default-src 'self'; "
"script-src 'self' cdn.jsdelivr.net 'unsafe-inline'; "
"style-src 'self' cdn.jsdelivr.net 'unsafe-inline'; "
"img-src 'self' data:; "
"font-src 'self' cdn.jsdelivr.net; "
"connect-src 'self'; "
"frame-ancestors 'none';"
)
# Prevent MIME type sniffing