Challenge: Preventing Code Injection
Oppgave
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
aandb. - If an operation is not allowed, return
None. - For division, if
bis zero, returnNone.
Løsning
Alt var klart?
Takk for tilbakemeldingene dine!
Seksjon 1. Kapittel 5
single
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Awesome!
Completion rate improved to 5.56
Challenge: Preventing Code Injection
Sveip for å vise menyen
Oppgave
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
aandb. - If an operation is not allowed, return
None. - For division, if
bis zero, returnNone.
Løsning
Alt var klart?
Takk for tilbakemeldingene dine!
Seksjon 1. Kapittel 5
single