AOSSIE-Org / AOSSIE-Org/EduAid

[BUG] Missing validation and timeout handling in /getTranscript endpoint using yt-dlp

Ouverte
#539 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub
bug
Langage dominant
JavaScript
Étoiles
171
Forks
425
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

### Bug Description

The `/getTranscript` endpoint executes a `yt-dlp` subprocess to download YouTube subtitles and extract the transcript.

Currently, the subprocess execution does not include proper validation, timeout handling, or exception handling.

Example code from the endpoint:

```python
subprocess.run(
["yt-dlp", "--write-auto-sub", "--sub-lang", "en", "--skip-download",
"--sub-format", "vtt", "-o", f"subtitles/{video_id}.vtt",
f"https://www.youtube.com/watch?v={video_id}"],
check=True, capture_output=True, text=True
)
```

If the command fails or subtitles are unavailable, `subprocess.run` raises a `CalledProcessError`. Since this exception is not caught, the Flask server returns a **500 Internal Server Error** instead of a structured JSON response.

Additionally:

- The endpoint does not validate the `videoId`
- No timeout is defined for the subprocess execution
- A long-running or hanging `yt-dlp` process may block the Flask worker

This affects the **reliability and stability of the backend API**.

---

### Steps to Reproduce

1. Run the backend server
2. Send a request to the endpoint

```
http://127.0.0.1:5000/getTranscript?videoId=dQw4w9WgXcQ
```

3. Observe the response.

---

## Observed Behavior

The server returns:

```
500 Internal Server Error
```

instead of returning a meaningful JSON error response.

---

## Expected Behavior

The endpoint should:

- Validate the provided `videoId`
- Handle subprocess failures gracefully
- Return a proper JSON error response instead of a server crash
- Add timeout protection for the subprocess execution

---

## Proposed Improvement

The endpoint should be updated to:

1. Validate `videoId` format before executing the command
2. Add a timeout parameter to `subprocess.run`
3. Handle subprocess exceptions such as:

- `subprocess.TimeoutExpired`
- `subprocess.CalledProcessError`

Example improvement:

```python
try:
subprocess.run(command, timeout=20, check=True)
except subprocess.TimeoutExpired:
return jsonify({"error": "Transcript extraction timed out"}), 504
except subprocess.CalledProcessError:
return jsonify({"error": "Failed to fetch subtitles"}), 500
```

These changes will improve the **security, stability, and reliability** of the transcript extraction endpoint.

---

### Logs and Screenshots

While testing the `/getTranscript` endpoint locally, the server returned an **Internal Server Error (500)** instead of a structured JSON response.

Example request:

http://127.0.0.1:5000/getTranscript?videoId=dQw4w9WgXcQ

Observed response in browser:

```
Internal Server Error
The server encountered an internal error and was unable to complete your request.
```

Screenshot of the error:
[https://drive.google.com/file/d/1MO8xcAGlXq8LtVSAX4bhTYA-DBMSngY8/view?usp=sharing
](url)

This indicates that the `yt-dlp` subprocess failure is not properly handled and results in an unhandled exception in the Flask application.

---

### Environment Details

- **Operating System:** Windows 11
- **Browser:** Google Chrome
- **Backend Framework:** Flask
- **Python Version:** Python 3.x
- **Server:** Local development server (Flask)
- **Endpoint Tested:** `/getTranscript`

### Impact

High - Major feature is broken

### Code of Conduct

- [x] I have joined the [Discord server](https://discord.gg/hjUhu33uAn) and will post updates there
- [x] I have searched existing issues to avoid duplicates

Guide de contribution

Aucun guide de contribution indexé pour ce dépôt

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.