Skip to content

Commit

Permalink
Ensure the title of the 'Issuer' block is no more than 50 characters
Browse files Browse the repository at this point in the history
- chop off the `https://` to give us a little more room
- use a sha1 hash if the title is still too big
  • Loading branch information
matyasselmeci committed Oct 30, 2023
1 parent b1c38f6 commit 992681d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
11 changes: 7 additions & 4 deletions src/tests/test_stashcache.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from configparser import ConfigParser
import copy
import flask
import hashlib
import pytest
import re
from pytest_mock import MockerFixture
Expand Down Expand Up @@ -112,15 +113,17 @@ def test_scitokens_issuer_character_limit(self, client: flask.Flask):
cp = ConfigParser()
cp.read_string(origin_scitokens_conf, "origin_scitokens.conf")

hasher = hashlib.sha1()
hasher.update(b"test.wisc.edu/long-name-that-is-over-50-characters-even-if-you-strip-off-https")
hashed_title = hasher.hexdigest()
try:
assert "Global" in cp, "Missing Global section"
assert "Issuer https://test.wisc.edu" in cp, \
assert "Issuer test.wisc.edu" in cp, \
"Issuer with reasonable length missing"
assert "Issuer issuer-thats-50-characters-long-if-you.chop" in cp, \
"Issuer that just barely fits if you chop off the scheme missing"
assert (cp["Issuer issuer-thats-50-characters-long-if-you.chop"]["issuer"] ==
"https://issuer-thats-50-characters-long-if-you.chop"), \
"Unexpected issuer in a section we modified"
assert f"Issuer {hashed_title}" in cp, \
"Issuer that's needed to be hashed missing"
assert not re.search(r"^\[[^]]{51}", origin_scitokens_conf, re.MULTILINE), \
"Section over 50 chars long found"
# ^^ easier to regexp this
Expand Down
15 changes: 14 additions & 1 deletion src/webapp/data_federation.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import hashlib
import urllib
import urllib.parse
from collections import OrderedDict
Expand Down Expand Up @@ -76,6 +77,8 @@ def get_authfile_id(self):

class SciTokenAuth(AuthMethod):
used_in_scitokens_conf = True
ISSUER_TITLE_MAX_LENGTH = 43 # 50 - the length of 'Issuer '
# xrootd/xrootd#2074

def __init__(self, issuer: str, base_path: str, restricted_path: Optional[str], map_subject: bool):
self.issuer = issuer
Expand All @@ -90,7 +93,17 @@ def __str__(self):
def get_scitokens_conf_block(self, service_name: str):
if service_name not in [XROOTD_CACHE_SERVER, XROOTD_ORIGIN_SERVER]:
raise ValueError(f"service_name must be '{XROOTD_CACHE_SERVER}' or '{XROOTD_ORIGIN_SERVER}'")
block = (f"[Issuer {self.issuer}]\n"

issuer_title = self.issuer
if issuer_title.startswith("https://"):
issuer_title = issuer_title[8:]
# title is too long; replace it with a hash
if len(issuer_title) > self.ISSUER_TITLE_MAX_LENGTH:
hasher = hashlib.sha1() # sha1 hex digest is 40 chars
hasher.update(issuer_title.encode("utf-8", errors="ignore"))
issuer_title = hasher.hexdigest()

block = (f"[Issuer {issuer_title}]\n"
f"issuer = {self.issuer}\n"
f"base_path = {self.base_path}\n")
if self.restricted_path:
Expand Down

0 comments on commit 992681d

Please sign in to comment.