mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-17 23:43:34 +01:00
Enforce length limits on link preview fields.
This commit is contained in:
@@ -21,6 +21,8 @@ import java.util.Optional;
|
|||||||
|
|
||||||
public class LinkPreview implements Parcelable {
|
public class LinkPreview implements Parcelable {
|
||||||
|
|
||||||
|
private static final int MAX_FIELD_LENGTH = 500;
|
||||||
|
|
||||||
@JsonProperty
|
@JsonProperty
|
||||||
private final String url;
|
private final String url;
|
||||||
|
|
||||||
@@ -41,8 +43,8 @@ public class LinkPreview implements Parcelable {
|
|||||||
|
|
||||||
public LinkPreview(@NonNull String url, @NonNull String title, @NonNull String description, long date, @NonNull DatabaseAttachment thumbnail) {
|
public LinkPreview(@NonNull String url, @NonNull String title, @NonNull String description, long date, @NonNull DatabaseAttachment thumbnail) {
|
||||||
this.url = url;
|
this.url = url;
|
||||||
this.title = title;
|
this.title = truncate(title);
|
||||||
this.description = description;
|
this.description = truncate(description);
|
||||||
this.date = date;
|
this.date = date;
|
||||||
this.thumbnail = Optional.of(thumbnail);
|
this.thumbnail = Optional.of(thumbnail);
|
||||||
this.attachmentId = thumbnail.attachmentId;
|
this.attachmentId = thumbnail.attachmentId;
|
||||||
@@ -50,8 +52,8 @@ public class LinkPreview implements Parcelable {
|
|||||||
|
|
||||||
public LinkPreview(@NonNull String url, @NonNull String title, @NonNull String description, long date, @NonNull Optional<Attachment> thumbnail) {
|
public LinkPreview(@NonNull String url, @NonNull String title, @NonNull String description, long date, @NonNull Optional<Attachment> thumbnail) {
|
||||||
this.url = url;
|
this.url = url;
|
||||||
this.title = title;
|
this.title = truncate(title);
|
||||||
this.description = description;
|
this.description = truncate(description);
|
||||||
this.date = date;
|
this.date = date;
|
||||||
this.thumbnail = thumbnail;
|
this.thumbnail = thumbnail;
|
||||||
this.attachmentId = null;
|
this.attachmentId = null;
|
||||||
@@ -64,8 +66,8 @@ public class LinkPreview implements Parcelable {
|
|||||||
@JsonProperty("attachmentId") @Nullable AttachmentId attachmentId)
|
@JsonProperty("attachmentId") @Nullable AttachmentId attachmentId)
|
||||||
{
|
{
|
||||||
this.url = url;
|
this.url = url;
|
||||||
this.title = title;
|
this.title = truncate(title);
|
||||||
this.description = Optional.ofNullable(description).orElse("");
|
this.description = truncate(Optional.ofNullable(description).orElse(""));
|
||||||
this.date = date;
|
this.date = date;
|
||||||
this.attachmentId = attachmentId;
|
this.attachmentId = attachmentId;
|
||||||
this.thumbnail = Optional.empty();
|
this.thumbnail = Optional.empty();
|
||||||
@@ -73,8 +75,8 @@ public class LinkPreview implements Parcelable {
|
|||||||
|
|
||||||
protected LinkPreview(Parcel in) {
|
protected LinkPreview(Parcel in) {
|
||||||
url = in.readString();
|
url = in.readString();
|
||||||
title = in.readString();
|
title = truncate(in.readString());
|
||||||
description = in.readString();
|
description = truncate(in.readString());
|
||||||
date = in.readLong();
|
date = in.readLong();
|
||||||
attachmentId = ParcelCompat.readParcelable(in, AttachmentId.class.getClassLoader(), AttachmentId.class);
|
attachmentId = ParcelCompat.readParcelable(in, AttachmentId.class.getClassLoader(), AttachmentId.class);
|
||||||
thumbnail = Optional.ofNullable(ParcelCompat.readParcelable(in, Attachment.class.getClassLoader(), Attachment.class));
|
thumbnail = Optional.ofNullable(ParcelCompat.readParcelable(in, Attachment.class.getClassLoader(), Attachment.class));
|
||||||
@@ -135,6 +137,10 @@ public class LinkPreview implements Parcelable {
|
|||||||
return attachmentId;
|
return attachmentId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static @NonNull String truncate(@NonNull String value) {
|
||||||
|
return value.length() > MAX_FIELD_LENGTH ? value.substring(0, MAX_FIELD_LENGTH) : value;
|
||||||
|
}
|
||||||
|
|
||||||
public @NonNull String serialize() throws IOException {
|
public @NonNull String serialize() throws IOException {
|
||||||
return JsonUtils.toJson(this);
|
return JsonUtils.toJson(this);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user