Skip to content

Commit 8a19b1d

Browse files
Added a filesize validation to make sure we're under 1.5MB
1 parent 173a4f1 commit 8a19b1d

4 files changed

Lines changed: 42 additions & 14 deletions

File tree

android/app/src/main/java/com/httpsms/SmsManagerService.kt

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,8 @@ class SmsManagerService {
7777
}
7878
}
7979

80-
fun sendMultimediaMessage(
81-
context: Context,
82-
pduUri: android.net.Uri,
83-
sim: String,
84-
sentIntent: PendingIntent
85-
) {
80+
fun sendMultimediaMessage(context: Context, pduUri: android.net.Uri, sim: String, sentIntent: PendingIntent) {
8681
val smsManager = getSmsManager(context, sim)
87-
smsManager.sendMultimediaMessage(
88-
context,
89-
pduUri,
90-
null,
91-
null,
92-
sentIntent
93-
)
82+
smsManager.sendMultimediaMessage(context, pduUri, null, null, sentIntent)
9483
}
9584
}

api/pkg/validators/bulk_message_handler_validator.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ func (v *BulkMessageHandlerValidator) validateMessages(messages []*requests.Bulk
221221

222222
if message.AttachmentURLs != "" {
223223
urls := strings.Split(message.AttachmentURLs, ",")
224-
224+
225225
if len(urls) > 10 {
226226
result.Add("document", fmt.Sprintf("Row [%d]: You cannot attach more than 10 files per message.", index+2))
227227
}
@@ -237,6 +237,10 @@ func (v *BulkMessageHandlerValidator) validateMessages(messages []*requests.Bulk
237237
result.Add("document", fmt.Sprintf("Row [%d]: The attachment URL [%s] has an invalid url format.", index+2, cleanURL))
238238
} else if parsedURL.Scheme != "http" && parsedURL.Scheme != "https" {
239239
result.Add("document", fmt.Sprintf("Row [%d]: The attachment URL [%s] must use http or https.", index+2, cleanURL))
240+
} else {
241+
if err := validateAttachmentURL(cleanURL); err != nil {
242+
result.Add("attachments", fmt.Sprintf("Row [%d]: The attachment URL [%s] failed validation: %s", index+2, cleanURL, err.Error()))
243+
}
240244
}
241245
}
242246
}

api/pkg/validators/message_handler_validator.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ func (validator MessageHandlerValidator) ValidateMessageSend(ctx context.Context
123123
result.Add("attachments", fmt.Sprintf("attachment at index %d has an invalid url format", i))
124124
} else if parsedURL.Scheme != "http" && parsedURL.Scheme != "https" {
125125
result.Add("attachments", fmt.Sprintf("attachment at index %d must use http or https scheme", i))
126+
} else {
127+
if err := validateAttachmentURL(attachment.URL); err != nil {
128+
result.Add("attachments", fmt.Sprintf("attachment at index %d failed validation: %s", i, err.Error()))
129+
}
126130
}
127131
}
128132
}

api/pkg/validators/validator.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package validators
22

33
import (
44
"fmt"
5+
"net/http"
56
"net/url"
67
"regexp"
78
"strings"
9+
"time"
810

911
"github.com/NdoleStudio/httpsms/pkg/events"
1012

@@ -160,3 +162,32 @@ func (validator *validator) ValidateUUID(ID string, name string) url.Values {
160162

161163
return v.ValidateStruct()
162164
}
165+
166+
func validateAttachmentURL(attachmentURL string) error {
167+
client := &http.Client{
168+
Timeout: 5 * time.Second,
169+
}
170+
171+
req, err := http.NewRequest(http.MethodHead, attachmentURL, nil)
172+
if err != nil {
173+
return fmt.Errorf("invalid url format")
174+
}
175+
176+
resp, err := client.Do(req)
177+
if err != nil {
178+
return fmt.Errorf("could not reach the url")
179+
}
180+
defer resp.Body.Close()
181+
182+
if resp.StatusCode < 200 || resp.StatusCode >= 400 {
183+
return fmt.Errorf("url returned an error status code: %d", resp.StatusCode)
184+
}
185+
186+
const maxSizeBytes = 1.5 * 1024 * 1024
187+
188+
if resp.ContentLength > int64(maxSizeBytes) {
189+
return fmt.Errorf("file size (%.2f MB) exceeds the 1.5 MB carrier limit", float64(resp.ContentLength)/(1024*1024))
190+
}
191+
192+
return nil
193+
}

0 commit comments

Comments
 (0)