Skip to content
This repository was archived by the owner on Jul 12, 2023. It is now read-only.

Commit e31109a

Browse files
authored
Don't allow longexpires in user report (#2370)
1 parent 789f467 commit e31109a

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

assets/server/realmadmin/_form_sms.html

+11-3
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ <h5 class="mb-3">SMS templates</h5>
187187
</div>
188188
<div class="form-floating mb-3">
189189
<textarea name="sms_text_template_{{$i}}" class="form-control font-monospace {{if $realm.ErrorsFor $v.Label}}is-invalid{{end}} sms-text-template"
190-
placeholder="SMS text template" style="height:150px;">{{$v.Value}}</textarea>
190+
placeholder="SMS text template" style="height:150px;" label="{{$v.Label}}">{{$v.Value}}</textarea>
191191
<label for="sms-text-template">SMS text template</label>
192192
</div>
193193
{{if $realm.ErrorsFor $v.Label}}
@@ -416,7 +416,7 @@ <h5 class="mb-3">SMS templates</h5>
416416
if (val.length > {{$realm.SMSTemplateMaxLength}}) {
417417
errors.push('SMS Templates must be <= {{$realm.SMSTemplateMaxLength}} characters, currently ' + val.length + ' characters.');
418418
}
419-
419+
420420
// Provide live feedback on errors in the SMS Template construction.
421421
if (enxEnabled) {
422422
if (!val.includes("[enslink]")) {
@@ -438,11 +438,19 @@ <h5 class="mb-3">SMS templates</h5>
438438
hasLC = val.includes("[longcode]");
439439
if (!(hasSC || hasLC) || (hasSC && hasLC)) {
440440
errors.push('must contain exactly one of `[code]` or `[longcode]`');
441-
}
441+
}
442442

443443
val = val.replace(/\[region\]/g, region);
444444
val = val.replace(/\[longcode\]/g, longCode);
445445
}
446+
447+
isUserReport = $(target).attr('label') === "User Report";
448+
if (isUserReport) {
449+
if (val.includes(("[longexpires]"))) {
450+
errors.push("'User Report' template cannot contain `[longexpires]` since user report always uses the short expiration time `[expires]`, which is always in minutes.");
451+
}
452+
}
453+
446454
val = val.replace(/\[code\]/g, shortCode);
447455
val = val.replace(/\[expires\]/g, shortExpires);
448456
val = val.replace(/\[longexpires\]/g, longExpires);

pkg/database/realm.go

+7
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,13 @@ func (r *Realm) validateSMSTemplate(label, t string) string {
718718
}
719719
}
720720

721+
if label == UserReportTemplateLabel {
722+
if strings.Contains(t, SMSLongExpires) {
723+
r.AddError("smsTextTemplate", fmt.Sprintf("cannot contain %q - for %q the 'short expiration' time is used an is represented in minutes", SMSLongExpires, UserReportTemplateLabel))
724+
r.AddError(label, fmt.Sprintf("cannot contain %q", SMSLongExpires))
725+
}
726+
}
727+
721728
// Check template length.
722729
if l := len(t); l > SMSTemplateMaxLength {
723730
r.AddError("smsTextTemplate", fmt.Sprintf("must be %d characters or less, current message is %v characters long", SMSTemplateMaxLength, l))

pkg/database/realm_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,34 @@ func TestRealm_BeforeSave(t *testing.T) {
458458
}
459459
}
460460

461+
func TestRealm_ValidateSMSTemplateUserReport(t *testing.T) {
462+
t.Parallel()
463+
464+
realm := NewRealmWithDefaults("test")
465+
realm.RegionCode = "US-WA"
466+
realm.AllowedTestTypes = TestTypeConfirmed | TestTypeUserReport
467+
realm.EnableENExpress = true
468+
realm.SMSTextTemplate = DefaultENXSMSTextTemplate
469+
470+
db, _ := testDatabaseInstance.NewDatabase(t, nil)
471+
if err := db.SaveRealm(realm, SystemTest); err != nil {
472+
t.Fatalf("save error: %v issues: %+v", err, realm.ErrorMessages())
473+
}
474+
475+
_ = realm.validateSMSTemplate(UserReportTemplateLabel, *realm.SMSTextAlternateTemplates[UserReportTemplateLabel])
476+
if len(realm.Errors()) > 0 {
477+
t.Fatalf("unexpected errors when saving realm in ")
478+
}
479+
480+
badTemplate := "Click here [enslink] expires in [longexpires] time"
481+
realm.SMSTextAlternateTemplates[UserReportTemplateLabel] = &badTemplate
482+
483+
_ = realm.validateSMSTemplate(UserReportTemplateLabel, *realm.SMSTextAlternateTemplates[UserReportTemplateLabel])
484+
if _, ok := realm.Errors()["smsTextTemplate"]; !ok {
485+
t.Fatalf("missing expected error for `smsTextTemplate`")
486+
}
487+
}
488+
461489
func TestRealm_validateSMSTemplate(t *testing.T) {
462490
t.Parallel()
463491

0 commit comments

Comments
 (0)