fix backend audit findings (auth, upload, sync, data, ocr, contract)
CI / lint-backend (push) Failing after 1m39s
CI / lint-mobile (push) Canceled after 11m8s
CI / test-backend (push) Canceled after 0s
CI / test-mobile (push) Canceled after 0s
CI / build-backend (push) Canceled after 0s
CI / docker (push) Canceled after 0s
CI / lint-backend (push) Failing after 1m39s
CI / lint-mobile (push) Canceled after 11m8s
CI / test-backend (push) Canceled after 0s
CI / test-mobile (push) Canceled after 0s
CI / build-backend (push) Canceled after 0s
CI / docker (push) Canceled after 0s
This commit is contained in:
@@ -4,13 +4,42 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/ledongthuc/pdf"
|
||||
)
|
||||
|
||||
const (
|
||||
// tesseractTimeout : budget max par job (A13) — Tesseract ne doit jamais
|
||||
// bloquer un worker indéfiniment (image pathologique).
|
||||
tesseractTimeout = 120 * time.Second
|
||||
// outputCap : plafond stdout/stderr capturés (A13, 2 Mo) — un document
|
||||
// hostile ne doit pas faire exploser la RAM du worker.
|
||||
outputCap = 2 << 20
|
||||
// maxPDFPages : plafond de pages lues (A13) — au-delà, erreur explicite
|
||||
// (pas de troncature silencieuse).
|
||||
maxPDFPages = 50
|
||||
)
|
||||
|
||||
// validTesseractLang : whitelist `^[a-z+]{2,20}$` (A12, défense en profondeur
|
||||
// — le point d'entrée normal est service.normalizeOcrLang). Évite toute
|
||||
// injection via `tesseract -l` (espaces, `;`, options `--...`).
|
||||
func validTesseractLang(lang string) bool {
|
||||
if len(lang) < 2 || len(lang) > 20 {
|
||||
return false
|
||||
}
|
||||
for _, r := range lang {
|
||||
if (r < 'a' || r > 'z') && r != '+' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// TesseractEngine runs the `tesseract` binary in a subprocess.
|
||||
// Images are OCR'd directly; PDFs have their text layer extracted first
|
||||
// (scanned PDFs → empty text, no rendering pipeline in V1).
|
||||
@@ -18,20 +47,55 @@ type TesseractEngine struct{}
|
||||
|
||||
func NewTesseract() *TesseractEngine { return &TesseractEngine{} }
|
||||
|
||||
// ExtractText implements Engine.
|
||||
// ExtractText implements Engine. Chaque job est borné par tesseractTimeout
|
||||
// (contexte dérivé, annulé en fin d'appel).
|
||||
func (t *TesseractEngine) ExtractText(ctx context.Context, filePath, lang string) (string, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, tesseractTimeout)
|
||||
defer cancel()
|
||||
if strings.ToLower(filepath.Ext(filePath)) == ".pdf" {
|
||||
return extractPDFText(filePath)
|
||||
return extractPDFText(ctx, filePath)
|
||||
}
|
||||
return runTesseract(ctx, filePath, lang)
|
||||
}
|
||||
|
||||
// cappedBuffer borne un flux capturé à `limit` octets et signale la
|
||||
// troncature (erreur explicite côté appelant, pas de coupe silencieuse).
|
||||
type cappedBuffer struct {
|
||||
buf bytes.Buffer
|
||||
limit int
|
||||
truncated bool
|
||||
}
|
||||
|
||||
func (c *cappedBuffer) Write(p []byte) (int, error) {
|
||||
remaining := c.limit - c.buf.Len()
|
||||
if remaining <= 0 {
|
||||
c.truncated = true
|
||||
return len(p), nil // déverse : le process ne bloque pas sur un pipe plein
|
||||
}
|
||||
if len(p) > remaining {
|
||||
c.truncated = true
|
||||
p = p[:remaining]
|
||||
}
|
||||
return c.buf.Write(p)
|
||||
}
|
||||
|
||||
func (c *cappedBuffer) String() string { return c.buf.String() }
|
||||
|
||||
func runTesseract(ctx context.Context, filePath, lang string) (string, error) {
|
||||
// Défense en profondeur (A12) : même si l'appelant ne passe pas par
|
||||
// service.normalizeOcrLang, `-l` ne reçoit jamais de métacaractères.
|
||||
if !validTesseractLang(lang) {
|
||||
return "", fmt.Errorf("tesseract: langue %q invalide", lang)
|
||||
}
|
||||
cmd := exec.CommandContext(ctx, "tesseract", filePath, "stdout", "-l", lang)
|
||||
var stdout, stderr bytes.Buffer
|
||||
cmd.Stdout = &stdout
|
||||
cmd.Stderr = &stderr
|
||||
stdout := &cappedBuffer{limit: outputCap}
|
||||
stderr := &cappedBuffer{limit: outputCap}
|
||||
cmd.Stdout = stdout
|
||||
cmd.Stderr = stderr
|
||||
if err := cmd.Run(); err != nil {
|
||||
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
|
||||
return "", fmt.Errorf("tesseract: délai dépassé (%s): %w", tesseractTimeout, ctx.Err())
|
||||
}
|
||||
if errors.Is(ctx.Err(), context.Canceled) {
|
||||
return "", ctx.Err()
|
||||
}
|
||||
@@ -41,24 +105,37 @@ func runTesseract(ctx context.Context, filePath, lang string) (string, error) {
|
||||
}
|
||||
return "", errors.New("tesseract: " + msg)
|
||||
}
|
||||
if stdout.truncated {
|
||||
return "", fmt.Errorf("tesseract: sortie tronquée (> %d octets)", outputCap)
|
||||
}
|
||||
// stderr tronqué avec un run OK : le texte est complet, mais le diagnostic
|
||||
// est perdu — on l'accepte sans échouer le job.
|
||||
return strings.TrimSpace(stdout.String()), nil
|
||||
}
|
||||
|
||||
func extractPDFText(filePath string) (string, error) {
|
||||
func extractPDFText(ctx context.Context, filePath string) (string, error) {
|
||||
f, r, err := pdf.Open(filePath)
|
||||
if err != nil {
|
||||
return "", errors.New("pdf: " + err.Error())
|
||||
}
|
||||
defer f.Close()
|
||||
if n := r.NumPage(); n > maxPDFPages {
|
||||
return "", fmt.Errorf("pdf: %d pages > plafond %d (tronquer = perdre du texte)", n, maxPDFPages)
|
||||
}
|
||||
var builder strings.Builder
|
||||
for i := 1; i <= r.NumPage(); i++ {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return "", fmt.Errorf("pdf: annulé page %d: %w", i, ctx.Err())
|
||||
default:
|
||||
}
|
||||
p := r.Page(i)
|
||||
if p.V.IsNull() {
|
||||
continue
|
||||
return "", fmt.Errorf("pdf: page %d nulle", i)
|
||||
}
|
||||
plain, err := p.GetPlainText(nil)
|
||||
if err != nil {
|
||||
continue
|
||||
return "", fmt.Errorf("pdf: page %d illisible: %w", i, err)
|
||||
}
|
||||
builder.WriteString(plain)
|
||||
builder.WriteString("\n")
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package ocr
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestValidTesseractLang(t *testing.T) {
|
||||
valid := []string{"fra", "eng", "fra+eng", "de", "abcdefghijklmnopqrst"} // 20 chars
|
||||
for _, lang := range valid {
|
||||
if !validTesseractLang(lang) {
|
||||
t.Errorf("langue %q : attendue valide", lang)
|
||||
}
|
||||
}
|
||||
invalid := []string{
|
||||
"", "e", "fra;rm -rf", "fra eng", "fra+eng --psm",
|
||||
"FRA", "fra-eng", "fra/eng", "abcdefghijklmnopqrstu", // 21 chars
|
||||
"fra\neng", "-l fra",
|
||||
}
|
||||
for _, lang := range invalid {
|
||||
if validTesseractLang(lang) {
|
||||
t.Errorf("langue %q : attendue invalide (injection -l)", lang)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCappedBufferTruncation(t *testing.T) {
|
||||
c := &cappedBuffer{limit: 8}
|
||||
if _, err := c.Write([]byte("12345678")); err != nil {
|
||||
t.Fatalf("write: %v", err)
|
||||
}
|
||||
if c.truncated {
|
||||
t.Error("8 octets pour limite 8 : ne doit pas être tronqué")
|
||||
}
|
||||
if _, err := c.Write([]byte("9")); err != nil {
|
||||
t.Fatalf("write: %v", err)
|
||||
}
|
||||
if !c.truncated {
|
||||
t.Error("9 octets pour limite 8 : doit être marqué tronqué")
|
||||
}
|
||||
if c.String() != "12345678" {
|
||||
t.Errorf("contenu = %q, attendu %q", c.String(), "12345678")
|
||||
}
|
||||
// Écriture massive d'un coup : bornée, jamais plus que `limit`.
|
||||
big := &cappedBuffer{limit: 4}
|
||||
if _, err := big.Write([]byte(strings.Repeat("x", 1<<20))); err != nil {
|
||||
t.Fatalf("write: %v", err)
|
||||
}
|
||||
if !big.truncated || len(big.String()) != 4 {
|
||||
t.Errorf("écriture 1 Mo limite 4 : tronqué=%v len=%d", big.truncated, len(big.String()))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user