forked from mtusi2026/mtusi2026lab5
API принимает 202 после durable staging и TX intent (job+outbox); отдельные relay и worker публикуют/обрабатывают задания с publisher confirm, lease, retry/backoff, DLQ и replay. Добавлены версии pipeline, варианты thumbnail/crop/optimized, presign в verified staging, status-aware sweeper и admission control.
119 lines
2.1 KiB
Go
119 lines
2.1 KiB
Go
package imageproc
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"io"
|
|
)
|
|
|
|
const (
|
|
exifMaxBytes = 64 << 10
|
|
exifTagOrient = 0x0112
|
|
)
|
|
|
|
// readEXIFOrientation extracts only IFD0 Orientation from a JPEG APP1 segment.
|
|
// The parser is deliberately small and bounded: no TIFF offsets are trusted and
|
|
// the whole segment is capped at 64 KiB, so attacker-controlled EXIF cannot make
|
|
// the worker allocate unbounded memory.
|
|
func readEXIFOrientation(r io.Reader) int {
|
|
data, err := io.ReadAll(io.LimitReader(r, exifMaxBytes))
|
|
if err != nil || len(data) < 4 || data[0] != 0xff || data[1] != 0xd8 {
|
|
return 1
|
|
}
|
|
|
|
pos := 2
|
|
|
|
for pos+4 <= len(data) {
|
|
if data[pos] != 0xff {
|
|
return 1
|
|
}
|
|
|
|
marker := data[pos+1]
|
|
pos += 2
|
|
|
|
// Standalone markers without a length field.
|
|
if marker == 0xd8 || marker == 0xd9 || marker == 0x01 || (marker >= 0xd0 && marker <= 0xd7) {
|
|
continue
|
|
}
|
|
|
|
if pos+2 > len(data) {
|
|
return 1
|
|
}
|
|
|
|
length := int(binary.BigEndian.Uint16(data[pos : pos+2]))
|
|
if length < 2 || pos+length > len(data) {
|
|
return 1
|
|
}
|
|
|
|
segment := data[pos+2 : pos+length]
|
|
pos += length
|
|
|
|
if marker != 0xe1 || len(segment) < 6 {
|
|
continue
|
|
}
|
|
|
|
if string(segment[:6]) != "Exif\x00\x00" {
|
|
continue
|
|
}
|
|
|
|
return parseTIFFOrientation(segment[6:])
|
|
}
|
|
|
|
return 1
|
|
}
|
|
|
|
func parseTIFFOrientation(tiff []byte) int {
|
|
if len(tiff) < 8 {
|
|
return 1
|
|
}
|
|
|
|
var order binary.ByteOrder
|
|
|
|
switch string(tiff[:2]) {
|
|
case "II":
|
|
order = binary.LittleEndian
|
|
case "MM":
|
|
order = binary.BigEndian
|
|
default:
|
|
return 1
|
|
}
|
|
|
|
if order.Uint16(tiff[2:4]) != 42 {
|
|
return 1
|
|
}
|
|
|
|
ifdOffset := int(order.Uint32(tiff[4:8]))
|
|
if ifdOffset < 8 || ifdOffset+2 > len(tiff) {
|
|
return 1
|
|
}
|
|
|
|
count := int(order.Uint16(tiff[ifdOffset : ifdOffset+2]))
|
|
if count > 512 {
|
|
count = 512
|
|
}
|
|
|
|
entry := ifdOffset + 2
|
|
|
|
for range count {
|
|
if entry+12 > len(tiff) {
|
|
return 1
|
|
}
|
|
|
|
tag := order.Uint16(tiff[entry : entry+2])
|
|
typ := order.Uint16(tiff[entry+2 : entry+4])
|
|
n := order.Uint32(tiff[entry+4 : entry+8])
|
|
|
|
if tag == exifTagOrient && typ == 3 && n == 1 {
|
|
value := order.Uint16(tiff[entry+8 : entry+10])
|
|
if value >= 1 && value <= 8 {
|
|
return int(value)
|
|
}
|
|
|
|
return 1
|
|
}
|
|
|
|
entry += 12
|
|
}
|
|
|
|
return 1
|
|
}
|