Skip to content

Commit

Permalink
Merge pull request #3394 from semgrep/merge-develop-to-release
Browse files Browse the repository at this point in the history
Merge Develop into Release
  • Loading branch information
inkz authored May 29, 2024
2 parents 30f6caa + 95ac723 commit 81f05e2
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
88 changes: 88 additions & 0 deletions python/twilio/security/twiml-injection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
from twilio.rest import Client
import html
from xml.sax.saxutils import escape

client = Client("accountSid", "authToken")
XML = "<Response><Say>{}</Say><Hangup/></Response>"


def fstring(to: str, msg: str) -> None:
client.calls.create(
# ruleid: twiml-injection
twiml=f"<Response><Say>{msg}</Say><Hangup/></Response>",
to=to,
from_="555-555-5555",
)


def format_const(to: str, msg: str) -> None:
twiml = XML.format(msg)
client.calls.create(
# ruleid: twiml-injection
twiml=twiml,
to=to,
from_="555-555-5555",
)


def percent(to: str, msg: str) -> None:
client.calls.create(
# ruleid: twiml-injection
twiml="<Response><Say>%s</Say><Hangup/></Response>" % msg,
to=to,
from_="555-555-5555",
)


def format(to: str, msg: str) -> None:
client.calls.create(
# ruleid: twiml-injection
twiml="<Response><Say>{}</Say><Hangup/></Response>".format(msg),
to=to,
from_="555-555-5555",
)


def concat(to: str, msg: str) -> None:
client.calls.create(
# ruleid: twiml-injection
twiml="<Response><Say>" + msg + "</Say><Hangup/></Response>",
to=to,
from_="555-555-5555",
)


def safe(to: str, msg: str) -> None:
client.calls.create(
# ok: twiml-injection
twiml="<Response><Say>nsec</Say><Hangup/></Response>",
to=to,
from_="555-555-5555",
)


def also_safe(to: str, msg: str) -> None:
client.calls.create(
# ok: twiml-injection
twiml="<Response><Say>nsec</Say><Hangup/></Response>",
to=to,
from_=f"{1+2}34-323-1234",
)


def html_escape(to: str, msg: str) -> None:
client.calls.create(
# ok: twiml-injection
twiml="<Response><Say>" + html.escape(msg) + "</Say><Hangup/></Response>",
to=to,
from_="555-555-5555",
)


def xml_escape(to: str, msg: str) -> None:
client.calls.create(
# ok: twiml-injection
twiml="<Response><Say>" + escape(msg) + "</Say><Hangup/></Response>",
to=to,
from_="555-555-5555",
)
48 changes: 48 additions & 0 deletions python/twilio/security/twiml-injection.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
rules:
- id: twiml-injection
languages: [python]
severity: WARNING
message: >-
Using non-constant TwiML (Twilio Markup Language) argument when creating a
Twilio conversation could allow the injection of additional TwiML commands
metadata:
cwe:
- "CWE-91: XML Injection"
owasp:
- "A03:2021 - Injection"
category: security
technology:
- python
- twilio
- twiml
confidence: MEDIUM
likelihood: HIGH
impact: MEDIUM
subcategory: vuln
references:
- https://codeberg.org/fennix/funjection
mode: taint
pattern-sources:
- pattern: |
f"..."
- pattern: |
"..." % ...
- pattern: |
"...".format(...)
- patterns:
- pattern: $ARG
- pattern-inside: |
def $F(..., $ARG, ...):
...
pattern-sanitizers:
- pattern: xml.sax.saxutils.escape(...)
- pattern: html.escape(...)

pattern-sinks:
- patterns:
- pattern: |
$CLIENT.calls.create(..., twiml=$SINK, ...)
- focus-metavariable: $SINK

0 comments on commit 81f05e2

Please sign in to comment.