Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix code scanning alert no. 11: Full server-side request forgery #144

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

AJaySi
Copy link
Owner

@AJaySi AJaySi commented Jan 5, 2025

Fixes https://github.com/AJaySi/AI-Writer/security/code-scanning/11

To fix the problem, we need to ensure that the user-provided URL is validated against a list of authorized domains. This can be achieved by maintaining a list of allowed domains and checking if the user-provided URL belongs to one of these domains before making the request.

  1. Create a list of authorized domains.
  2. Parse the user-provided URL and extract the domain.
  3. Check if the extracted domain is in the list of authorized domains.
  4. If the domain is not authorized, display an error message and stop the execution.

Suggested fixes powered by Copilot Autofix. Review carefully before merging.

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
@AJaySi AJaySi requested review from uniqueumesh and removed request for DikshaDisciplines January 13, 2025 11:15
@AJaySi AJaySi assigned AJaySi and unassigned AJaySi and DikshaDisciplines Jan 13, 2025
@uniqueumesh uniqueumesh marked this pull request as ready for review January 13, 2025 14:27
@AJaySi
Copy link
Owner Author

AJaySi commented Jan 15, 2025

@uniqueumesh
Please Approve.

Copy link
Collaborator

@uniqueumesh uniqueumesh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.



# --- Analyze Button & Processing --- 

# List of authorized domains
AUTHORIZED_DOMAINS = ["example.com", "another-example.com"]

def is_valid_url(url):
    """
    Validates if the URL is well-formed and starts with http:// or https://.
    """
    if not url.startswith(("http://", "https://")):
        return False, "Invalid URL! Please ensure it starts with 'http://' or 'https://'."
    return True, None

def is_authorized_domain(url, authorized_domains):
    """
    Checks if the URL's domain is in the list of authorized domains.
    """
    domain = url.split("//")[-1].split("/")[0]
    if not any(domain.endswith(auth_domain) for auth_domain in authorized_domains):
        return False, f"Unauthorized domain! Allowed domains are: {', '.join(authorized_domains)}."
    return True, None

if st.button("Analyze with AI!"):
    with st.spinner('Analyzing your content...'):
        url = url_input.strip()

        # Validate URL format
        is_valid, error_message = is_valid_url(url)
        if not is_valid:
            st.error(error_message)
            st.stop()

        # Check if the domain is authorized
        is_auth, error_message = is_authorized_domain(url, AUTHORIZED_DOMAINS)
        if not is_auth:
            st.error(error_message)
            st.stop()

        # Fetch webpage content
        try:
            response = requests.get(url)
            response.raise_for_status()
            # Add your content analysis logic here
            st.success("Content analysis completed successfully!")
        except requests.RequestException as e:
            st.error(f"Failed to fetch the webpage. Error: {e}")


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants