AOSSIE-Org / AOSSIE-Org/EduAid

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

Aperta
#539 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
bug
Lingua principale
JavaScript
Stelle
171
Fork
425
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

### 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

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.