Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Challenge: Preventing Code Injection | Understanding Python Vulnerabilities
Python Security Best Practices

bookChallenge: Preventing Code Injection

Tâche

Swipe to start coding

Refactor a function that previously used eval() to perform mathematical operations based on a string input, so that it cannot be exploited for code injection. Use only a dictionary of explicitly allowed operations.

  • Only allow the operations "add", "subtract", "multiply", and "divide".
  • For each allowed operation, perform the corresponding arithmetic on a and b.
  • If an operation is not allowed, return None.
  • For division, if b is zero, return None.

Solution

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 5
single

single

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Suggested prompts:

Can you explain this in simpler terms?

What are some examples related to this topic?

Where can I learn more about this?

close

Awesome!

Completion rate improved to 5.56

bookChallenge: Preventing Code Injection

Glissez pour afficher le menu

Tâche

Swipe to start coding

Refactor a function that previously used eval() to perform mathematical operations based on a string input, so that it cannot be exploited for code injection. Use only a dictionary of explicitly allowed operations.

  • Only allow the operations "add", "subtract", "multiply", and "divide".
  • For each allowed operation, perform the corresponding arithmetic on a and b.
  • If an operation is not allowed, return None.
  • For division, if b is zero, return None.

Solution

Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 5
single

single

some-alt