Skip to content

Commit 7b29eb5

Browse files
AchoArnoldCopilot
andcommitted
fix: address PR review comments
- Add 4-char random base62 suffix to prevent same-second collisions - Sanitize filename by stripping non-alphanumeric chars (except . and -) - Restore backward-compat in cleanName for old bulk-csv-/bulk-xls- entries Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent b534091 commit 7b29eb5

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

api/pkg/handlers/bulk_message_handler.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package handlers
22

33
import (
4+
"crypto/rand"
45
"fmt"
56
"path/filepath"
7+
"regexp"
68
"sync"
79
"sync/atomic"
810
"time"
@@ -147,8 +149,27 @@ func (h *BulkMessageHandler) Store(c *fiber.Ctx) error {
147149

148150
func (h *BulkMessageHandler) generateRequestID(filename string) string {
149151
timestamp := encodeBase62(time.Now().Unix())
150-
truncated := truncateFilename(filename, 32)
151-
return fmt.Sprintf("bulk-%s-%s", timestamp, truncated)
152+
suffix := randomBase62(4)
153+
truncated := truncateFilename(sanitizeFilename(filename), 32)
154+
return fmt.Sprintf("bulk-%s%s-%s", timestamp, suffix, truncated)
155+
}
156+
157+
var unsafeCharsRegex = regexp.MustCompile(`[^a-zA-Z0-9.\-]`)
158+
159+
func sanitizeFilename(filename string) string {
160+
return unsafeCharsRegex.ReplaceAllString(filename, "")
161+
}
162+
163+
func randomBase62(length int) string {
164+
const charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
165+
b := make([]byte, length)
166+
if _, err := rand.Read(b); err != nil {
167+
return charset[:length]
168+
}
169+
for i := range b {
170+
b[i] = charset[int(b[i])%len(charset)]
171+
}
172+
return string(b)
152173
}
153174

154175
func encodeBase62(n int64) string {

web/pages/bulk-messages/index.vue

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,12 @@ export default Vue.extend({
221221
},
222222
methods: {
223223
cleanName(requestId: string): string {
224+
if (requestId.startsWith('bulk-csv-')) {
225+
return requestId.replace(/^bulk-csv-/, '') + '.csv'
226+
}
227+
if (requestId.startsWith('bulk-xls-')) {
228+
return requestId.replace(/^bulk-xls-/, '') + '.xlsx'
229+
}
224230
return requestId.replace(/^bulk-/, '')
225231
},
226232
fetchBulkOrders() {

0 commit comments

Comments
 (0)