"""Verify actual issue-only token boundaries without printing the token.""" from __future__ import annotations import json import os from pathlib import Path import sys import urllib.error import urllib.request REPO = Path(__file__).resolve().parents[1] sys.path.insert(0, str(REPO / 'src')) def main(): from dx_productivity.dsh.git_issues import GitIssuePublisher from dx_productivity.dsh.issue_credentials import load_bootstrap bootstrap = load_bootstrap() assert bootstrap and bootstrap['scopes'] == ['write:issue'] # Empty PATH ensures no Git executable or Git credential helper can be used. os.environ['PATH'] = '' config, headers, source = GitIssuePublisher(REPO / 'build/absent-user-issue-config.json').connection() assert source == 'prefilled_issue_token' base = config['server'].rstrip('/') + '/api/v1' target = '/repos/' + config['repository'] def status(route, method='GET', body=None): request = urllib.request.Request(base + route, method=method, data=body, headers=headers | {'Content-Type': 'application/json'}) try: with urllib.request.urlopen(request, timeout=20) as response: return response.status except urllib.error.HTTPError as error: return error.code positive = {name: status(route) for name, route in { 'list_issues': target + '/issues?limit=1', 'issue_comments': target + '/issues/4/comments', 'issue_attachments': target + '/issues/4/assets'}.items()} assert all(value == 200 for value in positive.values()), positive negative = { 'read_repository_contents': status(target + '/contents'), 'read_releases': status(target + '/releases'), 'read_admin_users': status('/admin/users'), 'read_organization_teams': status('/orgs/' + config['repository'].split('/')[0] + '/teams'), # Empty payloads cannot create a repository/release/file even if a # permission regression admits the route. Require an explicit denial. 'create_repository': status('/user/repos', 'POST', b'{}'), 'create_release': status(target + '/releases', 'POST', b'{}'), 'write_code': status(target + '/contents/__dsh_scope_probe__', 'POST', b'{}'), } assert all(value in {401, 403, 404} for value in negative.values()), negative report = {'status': 'passed', 'username': bootstrap['username'], 'token_id': bootstrap.get('token_id'), 'scopes': bootstrap['scopes'], 'credential_source': source, 'git_executable_available': False, 'no_local_user_configuration': True, 'allowed': positive, 'denied': negative} (REPO / 'build/dsh-issue-reporter-permissions.json').write_text(json.dumps(report, indent=2), encoding='utf8') print(json.dumps(report)) if __name__ == '__main__': main()