Skip to content

Commit 6b04344

Browse files
committed
Refactor sendgridContent and sendgridWrite
Move the predicates inside `SendGridMail`. See #7127 (comment)
1 parent 6722671 commit 6b04344

1 file changed

Lines changed: 24 additions & 22 deletions

File tree

  • python/ql/src/experimental/semmle/python/frameworks

python/ql/src/experimental/semmle/python/frameworks/Sendgrid.qll

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,6 @@ private module Sendgrid {
4141
.getACall()
4242
}
4343

44-
private DataFlow::Node sendgridContent(DataFlow::CallCfgNode contentCall, string mime) {
45-
exists(StrConst mimeNode |
46-
mimeNode.getText() = mime and
47-
DataFlow::exprNode(mimeNode).(DataFlow::LocalSourceNode).flowsTo(contentCall.getArg(0)) and
48-
result = contentCall.getArg(1)
49-
)
50-
}
51-
52-
private DataFlow::Node sendgridWrite(DataFlow::CallCfgNode mailCall, string attributeName) {
53-
exists(DataFlow::AttrWrite attrWrite |
54-
attrWrite.getObject().getALocalSource() = mailCall and
55-
attrWrite.getAttributeName() = attributeName and
56-
result = attrWrite.getValue()
57-
)
58-
}
59-
6044
/**
6145
* Gets a reference to `sg.send()` and `sg.client.mail.send.post()`.
6246
*
@@ -82,15 +66,33 @@ private module Sendgrid {
8266
* * `getSubject()`'s result would be `"Sending with SendGrid is Fun"`.
8367
*/
8468
private class SendGridMail extends DataFlow::CallCfgNode, EmailSender::Range {
85-
SendGridMail() { this.getFunction() = sendgridApiSendCall() }
69+
SendGridMail() { this = sendgridApiSendCall() }
8670

87-
DataFlow::CallCfgNode getMailCall() {
71+
private DataFlow::CallCfgNode getMailCall() {
8872
exists(DataFlow::Node n |
8973
n in [this.getArg(0), this.getArgByName("request_body")] and
9074
result = [n, n.(DataFlow::MethodCallNode).getObject()].getALocalSource()
9175
)
9276
}
9377

78+
private DataFlow::Node sendgridContent(DataFlow::CallCfgNode contentCall, string mime) {
79+
mime in ["text/plain", "text/html", "text/x-amp-html"] and
80+
exists(StrConst mimeNode |
81+
mimeNode.getText() = mime and
82+
DataFlow::exprNode(mimeNode).(DataFlow::LocalSourceNode).flowsTo(contentCall.getArg(0)) and
83+
result = contentCall.getArg(1)
84+
)
85+
}
86+
87+
private DataFlow::Node sendgridWrite(string attributeName) {
88+
attributeName in ["plain_text_content", "html_content", "from_email", "subject"] and
89+
exists(DataFlow::AttrWrite attrWrite |
90+
attrWrite.getObject().getALocalSource() = this.getMailCall() and
91+
attrWrite.getAttributeName() = attributeName and
92+
result = attrWrite.getValue()
93+
)
94+
}
95+
9496
override DataFlow::Node getPlainTextBody() {
9597
result in [
9698
this.getMailCall().getArg(3), this.getMailCall().getArgByName("plain_text_content")
@@ -103,7 +105,7 @@ private module Sendgrid {
103105
sendgridContent(sendgridMailInstance().getMember("add_content").getACall(), "text/plain")
104106
]
105107
or
106-
result = sendgridWrite(this.getMailCall(), "plain_text_content")
108+
result = sendgridWrite("plain_text_content")
107109
}
108110

109111
override DataFlow::Node getHtmlBody() {
@@ -116,7 +118,7 @@ private module Sendgrid {
116118
this.getMailCall().getArg(4), this.getMailCall().getArgByName("html_content")
117119
].getALocalSource(), ["text/html", "text/x-amp-html"])
118120
or
119-
result = sendgridWrite(this.getMailCall(), "html_content")
121+
result = sendgridWrite("html_content")
120122
or
121123
exists(KeyValuePair content, Dict generalDict, KeyValuePair typePair, KeyValuePair valuePair |
122124
content.getKey().(StrConst).getText() = "content" and
@@ -173,15 +175,15 @@ private module Sendgrid {
173175
or
174176
result = this.getMailCall().getAMethodCall(["from_email", "set_from"]).getArg(0)
175177
or
176-
result = sendgridWrite(this.getMailCall(), "from_email")
178+
result = sendgridWrite("from_email")
177179
}
178180

179181
override DataFlow::Node getSubject() {
180182
result in [this.getMailCall().getArg(2), this.getMailCall().getArgByName("subject")]
181183
or
182184
result = this.getMailCall().getAMethodCall(["subject", "set_subject"]).getArg(0)
183185
or
184-
result = sendgridWrite(this.getMailCall(), "subject")
186+
result = sendgridWrite("subject")
185187
}
186188
}
187189
}

0 commit comments

Comments
 (0)