Skip to content

Commit

Permalink
Validate AWS IDs to ensure they match AWS account IDs, which should b…
Browse files Browse the repository at this point in the history
…e 12-digit numerical values.
  • Loading branch information
Chrystinne committed Oct 4, 2023
1 parent cce0c96 commit d264d03
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions physionet-django/user/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import os
import pdb
import re
from datetime import datetime

import django.contrib.auth.views as auth_views
Expand Down Expand Up @@ -904,6 +905,24 @@ def credential_reference_verification(request, application_slug, verification_to
return render(request, 'user/credential_reference.html', {'form': form, 'application': application})


def is_aws_valid(aws_id):
"""
Validate an AWS ID.
Args:
aws_id (str): The AWS ID to be validated.
Returns:
bool: True if the AWS ID is valid, False otherwise.
"""
aws_id_pattern = r"\b\d{12}\b"

if re.search(aws_id_pattern, aws_id):
return True
else:
return False


@login_required
def edit_cloud(request):
"""
Expand All @@ -915,8 +934,13 @@ def edit_cloud(request):
if request.method == 'POST':
form = forms.CloudForm(instance=cloud_info, data=request.POST)
if form.is_valid():
form.save()
messages.success(request, 'Your cloud information has been saved.')
aws_id = form.cleaned_data.get('aws_id')
# Check if the AWS ID is valid or None
if aws_id is None or is_aws_valid(aws_id):
form.save()
messages.success(request, 'Your cloud information has been saved.')
else:
messages.error(request, 'Invalid AWS ID. Please provide a valid AWS ID.')
else:
messages.error(request, 'Invalid submission. See errors below.')

Expand Down

0 comments on commit d264d03

Please sign in to comment.