11import concurrent .futures
22import configparser
3+ import ipaddress
34import logging
45import multiprocessing
56import os
67import shutil
78import signal
9+ import socket
810import ssl
911import sys
12+ from urllib .parse import urlsplit
1013import _thread
1114
1215import tornado
@@ -357,8 +360,15 @@ def _parse_config(self, config_file):
357360 100 , None ),
358361 (SettingsParameters .GzipEnabled , ConfigParameters .TABPY_GZIP_ENABLE ,
359362 True , parser .getboolean ),
360- (SettingsParameters .ArrowEnabled , ConfigParameters .TABPY_ARROW_ENABLE , False , parser .getboolean ),
363+ (SettingsParameters .ArrowEnabled , ConfigParameters .TABPY_ARROW_ENABLE , False , parser .getboolean ),
361364 (SettingsParameters .ArrowFlightPort , ConfigParameters .TABPY_ARROWFLIGHT_PORT , 13622 , parser .getint ),
365+ (SettingsParameters .OAuthEnabled , ConfigParameters .TABPY_OAUTH_ENABLED , False , parser .getboolean ),
366+ (SettingsParameters .OAuthIssuer , ConfigParameters .TABPY_OAUTH_ISSUER , None , None ),
367+ (SettingsParameters .OAuthJwksUri , ConfigParameters .TABPY_OAUTH_JWKS_URI , None , None ),
368+ (SettingsParameters .OAuthAudience , ConfigParameters .TABPY_OAUTH_AUDIENCE , None , None ),
369+ (SettingsParameters .OAuthRequiredScopes , ConfigParameters .TABPY_OAUTH_REQUIRED_SCOPES ,
370+ None , None ),
371+ (SettingsParameters .OAuthLogUser , ConfigParameters .TABPY_OAUTH_LOG_USER , False , parser .getboolean ),
362372 ]
363373
364374 for setting , parameter , default_val , parse_function in settings_parameters :
@@ -403,6 +413,10 @@ def _parse_config(self, config_file):
403413 if state_config .has_option ("Service Info" , "Subdirectory" ):
404414 self .subdirectory = "/" + state_config .get ("Service Info" , "Subdirectory" )
405415
416+ # Validate OAuth config if enabled
417+ if self .settings [SettingsParameters .OAuthEnabled ]:
418+ self ._validate_oauth_settings ()
419+
406420 # If passwords file specified load credentials
407421 if ConfigParameters .TABPY_PWD_FILE in self .settings :
408422 if not self ._parse_pwd_file ():
@@ -412,7 +426,7 @@ def _parse_config(self, config_file):
412426 )
413427 logger .critical (msg )
414428 raise RuntimeError (msg )
415- else :
429+ elif not self . settings [ SettingsParameters . OAuthEnabled ] :
416430 self ._handle_configuration_without_authentication ()
417431
418432 features = self ._get_features ()
@@ -513,14 +527,110 @@ def _handle_configuration_without_authentication(self):
513527 "https://github.com/tableau/TabPy/blob/master/docs/server-config.md#authentication." )
514528 exit ()
515529
530+ def _validate_oauth_settings (self ):
531+ required = [
532+ (SettingsParameters .OAuthIssuer , ConfigParameters .TABPY_OAUTH_ISSUER ),
533+ (SettingsParameters .OAuthJwksUri , ConfigParameters .TABPY_OAUTH_JWKS_URI ),
534+ (SettingsParameters .OAuthAudience , ConfigParameters .TABPY_OAUTH_AUDIENCE ),
535+ ]
536+ missing = [
537+ config_key for setting , config_key in required
538+ if not self .settings .get (setting )
539+ ]
540+ if missing :
541+ msg = (
542+ f"{ ConfigParameters .TABPY_OAUTH_ENABLED } is true but missing required "
543+ f"setting(s): { ', ' .join (missing )} "
544+ )
545+ logger .critical (msg )
546+ raise RuntimeError (msg )
547+
548+ # JWKS/issuer are the trust anchor for JWT verification, so both must
549+ # be fetched over https to prevent an on-path attacker from substituting
550+ # their own keys/issuer.
551+ insecure = [
552+ config_key for setting , config_key in (
553+ (SettingsParameters .OAuthIssuer , ConfigParameters .TABPY_OAUTH_ISSUER ),
554+ (SettingsParameters .OAuthJwksUri , ConfigParameters .TABPY_OAUTH_JWKS_URI ),
555+ )
556+ if not self .settings [setting ].lower ().startswith ("https://" )
557+ ]
558+ if insecure :
559+ msg = (
560+ f"{ ', ' .join (insecure )} must use https: JWKS/issuer are the trust "
561+ "anchor for JWT verification and must not be fetched over plain HTTP"
562+ )
563+ logger .critical (msg )
564+ raise RuntimeError (msg )
565+
566+ # TABPY_OAUTH_JWKS_URI is fetched over the network, so a
567+ # misconfigured or attacker-supplied host pointing at an
568+ # internal/link-local address (e.g. a cloud metadata endpoint) would
569+ # otherwise be an SSRF vector. Reject it here at startup.
570+ jwks_host = urlsplit (self .settings [SettingsParameters .OAuthJwksUri ]).hostname
571+ try :
572+ jwks_addresses = {
573+ info [4 ][0 ] for info in socket .getaddrinfo (jwks_host , None )
574+ }
575+ except socket .gaierror as ex :
576+ msg = (
577+ f"Unable to resolve { ConfigParameters .TABPY_OAUTH_JWKS_URI } host "
578+ f'"{ jwks_host } ": { ex } '
579+ )
580+ logger .critical (msg )
581+ raise RuntimeError (msg )
582+ # Allowlist (require is_global) rather than denylist: a denylist of
583+ # is_private/is_loopback/is_link_local/is_reserved misses ranges
584+ # like IPv4-mapped IPv6 (::ffff:169.254.169.254) and CGNAT
585+ # (100.64.0.0/10), which is_global correctly excludes.
586+ unsafe_addresses = [
587+ address for address in jwks_addresses
588+ if not ipaddress .ip_address (address ).is_global
589+ ]
590+ if unsafe_addresses :
591+ msg = (
592+ f"{ ConfigParameters .TABPY_OAUTH_JWKS_URI } host \" { jwks_host } \" "
593+ f"resolves to a non-public address "
594+ f"({ ', ' .join (unsafe_addresses )} ): refusing to use it as the "
595+ "JWKS endpoint"
596+ )
597+ logger .critical (msg )
598+ raise RuntimeError (msg )
599+
600+ # Arrow Flight's auth middleware currently only supports basic auth
601+ # (see _get_arrow_server): whenever any auth method is enabled it
602+ # unconditionally reads TABPY_PWD_FILE, so OAuth-only + Arrow would
603+ # otherwise crash at startup looking for a pwd file that was never
604+ # configured. Revisit this check if Arrow Flight ever adds its own
605+ # OAuth/JWT middleware option.
606+ if (
607+ self .settings [SettingsParameters .ArrowEnabled ]
608+ and ConfigParameters .TABPY_PWD_FILE not in self .settings
609+ ):
610+ msg = (
611+ f"{ ConfigParameters .TABPY_ARROW_ENABLE } requires "
612+ f"{ ConfigParameters .TABPY_PWD_FILE } to be set: Arrow Flight does not "
613+ "support OAuth authentication"
614+ )
615+ logger .critical (msg )
616+ raise RuntimeError (msg )
617+
516618 def _get_features (self ):
517619 features = {}
518620
519621 # Check for auth
520- if ConfigParameters .TABPY_PWD_FILE in self .settings :
622+ if (
623+ ConfigParameters .TABPY_PWD_FILE in self .settings
624+ or self .settings [SettingsParameters .OAuthEnabled ]
625+ ):
626+ methods = {}
627+ if ConfigParameters .TABPY_PWD_FILE in self .settings :
628+ methods ["basic-auth" ] = {}
629+ if self .settings [SettingsParameters .OAuthEnabled ]:
630+ methods ["oauth-jwt" ] = {}
521631 features ["authentication" ] = {
522632 "required" : True ,
523- "methods" : { "basic-auth" : {}} ,
633+ "methods" : methods ,
524634 }
525635
526636 features ["evaluate_enabled" ] = self .settings [SettingsParameters .EvaluateEnabled ]
0 commit comments