218 lines
6.5 KiB
Go
218 lines
6.5 KiB
Go
package fbhttp
|
|
|
|
import (
|
|
"archive/zip"
|
|
"bytes"
|
|
"compress/gzip"
|
|
"encoding/binary"
|
|
"encoding/json"
|
|
"encoding/xml"
|
|
"errors"
|
|
"image"
|
|
_ "image/gif"
|
|
_ "image/jpeg"
|
|
_ "image/png"
|
|
"io"
|
|
"os"
|
|
"path"
|
|
"strings"
|
|
|
|
_ "golang.org/x/image/bmp"
|
|
_ "golang.org/x/image/tiff"
|
|
_ "golang.org/x/image/webp"
|
|
)
|
|
|
|
func validateCreativeDocument(extension string, data []byte) (bool, error) {
|
|
invalid := errors.New("invalid or oversized creative document")
|
|
if len(data) == 0 {
|
|
return true, invalid
|
|
}
|
|
switch extension {
|
|
case ".png", ".jpg", ".jpeg", ".gif", ".webp", ".bmp", ".tif", ".tiff":
|
|
config, format, err := image.DecodeConfig(bytes.NewReader(data))
|
|
expected := map[string]string{".png": "png", ".jpg": "jpeg", ".jpeg": "jpeg", ".gif": "gif", ".webp": "webp", ".bmp": "bmp", ".tif": "tiff", ".tiff": "tiff"}[extension]
|
|
if err != nil || format != expected || config.Width < 1 || config.Height < 1 || config.Width > 100000 || config.Height > 100000 || uint64(config.Width)*uint64(config.Height) > 100000000 {
|
|
return true, invalid
|
|
}
|
|
return true, nil
|
|
case ".psd", ".psb":
|
|
version := uint16(1)
|
|
if extension == ".psb" {
|
|
version = 2
|
|
}
|
|
if len(data) < 26 || string(data[:4]) != "8BPS" || binary.BigEndian.Uint16(data[4:6]) != version {
|
|
return true, invalid
|
|
}
|
|
h, w := binary.BigEndian.Uint32(data[14:18]), binary.BigEndian.Uint32(data[18:22])
|
|
if h == 0 || w == 0 || uint64(h)*uint64(w) > 100000000 {
|
|
return true, invalid
|
|
}
|
|
return true, nil
|
|
case ".svg":
|
|
decoder := xml.NewDecoder(bytes.NewReader(data))
|
|
root, depth := false, 0
|
|
for {
|
|
token, err := decoder.Token()
|
|
if err == io.EOF {
|
|
break
|
|
}
|
|
if err != nil {
|
|
return true, invalid
|
|
}
|
|
switch node := token.(type) {
|
|
case xml.Directive:
|
|
return true, invalid // No DTD or external entities.
|
|
case xml.StartElement:
|
|
if root && depth == 0 {
|
|
return true, invalid
|
|
}
|
|
if !root {
|
|
if node.Name.Local != "svg" || (node.Name.Space != "" && node.Name.Space != "http://www.w3.org/2000/svg") {
|
|
return true, invalid
|
|
}
|
|
root = true
|
|
}
|
|
depth++
|
|
if depth > 128 {
|
|
return true, invalid
|
|
}
|
|
case xml.EndElement:
|
|
depth--
|
|
}
|
|
}
|
|
if !root {
|
|
return true, invalid
|
|
}
|
|
return true, nil
|
|
case ".pcraft", ".figcraft", ".designcraft", ".idml", ".scraftz", ".deckcraft":
|
|
// The server never extracts project archives. Embedded assets count toward the same budget.
|
|
main := map[string]string{".pcraft": "manifest.json", ".figcraft": "document.json", ".designcraft": "document.json", ".idml": "designmap.xml", ".scraftz": "*.scraft", ".deckcraft": "presentation.json"}[extension]
|
|
// FigCraft and older DesignCraft also accept a single JSON document.
|
|
if (extension == ".figcraft" || extension == ".designcraft") && !bytes.HasPrefix(data, []byte("PK")) {
|
|
if !json.Valid(data) {
|
|
return true, invalid
|
|
}
|
|
return true, nil
|
|
}
|
|
return true, validateCreativeArchive(data, main)
|
|
case ".vectorcraft", ".drawcraft":
|
|
payload := data
|
|
if bytes.HasPrefix(data, []byte{0x1f, 0x8b}) {
|
|
reader, err := gzip.NewReader(bytes.NewReader(data))
|
|
if err != nil {
|
|
return true, invalid
|
|
}
|
|
payload, err = io.ReadAll(io.LimitReader(reader, maxCraftDocument+1))
|
|
reader.Close()
|
|
if err != nil || len(payload) > maxCraftDocument {
|
|
return true, invalid
|
|
}
|
|
}
|
|
var document struct {
|
|
Format string `json:"format"`
|
|
}
|
|
decoder := json.NewDecoder(bytes.NewReader(payload))
|
|
if decoder.Decode(&document) != nil || (document.Format != "vectorcraft" && document.Format != "drawcraft") {
|
|
return true, invalid
|
|
}
|
|
// Recent formats append bounded binary image data after the JSON value.
|
|
tail := bytes.TrimSpace(payload[decoder.InputOffset():])
|
|
if len(tail) > 0 && !bytes.HasPrefix(tail, []byte("\x00VCBLOBS\x00")) {
|
|
return true, invalid
|
|
}
|
|
return true, nil
|
|
case ".dxf":
|
|
if bytes.HasPrefix(data, []byte("AutoCAD Binary DXF\r\n\x1a\x00")) {
|
|
return true, nil
|
|
}
|
|
text := string(bytes.TrimSpace(data))
|
|
if !strings.HasPrefix(text, "0") || !strings.Contains(text, "SECTION") || !strings.HasSuffix(text, "EOF") {
|
|
return true, invalid
|
|
}
|
|
return true, nil
|
|
case ".dwg":
|
|
if len(data) < 6 || !bytes.HasPrefix(data, []byte("AC10")) {
|
|
return true, invalid
|
|
}
|
|
return true, nil
|
|
case ".cr3", ".m4a":
|
|
if len(data) < 12 || string(data[4:8]) != "ftyp" {
|
|
return true, invalid
|
|
}
|
|
return true, nil
|
|
case ".dng", ".cr2", ".nef", ".nrw", ".arw", ".pef", ".orf", ".rw2":
|
|
if len(data) < 8 || !(bytes.HasPrefix(data, []byte("II")) || bytes.HasPrefix(data, []byte("MM"))) {
|
|
return true, invalid
|
|
}
|
|
return true, nil
|
|
case ".raf":
|
|
if !bytes.HasPrefix(data, []byte("FUJIFILMCCD-RAW")) {
|
|
return true, invalid
|
|
}
|
|
return true, nil
|
|
case ".wav":
|
|
if len(data) < 12 || !(string(data[:4]) == "RIFF" || string(data[:4]) == "RF64") || string(data[8:12]) != "WAVE" {
|
|
return true, invalid
|
|
}
|
|
return true, nil
|
|
case ".flac":
|
|
if !bytes.HasPrefix(data, []byte("fLaC")) {
|
|
return true, invalid
|
|
}
|
|
return true, nil
|
|
case ".ogg":
|
|
if !bytes.HasPrefix(data, []byte("OggS")) {
|
|
return true, invalid
|
|
}
|
|
return true, nil
|
|
case ".mp3":
|
|
if len(data) < 3 || !(bytes.HasPrefix(data, []byte("ID3")) || data[0] == 0xff && data[1]&0xe0 == 0xe0) {
|
|
return true, invalid
|
|
}
|
|
return true, nil
|
|
case ".aif", ".aiff":
|
|
if len(data) < 12 || string(data[:4]) != "FORM" || !(string(data[8:12]) == "AIFF" || string(data[8:12]) == "AIFC") {
|
|
return true, invalid
|
|
}
|
|
return true, nil
|
|
case ".mid", ".midi":
|
|
if !bytes.HasPrefix(data, []byte("MThd")) {
|
|
return true, invalid
|
|
}
|
|
return true, nil
|
|
}
|
|
return false, nil
|
|
}
|
|
|
|
func validateCreativeArchive(data []byte, required string) error {
|
|
invalid := errors.New("invalid or oversized project archive")
|
|
reader, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if len(reader.File) > 4096 {
|
|
return invalid
|
|
}
|
|
var total uint64
|
|
found := false
|
|
seen := map[string]bool{}
|
|
for _, file := range reader.File {
|
|
if file.UncompressedSize64 > 64<<20 || file.UncompressedSize64 > (256<<20)-total {
|
|
return invalid
|
|
}
|
|
total += file.UncompressedSize64
|
|
name := strings.TrimSuffix(file.Name, "/")
|
|
if name == "" || strings.ContainsAny(name, "\\\x00:") || path.IsAbs(name) || path.Clean(name) != name || name == ".." || strings.HasPrefix(name, "../") || seen[strings.ToLower(name)] || file.Mode()&os.ModeSymlink != 0 || file.Flags&1 != 0 {
|
|
return invalid
|
|
}
|
|
seen[strings.ToLower(name)] = true
|
|
if file.UncompressedSize64 > 0 && (file.Name == required || required == "*.scraft" && strings.HasSuffix(file.Name, ".scraft")) {
|
|
found = true
|
|
}
|
|
}
|
|
if !found {
|
|
return invalid
|
|
}
|
|
return nil
|
|
}
|