Skip to content

Commit

Permalink
Do not accidentally leak errors from phonenumbers #4447
Browse files Browse the repository at this point in the history
ref DEV-1528
  • Loading branch information
tung2744 authored Jul 12, 2024
2 parents 4091c71 + f5d3118 commit 8036abf
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
1 change: 1 addition & 0 deletions pkg/util/phone/legal_and_valid_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func (p *legalAndValidParser) ParseInputPhoneNumber(phone string) (e164 string,
}
num, err := phonenumbers.Parse(phone, "")
if err != nil {
err = ErrNotInE164Format
return
}
isPhoneValid := phonenumbers.IsValidNumber(num)
Expand Down
15 changes: 12 additions & 3 deletions pkg/util/phone/legal_and_valid_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,20 @@ func TestLegalAndValidParser(t *testing.T) {
tooShort := "+85222"
So(parser.CheckE164(tooShort), ShouldBeError, "invalid phone number")

// Hong Kong number starting with 7
So(parser.CheckE164("+85270123456"), ShouldBeNil)
plus := "+"
So(parser.CheckE164(plus), ShouldBeError, "not in E.164 format")

plusCountryCode := "+852"
So(parser.CheckE164(plusCountryCode), ShouldBeError, "not in E.164 format")

nonsense := "a"
So(parser.CheckE164(nonsense), ShouldNotBeNil)
So(parser.CheckE164(nonsense), ShouldBeError, "not in E.164 format")

empty := ""
So(parser.CheckE164(empty), ShouldBeError, "not in E.164 format")

// Hong Kong number starting with 7
So(parser.CheckE164("+85270123456"), ShouldBeNil)
})

Convey("ParseCountryCallingCodeAndNationalNumber", func() {
Expand Down
1 change: 1 addition & 0 deletions pkg/util/phone/legal_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func (p *legalParser) ParseInputPhoneNumber(phone string) (e164 string, err erro
}
num, err := phonenumbers.Parse(phone, "")
if err != nil {
err = ErrNotInE164Format
return
}
e164 = phonenumbers.Format(num, phonenumbers.E164)
Expand Down
11 changes: 10 additions & 1 deletion pkg/util/phone/legal_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,17 @@ func TestLegalParser(t *testing.T) {
tooShort := "+85222"
So(parser.CheckE164(tooShort), ShouldBeNil)

plus := "+"
So(parser.CheckE164(plus), ShouldBeError, "not in E.164 format")

plusCountryCode := "+852"
So(parser.CheckE164(plusCountryCode), ShouldBeError, "not in E.164 format")

nonsense := "a"
So(parser.CheckE164(nonsense), ShouldNotBeNil)
So(parser.CheckE164(nonsense), ShouldBeError, "not in E.164 format")

empty := ""
So(parser.CheckE164(empty), ShouldBeError, "not in E.164 format")
})
})
}

0 comments on commit 8036abf

Please sign in to comment.