Skip to content

Commit 69f7b69

Browse files
Add OAuth 2.0 / JWT Bearer token authentication (#664)
* Add JWT validation module for OAuth Bearer tokens. * Add TABPY_OAUTH_* config settings and startup validation. * Add oauth-jwt as a request authentication method. * Document OAuth/JWT Bearer token authentication. * Ignore all .venv* directories, not just .venv. * Fix markdownlint failures in CODE_OF_CONDUCT.md and SECURITY.md. * Document the JWKS blocking/global-state tradeoffs. * Extract shared JWT test fixtures. * Pin PyJWT to >=2.9,<3. * Reject OAuth-only + Arrow Flight at startup. * Stop logging the raw Authorization header on parse failure. * Rate-limit JWKS fetch failures and bound forced-refresh cooldowns. * Reject JWKS URIs that resolve to private/loopback/link-local addresses (SSRF guard). * Default TABPY_OAUTH_LOG_USER to disabled. * Regression-test uniform 401 body across JWT failure reasons. * Harden SSRF check, log-user fallback, and kid logging. * Document TABPY_LOG_DETAILS dependency and list TABPY_OAUTH_* params in config reference.
1 parent 6ab5d72 commit 69f7b69

14 files changed

Lines changed: 1584 additions & 44 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ celerybeat-schedule
8585

8686
# Environments
8787
.env
88-
.venv
88+
.venv*
8989
env/
9090
venv/
9191
ENV/

‎CODE_OF_CONDUCT.md‎

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ Equality is a core value at Salesforce. We believe a diverse and inclusive
66
community fosters innovation and creativity, and are committed to building a
77
culture where everyone feels included.
88

9-
Salesforce open-source projects are committed to providing a friendly, safe, and
10-
welcoming environment for all, regardless of gender identity and expression,
11-
sexual orientation, disability, physical appearance, body size, ethnicity, nationality,
12-
race, age, religion, level of experience, education, socioeconomic status, or
13-
other similar personal characteristics.
9+
Salesforce open-source projects are committed to providing a friendly, safe,
10+
and welcoming environment for all, regardless of gender identity and
11+
expression, sexual orientation, disability, physical appearance, body size,
12+
ethnicity, nationality, race, age, religion, level of experience, education,
13+
socioeconomic status, or other similar personal characteristics.
1414

1515
The goal of this code of conduct is to specify a baseline standard of behavior so
1616
that people with different social values and communication styles can work
@@ -19,16 +19,16 @@ It also establishes a mechanism for reporting issues and resolving conflicts.
1919

2020
All questions and reports of abusive, harassing, or otherwise unacceptable behavior
2121
in a Salesforce open-source project may be reported by contacting the Salesforce
22-
Open Source Conduct Committee at ossconduct@salesforce.com.
22+
Open Source Conduct Committee at <ossconduct@salesforce.com>.
2323

2424
## Our Pledge
2525

2626
In the interest of fostering an open and welcoming environment, we as
2727
contributors and maintainers pledge to making participation in our project and
28-
our community a harassment-free experience for everyone, regardless of gender
29-
identity and expression, sexual orientation, disability, physical appearance,
30-
body size, ethnicity, nationality, race, age, religion, level of experience, education,
31-
socioeconomic status, or other similar personal characteristics.
28+
our community a harassment-free experience for everyone, regardless of gender
29+
identity and expression, sexual orientation, disability, physical appearance,
30+
body size, ethnicity, nationality, race, age, religion, level of experience,
31+
education, socioeconomic status, or other similar personal characteristics.
3232

3333
## Our Standards
3434

@@ -77,23 +77,24 @@ further defined and clarified by project maintainers.
7777
## Enforcement
7878

7979
Instances of abusive, harassing, or otherwise unacceptable behavior may be
80-
reported by contacting the Salesforce Open Source Conduct Committee
81-
at ossconduct@salesforce.com. All complaints will be reviewed and investigated
82-
and will result in a response that is deemed necessary and appropriate to the
83-
circumstances. The committee is obligated to maintain confidentiality with
84-
regard to the reporter of an incident. Further details of specific enforcement
80+
reported by contacting the Salesforce Open Source Conduct Committee
81+
at <ossconduct@salesforce.com>. All complaints will be reviewed and investigated
82+
and will result in a response that is deemed necessary and appropriate to the
83+
circumstances. The committee is obligated to maintain confidentiality with
84+
regard to the reporter of an incident. Further details of specific enforcement
8585
policies may be posted separately.
8686

8787
Project maintainers who do not follow or enforce the Code of Conduct in good
8888
faith may face temporary or permanent repercussions as determined by other
89-
members of the project's leadership and the Salesforce Open Source Conduct
89+
members of the project's leadership and the Salesforce Open Source Conduct
9090
Committee.
9191

9292
## Attribution
9393

94-
This Code of Conduct is adapted from the [Contributor Covenant][contributor-covenant-home],
95-
version 1.4, available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html.
96-
It includes adaptions and additions from [Go Community Code of Conduct][golang-coc],
94+
This Code of Conduct is adapted from the
95+
[Contributor Covenant][contributor-covenant-home], version 1.4, available at
96+
[https://www.contributor-covenant.org/version/1/4/code-of-conduct.html](https://www.contributor-covenant.org/version/1/4/code-of-conduct.html).
97+
It includes adaptions and additions from [Go Community Code of Conduct][golang-coc],
9798
[CNCF Code of Conduct][cncf-coc], and [Microsoft Open Source Code of Conduct][microsoft-coc].
9899

99100
This Code of Conduct is licensed under the [Creative Commons Attribution 3.0 License][cc-by-3-us].
@@ -102,4 +103,4 @@ This Code of Conduct is licensed under the [Creative Commons Attribution 3.0 Lic
102103
[golang-coc]: https://golang.org/conduct
103104
[cncf-coc]: https://github.com/cncf/foundation/blob/master/code-of-conduct.md
104105
[microsoft-coc]: https://opensource.microsoft.com/codeofconduct/
105-
[cc-by-3-us]: https://creativecommons.org/licenses/by/3.0/us/
106+
[cc-by-3-us]: https://creativecommons.org/licenses/by/3.0/us/

‎SECURITY.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Security
1+
# Security
22

33
Please report any security issue to [https://www.sfdc.co/SubmitVuln](https://www.sfdc.co/SubmitVuln)
44
as soon as it is discovered. This library limits its runtime dependencies in

‎docs/server-config.md‎

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ at [`logging.config` documentation page](https://docs.python.org/3.6/library/log
7171
makes TabPy require credentials with HTTP(S) requests. More details about
7272
authentication can be found in [Authentication](#authentication)
7373
section. Default value - not set.
74+
- `TABPY_OAUTH_ENABLED`, `TABPY_OAUTH_ISSUER`, `TABPY_OAUTH_JWKS_URI`,
75+
`TABPY_OAUTH_AUDIENCE`, `TABPY_OAUTH_REQUIRED_SCOPES`,
76+
`TABPY_OAUTH_LOG_USER` - configure OAuth/JWT Bearer token authentication.
77+
See [OAuth / JWT Bearer Token Authentication](#oauth--jwt-bearer-token-authentication).
7478
- `TABPY_TRANSFER_PROTOCOL` - transfer protocol. Default value - `http`. If
7579
set to `https` two additional parameters have to be specified:
7680
`TABPY_CERTIFICATE_FILE` and `TABPY_KEY_FILE`.
@@ -209,9 +213,10 @@ connection scenario.
209213

210214
## Authentication
211215

212-
TabPy supports basic access authentication (see
216+
TabPy supports two authentication methods, which can be enabled independently
217+
or at the same time: basic access authentication (see
213218
[https://en.wikipedia.org/wiki/Basic_access_authentication](https://en.wikipedia.org/wiki/Basic_access_authentication)
214-
for more details).
219+
for more details) and OAuth/JWT Bearer token authentication.
215220

216221
### Enabling Authentication
217222

@@ -269,6 +274,72 @@ will be generated and displayed in the command line.
269274
To delete an account open password file in any text editor and delete the
270275
line with the user name.
271276

277+
### OAuth / JWT Bearer Token Authentication
278+
279+
TabPy can validate OAuth 2.0 JWT Bearer tokens on incoming requests. This
280+
allows per-user credentials issued by an external identity provider (IdP)
281+
to be used instead of, or alongside, a single shared password-file
282+
credential.
283+
284+
To enable it, specify the following parameters in the TabPy configuration
285+
file:
286+
287+
```sh
288+
TABPY_OAUTH_ENABLED = true
289+
TABPY_OAUTH_ISSUER = https://idp.example.com/
290+
TABPY_OAUTH_JWKS_URI = https://idp.example.com/.well-known/jwks.json
291+
TABPY_OAUTH_AUDIENCE = tabpy
292+
```
293+
294+
`TABPY_OAUTH_ISSUER`, `TABPY_OAUTH_JWKS_URI`, and `TABPY_OAUTH_AUDIENCE` are
295+
all required when `TABPY_OAUTH_ENABLED` is `true`; TabPy will fail to start
296+
if any are missing. `TABPY_OAUTH_ISSUER` and `TABPY_OAUTH_JWKS_URI` must use
297+
`https://` -- the JWKS response is the trust anchor for verifying JWT
298+
signatures, so fetching it over plain HTTP would let anyone on the network
299+
path substitute their own keys. `TABPY_OAUTH_JWKS_URI` is also resolved at
300+
startup, and TabPy will fail to start if it resolves to a private,
301+
loopback, or link-local address, since that endpoint is fetched over the
302+
network on TabPy's behalf and could otherwise be pointed at an internal
303+
service (e.g. a cloud metadata endpoint).
304+
305+
- `TABPY_OAUTH_ISSUER` is the expected `iss` claim on incoming JWTs.
306+
- `TABPY_OAUTH_JWKS_URI` is the IdP's JWKS endpoint, used to fetch and cache
307+
the signing keys used to verify JWT signatures.
308+
- `TABPY_OAUTH_AUDIENCE` is the expected `aud` claim on incoming JWTs.
309+
310+
Two additional parameters are optional:
311+
312+
```sh
313+
TABPY_OAUTH_REQUIRED_SCOPES = tabpy:query,tabpy:evaluate
314+
TABPY_OAUTH_LOG_USER = true
315+
```
316+
317+
- `TABPY_OAUTH_REQUIRED_SCOPES` is a comma-separated list of scopes that
318+
must all be present in the JWT's `scope` claim for the request to be
319+
accepted. If unset, no scope check is performed.
320+
- `TABPY_OAUTH_LOG_USER` (default `false`) sets the JWT's `sub` claim as the
321+
authenticated user for logging purposes. The `sub` claim is often a
322+
user's email or SSO ID, so leave this disabled unless that's an
323+
acceptable thing to write to logs in your environment. This has no
324+
effect unless [`TABPY_LOG_DETAILS`](#request-context-logging) is also
325+
enabled -- that's what actually logs the authenticated user, for both
326+
basic auth and OAuth.
327+
328+
To authenticate a request, send the JWT as a Bearer token:
329+
330+
```sh
331+
curl -H "Authorization: Bearer <token>" http://localhost:9004/info
332+
```
333+
334+
When both basic access authentication and OAuth are enabled, TabPy picks the
335+
method based on the scheme of the `Authorization` header sent by the client
336+
(`Basic` or `Bearer`), so both can be used against the same server.
337+
338+
JWKS lookups are cached, but because TabPy serves requests on a single
339+
thread, a slow or unresponsive IdP during a cache-cold fetch (startup, or a
340+
key rotation) will briefly stall all concurrent requests, not just the one
341+
that triggered the fetch.
342+
272343
### Endpoint Security
273344

274345
All endpoints require authentication if it is enabled for the server.

‎setup.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ def read(fname):
7272
"configparser",
7373
"coverage",
7474
"coveralls",
75+
"cryptography",
7576
"docopt",
7677
"future",
7778
"genson",
@@ -81,6 +82,7 @@ def read(fname):
8182
"nltk",
8283
"numpy",
8384
"pandas",
85+
"pyjwt[crypto]>=2.9,<3",
8486
"pyopenssl",
8587
"pytest",
8688
"pytest-cov",

‎tabpy/tabpy_server/app/app.py‎

Lines changed: 114 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import concurrent.futures
22
import configparser
3+
import ipaddress
34
import logging
45
import multiprocessing
56
import os
67
import shutil
78
import signal
9+
import socket
810
import ssl
911
import sys
12+
from urllib.parse import urlsplit
1013
import _thread
1114

1215
import 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

Comments
 (0)