Skip to content

Commit 63eadc8

Browse files
committed
Polish sendgrid modeling
1 parent 33b6f6f commit 63eadc8

1 file changed

Lines changed: 31 additions & 62 deletions

File tree

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

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

Lines changed: 31 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,22 @@ private module Sendgrid {
4343
result.(DataFlow::AttrRead).getAttributeName() in ["send", "post"]
4444
}
4545

46+
private DataFlow::Node sendgridContent(DataFlow::CallCfgNode contentCall, string mime) {
47+
exists(StrConst mimeNode |
48+
mimeNode.getText() = mime and
49+
DataFlow::exprNode(mimeNode).(DataFlow::LocalSourceNode).flowsTo(contentCall.getArg(0)) and
50+
result = contentCall.getArg(1)
51+
)
52+
}
53+
54+
private DataFlow::Node sendgridWrite(string attributeName) {
55+
exists(DataFlow::AttrWrite attrWrite |
56+
attrWrite.getObject().getALocalSource() = sendgridMailCall() and
57+
attrWrite.getAttributeName() = attributeName and
58+
result = attrWrite.getValue()
59+
)
60+
}
61+
4662
/**
4763
* Gets a reference to `sg.send()` and `sg.client.mail.send.post()`.
4864
*
@@ -75,51 +91,27 @@ private module Sendgrid {
7591
sendgridMailCall().getArg(3), sendgridMailCall().getArgByName("plain_text_content")
7692
]
7793
or
78-
exists(DataFlow::CallCfgNode contentCall, StrConst mime |
79-
contentCall = sendgridMailHelper().getMember("Content").getACall() and
80-
mime.getText() = "text/plain" and
81-
DataFlow::exprNode(mime).(DataFlow::LocalSourceNode).flowsTo(contentCall.getArg(0)) and
82-
result = contentCall.getArg(1)
83-
)
84-
or
85-
exists(DataFlow::CallCfgNode addContentCall, StrConst mime |
86-
addContentCall = sendgridMailInstance().getMember("add_content").getACall() and
87-
mime.getText() = "text/plain" and
88-
DataFlow::exprNode(mime).(DataFlow::LocalSourceNode).flowsTo(addContentCall.getArg(1)) and
89-
result = addContentCall.getArg(0)
90-
)
94+
result in [
95+
sendgridContent(sendgridMailHelper().getMember("Content").getACall(), "text/plain"),
96+
sendgridContent(sendgridMailInstance().getMember("add_content").getACall(), "text/plain")
97+
]
9198
or
92-
exists(DataFlow::AttrWrite bodyWrite |
93-
bodyWrite.getObject().getALocalSource() = sendgridMailCall() and
94-
bodyWrite.getAttributeName() = "plain_text_content" and
95-
result = bodyWrite.getValue()
96-
)
99+
result = sendgridWrite("plain_text_content")
97100
}
98101

99102
override DataFlow::Node getHtmlBody() {
100103
result in [sendgridMailCall().getArg(4), sendgridMailCall().getArgByName("html_content")]
101104
or
102105
result = sendgridMailInstance().getMember("set_html").getACall().getArg(0)
103106
or
104-
exists(DataFlow::CallCfgNode contentCall, StrConst mime |
105-
contentCall = sendgridMailHelper().getMember("Content").getACall() and
106-
mime.getText() = ["text/html", "text/x-amp-html"] and
107-
DataFlow::exprNode(mime).(DataFlow::LocalSourceNode).flowsTo(contentCall.getArg(0)) and
108-
result = contentCall.getArg(1)
109-
)
110-
or
111-
exists(DataFlow::CallCfgNode addContentCall, StrConst mime |
112-
addContentCall = sendgridMailInstance().getMember("add_content").getACall() and
113-
mime.getText() = ["text/html", "text/x-amp-html"] and
114-
DataFlow::exprNode(mime).(DataFlow::LocalSourceNode).flowsTo(addContentCall.getArg(1)) and
115-
result = addContentCall.getArg(0)
116-
)
107+
result in [
108+
sendgridContent(sendgridMailHelper().getMember("Content").getACall(),
109+
["text/html", "text/x-amp-html"]),
110+
sendgridContent(sendgridMailInstance().getMember("add_content").getACall(),
111+
["text/html", "text/x-amp-html"])
112+
]
117113
or
118-
exists(DataFlow::AttrWrite htmlWrite |
119-
htmlWrite.getObject().getALocalSource() = sendgridMailCall() and
120-
htmlWrite.getAttributeName() = "html_content" and
121-
result = htmlWrite.getValue()
122-
)
114+
result = sendgridWrite("html_content")
123115
or
124116
exists(KeyValuePair content, Dict generalDict, KeyValuePair typePair, KeyValuePair valuePair |
125117
content.getKey().(Str_).getS() = "content" and
@@ -134,7 +126,7 @@ private module Sendgrid {
134126
)
135127
or
136128
exists(KeyValuePair footer, Dict generalDict, KeyValuePair enablePair, KeyValuePair htmlPair |
137-
footer.getKey().(Str_).getS() = "footer" and
129+
footer.getKey().(Str_).getS() = ["footer", "subscription_tracking"] and
138130
footer.getValue().(Dict) = generalDict and
139131
// check footer is enabled
140132
enablePair.getKey().(Str_).getS() = "enable" and
@@ -145,21 +137,6 @@ private module Sendgrid {
145137
// correlate generalDict with previously set KeyValuePairs
146138
generalDict.getAnItem() in [enablePair, htmlPair]
147139
)
148-
or
149-
exists(
150-
KeyValuePair subTracking, Dict generalDict, KeyValuePair enablePair, KeyValuePair htmlPair
151-
|
152-
subTracking.getKey().(Str_).getS() = "subscription_tracking" and
153-
subTracking.getValue().(Dict) = generalDict and
154-
// check subscription tracking is enabled
155-
enablePair.getKey().(Str_).getS() = "enable" and
156-
exists(enablePair.getValue().(True)) and
157-
// get html content
158-
htmlPair.getKey().(Str_).getS() = "html" and
159-
result.asExpr() = htmlPair.getValue() and
160-
// correlate generalDict with previously set KeyValuePairs
161-
generalDict.getAnItem() in [enablePair, htmlPair]
162-
)
163140
}
164141

165142
override DataFlow::Node getTo() {
@@ -181,23 +158,15 @@ private module Sendgrid {
181158
or
182159
result = sendgridMailInstance().getMember(["from_email", "set_from"]).getACall().getArg(0)
183160
or
184-
exists(DataFlow::AttrWrite fromWrite |
185-
fromWrite.getObject().getALocalSource() = sendgridMailCall() and
186-
fromWrite.getAttributeName() = "from_email" and
187-
result = fromWrite.getValue()
188-
)
161+
result = sendgridWrite("from_email")
189162
}
190163

191164
override DataFlow::Node getSubject() {
192165
result in [sendgridMailCall().getArg(2), sendgridMailCall().getArgByName("subject")]
193166
or
194167
result = sendgridMailInstance().getMember(["subject", "set_subject"]).getACall().getArg(0)
195168
or
196-
exists(DataFlow::AttrWrite subjectWrite |
197-
subjectWrite.getObject().getALocalSource() = sendgridMailCall() and
198-
subjectWrite.getAttributeName() = "subject" and
199-
result = subjectWrite.getValue()
200-
)
169+
result = sendgridWrite("subject")
201170
}
202171
}
203172
}

0 commit comments

Comments
 (0)