-
Notifications
You must be signed in to change notification settings - Fork 24.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Watcher reporting: add email warning if CSV attachment contains values that may be interperted as formulas #44460
Changes from all commits
ecb394b
024ef74
0acff82
dea3635
ed5128f
6261d53
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,17 +24,26 @@ | |
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.nio.file.Path; | ||
import java.util.Collections; | ||
import java.util.Set; | ||
|
||
import static javax.mail.Part.ATTACHMENT; | ||
import static javax.mail.Part.INLINE; | ||
|
||
public abstract class Attachment extends BodyPartSource { | ||
|
||
private final boolean inline; | ||
private final Set<String> warnings; | ||
|
||
protected Attachment(String id, String name, String contentType, boolean inline) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this constructor used anymore? Im ok w/ removing it if we dont use it anywhere. Also, if we dont want a NPE down in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, this constructor is still used in some cases. I added an |
||
this(id, name, contentType, inline, Collections.emptySet()); | ||
} | ||
|
||
protected Attachment(String id, String name, String contentType, boolean inline, Set<String> warnings) { | ||
super(id, name, contentType); | ||
this.inline = inline; | ||
assert warnings != null; | ||
this.warnings = warnings; | ||
} | ||
|
||
@Override | ||
|
@@ -53,6 +62,10 @@ public boolean isInline() { | |
return inline; | ||
} | ||
|
||
public Set<String> getWarnings() { | ||
return warnings; | ||
} | ||
|
||
/** | ||
* intentionally not emitting path as it may come as an information leak | ||
*/ | ||
|
@@ -116,15 +129,15 @@ public static class Bytes extends Attachment { | |
private final byte[] bytes; | ||
|
||
public Bytes(String id, byte[] bytes, String contentType, boolean inline) { | ||
this(id, id, bytes, contentType, inline); | ||
this(id, id, bytes, contentType, inline, Collections.emptySet()); | ||
} | ||
|
||
public Bytes(String id, String name, byte[] bytes, boolean inline) { | ||
this(id, name, bytes, fileTypeMap.getContentType(name), inline); | ||
this(id, name, bytes, fileTypeMap.getContentType(name), inline, Collections.emptySet()); | ||
} | ||
|
||
public Bytes(String id, String name, byte[] bytes, String contentType, boolean inline) { | ||
super(id, name, contentType, inline); | ||
public Bytes(String id, String name, byte[] bytes, String contentType, boolean inline, Set<String> warnings) { | ||
super(id, name, contentType, inline, warnings); | ||
this.bytes = bytes; | ||
} | ||
|
||
|
@@ -213,7 +226,7 @@ protected XContent(String id, ToXContent content, XContentType type) { | |
} | ||
|
||
protected XContent(String id, String name, ToXContent content, XContentType type) { | ||
super(id, name, bytes(name, content, type), mimeType(type), false); | ||
super(id, name, bytes(name, content, type), mimeType(type), false, Collections.emptySet()); | ||
} | ||
|
||
static String mimeType(XContentType type) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
package org.elasticsearch.xpack.watcher.notification.email; | ||
|
||
import org.elasticsearch.ElasticsearchParseException; | ||
import org.elasticsearch.common.Strings; | ||
import org.elasticsearch.common.xcontent.ToXContentObject; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.common.xcontent.XContentParser; | ||
|
@@ -16,9 +17,11 @@ | |
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
|
||
public class EmailTemplate implements ToXContentObject { | ||
|
||
|
@@ -110,19 +113,46 @@ public Email.Builder render(TextTemplateEngine engine, Map<String, Object> model | |
if (subject != null) { | ||
builder.subject(engine.render(subject, model)); | ||
} | ||
if (textBody != null) { | ||
builder.textBody(engine.render(textBody, model)); | ||
} | ||
|
||
Set<String> warnings = new HashSet<>(1); | ||
if (attachments != null) { | ||
for (Attachment attachment : attachments.values()) { | ||
builder.attach(attachment); | ||
warnings.addAll(attachment.getWarnings()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just realized, that despite the current documenation, body is not actually required, will need to adjust this to always emit a warning even if the body is not defined. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done in 0acff82 |
||
String htmlWarnings = ""; | ||
String textWarnings = ""; | ||
if(warnings.isEmpty() == false){ | ||
StringBuilder textWarningBuilder = new StringBuilder(); | ||
StringBuilder htmlWarningBuilder = new StringBuilder(); | ||
warnings.forEach(w -> | ||
{ | ||
if(Strings.isNullOrEmpty(w) == false) { | ||
textWarningBuilder.append(w).append("\n"); | ||
htmlWarningBuilder.append(w).append("<br>"); | ||
} | ||
}); | ||
textWarningBuilder.append("\n"); | ||
htmlWarningBuilder.append("<br>"); | ||
htmlWarnings = htmlWarningBuilder.toString(); | ||
textWarnings = textWarningBuilder.toString(); | ||
} | ||
if (textBody != null) { | ||
builder.textBody(textWarnings + engine.render(textBody, model)); | ||
} | ||
|
||
if (htmlBody != null) { | ||
String renderedHtml = engine.render(htmlBody, model); | ||
String renderedHtml = htmlWarnings + engine.render(htmlBody, model); | ||
renderedHtml = htmlSanitizer.sanitize(renderedHtml); | ||
builder.htmlBody(renderedHtml); | ||
} | ||
|
||
if(htmlBody == null && textBody == null && Strings.isNullOrEmpty(textWarnings) == false){ | ||
builder.textBody(textWarnings); | ||
} | ||
|
||
return builder; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is to avoid JAR hell when running tests via IntelliJ (nothing specific to the changes here)