diff --git a/rhodecode/lib/utils2.py b/rhodecode/lib/utils2.py --- a/rhodecode/lib/utils2.py +++ b/rhodecode/lib/utils2.py @@ -1148,9 +1148,10 @@ def retry(func=None, exception=Exception return wrapper -def user_agent_normalizer(user_agent_raw): +def user_agent_normalizer(user_agent_raw, safe=True): log = logging.getLogger('rhodecode.user_agent_normalizer') ua = (user_agent_raw or '').strip().lower() + ua = ua.replace('"', '') try: if 'mercurial/proto-1.0' in ua: @@ -1158,8 +1159,15 @@ def user_agent_normalizer(user_agent_raw ua = ua.replace('(', '').replace(')', '').strip() ua = ua.replace('mercurial ', 'mercurial/') elif ua.startswith('git'): - pass + parts = ua.split(' ') + if parts: + ua = parts[0] + ua = re.sub('\.windows\.\d', '', ua).strip() + + return ua except Exception: log.exception('Failed to parse scm user-agent') + if not safe: + raise return ua diff --git a/rhodecode/tests/lib/test_utils.py b/rhodecode/tests/lib/test_utils.py --- a/rhodecode/tests/lib/test_utils.py +++ b/rhodecode/tests/lib/test_utils.py @@ -446,4 +446,33 @@ class TestGetEnabledHooks(object): def test_obfuscate_url_pw(): from rhodecode.lib.utils2 import obfuscate_url_pw engine = u'/home/repos/malmö' - assert obfuscate_url_pw(engine) \ No newline at end of file + assert obfuscate_url_pw(engine) + + +@pytest.mark.parametrize("test_ua, expected", [ + ("", ""), + ('"quoted"', 'quoted'), + ('internal-merge', 'internal-merge'), + ('hg/internal-merge', 'hg/internal-merge'), + ('git/internal-merge', 'git/internal-merge'), + + # git + ('git/2.10.1 (Apple Git-78)', 'git/2.10.1'), + ('GiT/2.37.2.windows.2', 'git/2.37.2'), + ('git/2.35.1 (Microsoft Windows NT 10.0.19044.0; Win32NT x64) CLR/4.0.30319 VS16/16.0.0', 'git/2.35.1'), + ('ssh-user-agent', 'ssh-user-agent'), + ('git/ssh-user-agent', 'git/ssh-user-agent'), + + + # hg + ('mercurial/proto-1.0 (Mercurial 4.2)', 'mercurial/4.2'), + ('mercurial/proto-1.0', ''), + ('mercurial/proto-1.0 (Mercurial 3.9.2)', 'mercurial/3.9.2'), + ('mercurial/ssh-user-agent', 'mercurial/ssh-user-agent'), + ('mercurial/proto-1.0 (Mercurial 5.8rc0)', 'mercurial/5.8rc0'), + + +]) +def test_user_agent_normalizer(test_ua, expected): + from rhodecode.lib.utils2 import user_agent_normalizer + assert user_agent_normalizer(test_ua, safe=False) == expected