Skip to content

Commit c0f4da1

Browse files
committed
Make duplicate digits in codes fungible (due to bad DTMF parsing).
To get optimally low keyspace reduction I should only replace up to a doubling, but its ok for now.
1 parent eaf53d3 commit c0f4da1

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

ts/pulumi/zemn.me/api/server/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ go_oapi_codegen(
2222
go_library(
2323
name = "server",
2424
srcs = [
25+
"TestRemoveDuplicateDigits.go",
2526
"api.gen.go",
2627
"callbox_settings.go",
2728
"date.go",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package apiserver
2+
3+
import "testing"
4+
5+
func TestRemoveDuplicateDigits(t *testing.T) {
6+
tests := []struct {
7+
input string
8+
expected string
9+
}{
10+
{"112233445566", "123456"},
11+
{"1111111", "1"},
12+
{"121212", "121212"},
13+
{"", ""},
14+
{"a111b222c", "a1b2c"},
15+
{"a1b2c3", "a1b2c3"},
16+
{"1234444555", "12345"},
17+
{"00aa00", "0aa0"},
18+
}
19+
20+
for _, tt := range tests {
21+
result := removeDuplicateDigits(tt.input)
22+
if result != tt.expected {
23+
t.Errorf("removeDuplicateDigits(%q) = %q; expected %q", tt.input, result, tt.expected)
24+
}
25+
}
26+
}

ts/pulumi/zemn.me/api/server/phone.go

+22-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/http"
99
"os"
1010
"time"
11+
"unicode"
1112

1213
"github.com/nyaruka/phonenumbers"
1314
"github.com/twilio/twilio-go/twiml"
@@ -136,6 +137,26 @@ func (s *Server) GetPhoneInit(rw http.ResponseWriter, rq *http.Request) {
136137
}
137138
}
138139

140+
func removeDuplicateDigits(input string) string {
141+
var result []rune
142+
var lastDigit rune
143+
inDigitSeq := false
144+
145+
for _, r := range input {
146+
if unicode.IsDigit(r) {
147+
if !inDigitSeq || r != lastDigit {
148+
result = append(result, r)
149+
}
150+
lastDigit = r
151+
inDigitSeq = true
152+
} else {
153+
result = append(result, r)
154+
inDigitSeq = false
155+
}
156+
}
157+
return string(result)
158+
}
159+
139160
func (s *Server) handleEntryViaCode(w http.ResponseWriter, rq *http.Request, params GetPhoneHandleEntryParams) (success bool, err error) {
140161
codes, err := s.getLatestEntryCodes(rq.Context())
141162
if err != nil {
@@ -150,7 +171,7 @@ func (s *Server) handleEntryViaCode(w http.ResponseWriter, rq *http.Request, par
150171

151172
for _, code := range codes {
152173
if success = subtle.ConstantTimeCompare(
153-
[]byte(code.Code), []byte(digits),
174+
[]byte(removeDuplicateDigits(code.Code)), []byte(removeDuplicateDigits(digits)),
154175
) == 1; success {
155176
break
156177
}

0 commit comments

Comments
 (0)