Skip to content

Improve backend error handling and input validation in upload API#94

Open
Darkshadow0409 wants to merge 1 commit intoindictechcom:masterfrom
Darkshadow0409:pr1-error-handling
Open

Improve backend error handling and input validation in upload API#94
Darkshadow0409 wants to merge 1 commit intoindictechcom:masterfrom
Darkshadow0409:pr1-error-handling

Conversation

@Darkshadow0409
Copy link
Copy Markdown

Related Task

https://phabricator.wikimedia.org/T415715

Problem

The current backend error handling in the /api/upload endpoint is limited and assumes valid input in several places. This can lead to crashes (e.g., IndexError when regex does not match) and inconsistent error responses.

Solution

This PR improves error handling by adding basic input validation and preventing unsafe operations.

Changes made:

  • Added validation for source URL to avoid crashes when regex does not match
  • Added checks for missing required fields (e.g., srcUrl, trfilename)
  • Introduced a helper function to standardize error responses
  • Ensured API returns structured JSON errors instead of crashing

Benefits

  • Prevents server crashes on invalid input
  • Improves API reliability
  • Makes error responses consistent and easier to debug

Note

This PR focuses on minimal and safe improvements aligned with microtask requirements, without introducing major refactoring.

Copilot AI review requested due to automatic review settings March 31, 2026 17:33
@Darkshadow0409
Copy link
Copy Markdown
Author

I have worked on improving basic error handling in the upload API by adding input validation and preventing unsafe assumptions in the code.

I focused on keeping the changes minimal and aligned with the microtask scope, while improving reliability and consistency of API responses.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR improves robustness of the /api/upload Flask endpoint by adding input validation and introducing a small helper to return structured JSON error responses instead of crashing on malformed input.

Changes:

  • Added an error_response() helper to standardize JSON error payloads.
  • Added basic validation for missing/invalid srcUrl and missing trfilename in /api/upload.
  • Added URL regex-match guarding to prevent unsafe indexing when parsing source project/lang.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread app.py
Comment on lines 74 to 75
data = request.get_json()
src_url = urllib.parse.unquote(data.get('srcUrl'))
Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

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

request.get_json() can return None (invalid/missing JSON), and urllib.parse.unquote(data.get('srcUrl')) will raise when data is None or when srcUrl is missing (because unquote(None) errors). Validate data is a dict and that srcUrl exists before calling unquote, or pass a safe default / use get_json(silent=True) and handle None with a structured error response.

Copilot uses AI. Check for mistakes.
Comment thread app.py
Comment on lines 94 to 103
tr_project = data.get('trproject')
tr_lang = data.get('trlang')
tr_filename = data.get('trfilename')

#adding safe data access
if not tr_filename:
return error_response("Missing target filename", 400)
tr_filename = urllib.parse.unquote(tr_filename)
tr_endpoint = "https://" + tr_lang + "." + tr_project + ".org/w/api.php"

Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

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

tr_lang and tr_project are used to build tr_endpoint via string concatenation, but they may be None if the request omits them, causing a TypeError before the later None not in (...) guard runs. Add explicit validation for required target fields (e.g., trlang, trproject) before constructing tr_endpoint, and return a consistent JSON error when missing/invalid.

Copilot uses AI. Check for mistakes.
Comment thread app.py
return jsonify({
"success": False,
"data": {},
"errors": [message]
Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

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

New helper returns payload with an errors key, but other endpoints in this file sometimes return error (singular). If the goal is standardized error responses, align on one key across the API (or document/encode backward compatibility) to avoid confusing clients.

Suggested change
"errors": [message]
"error": message

Copilot uses AI. Check for mistakes.
Comment thread app.py
Comment on lines +81 to +82
#addinig url validation
if not match:
Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

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

Typos in newly added comments: "addinig" should be "adding" (and consider capitalizing comments for readability).

Copilot uses AI. Check for mistakes.
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.

2 participants