Language: Python
Label: safe
CWE: CWE-668 \u2014 Exposure of Resource to Wrong Sphere
The product exposes a resource to the wrong control sphere, providing unintended actors with inappropriate access to the resource.

Code:
def upload_ssh_key(name: str, user_group: str, key: str) -> bool:
	if '..' in name:
		print('error: nice try')
		return False

	try:
		key = paramiko.pkey.load_private_key(key)
	except Exception as e:
		print(f'error: Cannot save SSH key file: {e}')
		return False

	lib_path = get_config.get_config_var('main', 'lib_path')
	full_dir = f'{lib_path}/keys/'
	ssh_keys = f'{name}.pem'

	try:
		_check_split = name.split('_')[1]
		split_name = True
	except Exception:
		split_name = False

	if not os.path.isfile(ssh_keys) and not split_name:
		name = f'{name}_{user_group}'

	if not os.path.exists(full_dir):
		os.makedirs(full_dir)

	ssh_keys = f'{full_dir}{name}.pem'

	try:
		key.write_private_key_file(ssh_keys)
	except Exception as e:
		print(f'error: Cannot save SSH key file: {e}')
		return False
	else:
		print(f'success: SSH key has been saved into: {ssh_keys}')

	try:
		os.chmod(ssh_keys, 0o600)
	except IOError as e:
		roxywi_common.logging('Roxy-WI server', e.args[0], roxywi=1)
		return False

	roxywi_common.logging("Roxy-WI server", f"A new SSH cert has been uploaded {ssh_keys}", roxywi=1, login=1)
	return True