Skip to content

Commit 0c0094a

Browse files
author
TeleGhost Dev
committed
fix(security): address remaining gosec issues (G115, G304, G104)
1 parent 0effb5f commit 0c0094a

4 files changed

Lines changed: 17 additions & 8 deletions

File tree

internal/appcore/reseed.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package appcore
33
import (
44
"archive/zip"
55
"crypto/rand"
6-
"encoding/binary"
76
"fmt"
87
"io"
98
"log"
9+
"math/big"
1010
"os"
1111
"path/filepath"
1212
"strings"
@@ -54,9 +54,11 @@ func (a *AppCore) ExportReseed() (string, error) {
5454
// Используем crypto/rand для перемешивания (чтобы не использовать слабый math/rand)
5555
// Хотя math/rand здесь не критичен, gosec требует crypto/rand
5656
for i := len(files) - 1; i > 0; i-- {
57-
b := make([]byte, 8)
58-
_, _ = rand.Read(b)
59-
j := int(binary.BigEndian.Uint64(b) % uint64(i+1))
57+
n, err := rand.Int(rand.Reader, big.NewInt(int64(i+1)))
58+
if err != nil {
59+
continue // Should not happen with rand.Reader
60+
}
61+
j := int(n.Int64())
6062
files[i], files[j] = files[j], files[i]
6163
}
6264

internal/network/media/media_crypt.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,12 @@ func (m *MediaCrypt) NewMediaHandler(storageDir string) http.Handler {
6464
relPath := strings.TrimPrefix(r.URL.Path, "/secure/")
6565
fullPath := filepath.Join(storageDir, relPath)
6666

67-
// Очищаем путь и проверяем, что он внутри хранилища (простая защита от ../)
67+
// Очищаем путь и проверяем, что он внутри хранилища
6868
cleanPath := filepath.Clean(fullPath)
69-
if strings.Contains(cleanPath, "..") {
70-
http.Error(w, "Invalid path", http.StatusBadRequest)
69+
absStorage, _ := filepath.Abs(storageDir)
70+
absClean, _ := filepath.Abs(cleanPath)
71+
if !strings.HasPrefix(absClean, absStorage) {
72+
http.Error(w, "Access denied", http.StatusForbidden)
7173
return
7274
}
7375

internal/network/messenger/messenger.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"io"
99
"log"
10+
"math"
1011
"net"
1112
"sync"
1213
"time"
@@ -384,6 +385,10 @@ func (s *Service) writePacket(conn net.Conn, data []byte) error {
384385
_ = conn.SetWriteDeadline(time.Now().Add(ConnectionTimeout))
385386

386387
// Пишем размер (4 байта, big endian)
388+
const maxUint32 = math.MaxUint32
389+
if uint64(len(data)) > maxUint32 {
390+
return fmt.Errorf("packet too large for uint32: %d", len(data))
391+
}
387392
if len(data) > 100*1024*1024 { // 100 MB limit for safety
388393
return fmt.Errorf("packet too large: %d", len(data))
389394
}

internal/utils/image.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func CompressImage(path string, maxWidth, maxHeight uint) ([]byte, string, int,
3030

3131
// If image is small enough, don't resize, just re-encode (or keep original if raw needed, but this function implies compression)
3232
// Actually, we should resize if it's huge.
33-
if uint(width) > maxWidth || uint(height) > maxHeight {
33+
if (width > 0 && uint(width) > maxWidth) || (height > 0 && uint(height) > maxHeight) {
3434
img = resize.Thumbnail(maxWidth, maxHeight, img, resize.Lanczos3)
3535
width = img.Bounds().Dx()
3636
height = img.Bounds().Dy()

0 commit comments

Comments
 (0)