snapshot of https://github.com/mattermost/mattermost.git (master, 2026-09-16; blobs > 131072B omitted)
Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
Server CI Nightly Race / Build Environment (push) Failing after 1s
Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
Server CI Nightly Race / Build Environment (push) Failing after 1s
This commit is contained in:
231
server/channels/app/imaging/decode.go
Normal file
231
server/channels/app/imaging/decode.go
Normal file
@@ -0,0 +1,231 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package imaging
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"image"
|
||||
_ "image/gif"
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
"io"
|
||||
"sync"
|
||||
|
||||
_ "golang.org/x/image/bmp"
|
||||
_ "golang.org/x/image/tiff"
|
||||
_ "golang.org/x/image/webp"
|
||||
)
|
||||
|
||||
// DecoderOptions holds configuration options for an image decoder.
|
||||
type DecoderOptions struct {
|
||||
// The level of concurrency for the decoder. This defines a limit on the
|
||||
// number of concurrently running encoding goroutines.
|
||||
ConcurrencyLevel int
|
||||
|
||||
// MaxDecodedResolution, when greater than zero, is the maximum number of
|
||||
// pixels (width*height) an image may declare before it is decoded. Images
|
||||
// exceeding this limit are rejected up front. This is a defense-in-depth
|
||||
// guard against decompression bombs that bounds server-side memory
|
||||
// allocation regardless of the underlying codec's behavior.
|
||||
MaxDecodedResolution int64
|
||||
}
|
||||
|
||||
func (o *DecoderOptions) validate() error {
|
||||
if o.ConcurrencyLevel < 0 {
|
||||
return errors.New("ConcurrencyLevel must be non-negative")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Decoder holds the necessary state to decode images.
|
||||
// This is safe to be used from multiple goroutines.
|
||||
type Decoder struct {
|
||||
sem chan struct{}
|
||||
opts DecoderOptions
|
||||
}
|
||||
|
||||
// NewDecoder creates and returns a new image decoder with the given options.
|
||||
func NewDecoder(opts DecoderOptions) (*Decoder, error) {
|
||||
var d Decoder
|
||||
if err := opts.validate(); err != nil {
|
||||
return nil, fmt.Errorf("imaging: error validating decoder options: %w", err)
|
||||
}
|
||||
if opts.ConcurrencyLevel > 0 {
|
||||
d.sem = make(chan struct{}, opts.ConcurrencyLevel)
|
||||
}
|
||||
d.opts = opts
|
||||
return &d, nil
|
||||
}
|
||||
|
||||
// enforceResolutionLimit inspects the image header and rejects images whose
|
||||
// declared resolution exceeds the configured MaxDecodedResolution before any
|
||||
// pixel data is decoded. It returns the reader to use for the subsequent full
|
||||
// decode: seekable readers are rewound to their original position, while
|
||||
// non-seekable readers are buffered so the cap is enforced for every input.
|
||||
func (d *Decoder) enforceResolutionLimit(rd io.Reader) (io.Reader, error) {
|
||||
if d.opts.MaxDecodedResolution <= 0 {
|
||||
return rd, nil
|
||||
}
|
||||
|
||||
if seeker, ok := rd.(io.ReadSeeker); ok {
|
||||
// Preserve the caller's position so an image decoded from a non-zero
|
||||
// offset still lines up for the full decode.
|
||||
start, err := seeker.Seek(0, io.SeekCurrent)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("imaging: failed to read image position: %w", err)
|
||||
}
|
||||
cfg, _, cfgErr := image.DecodeConfig(seeker)
|
||||
if _, err := seeker.Seek(start, io.SeekStart); err != nil {
|
||||
return nil, fmt.Errorf("imaging: failed to seek after reading image config: %w", err)
|
||||
}
|
||||
if err := d.checkConfigResolution(cfg, cfgErr); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rd, nil
|
||||
}
|
||||
|
||||
// Non-seekable reader: buffer the input so the resolution cap can still be
|
||||
// enforced and the data can be decoded afterwards.
|
||||
data, err := io.ReadAll(rd)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("imaging: failed to read image data: %w", err)
|
||||
}
|
||||
cfg, _, cfgErr := image.DecodeConfig(bytes.NewReader(data))
|
||||
if err := d.checkConfigResolution(cfg, cfgErr); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return bytes.NewReader(data), nil
|
||||
}
|
||||
|
||||
// checkConfigResolution rejects a decoded image config whose resolution exceeds
|
||||
// the configured cap. A config-decode error is ignored so the subsequent full
|
||||
// decode can surface a meaningful error for malformed input.
|
||||
func (d *Decoder) checkConfigResolution(cfg image.Config, cfgErr error) error {
|
||||
if cfgErr != nil {
|
||||
return nil
|
||||
}
|
||||
if exceedsResolution(int64(cfg.Width), int64(cfg.Height), d.opts.MaxDecodedResolution) {
|
||||
return fmt.Errorf("imaging: image resolution %dx%d exceeds the maximum allowed %d pixels", cfg.Width, cfg.Height, d.opts.MaxDecodedResolution)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// exceedsResolution reports whether width*height exceeds maxRes. It divides
|
||||
// instead of multiplying so it can't overflow int64 for very large declared
|
||||
// dimensions.
|
||||
func exceedsResolution(width, height, maxRes int64) bool {
|
||||
if width <= 0 || height <= 0 {
|
||||
return false
|
||||
}
|
||||
return width > maxRes/height
|
||||
}
|
||||
|
||||
// Decode decodes the given encoded data and returns the decoded image.
|
||||
func (d *Decoder) Decode(rd io.Reader) (img image.Image, format string, err error) {
|
||||
rd, err = d.enforceResolutionLimit(rd)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
if d.opts.ConcurrencyLevel != 0 {
|
||||
d.sem <- struct{}{}
|
||||
defer func() { <-d.sem }()
|
||||
}
|
||||
|
||||
img, format, err = image.Decode(rd)
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("imaging: failed to decode image: %w", err)
|
||||
}
|
||||
|
||||
return img, format, nil
|
||||
}
|
||||
|
||||
// DecodeMemBounded works similarly to Decode but also returns a release function that
|
||||
// must be called when access to the raw image is not needed anymore.
|
||||
// This sets the raw image data pointer to nil in an attempt to help the GC to re-use the underlying data as soon as possible.
|
||||
func (d *Decoder) DecodeMemBounded(rd io.Reader) (img image.Image, format string, releaseFunc func(), err error) {
|
||||
rd, err = d.enforceResolutionLimit(rd)
|
||||
if err != nil {
|
||||
return nil, "", nil, err
|
||||
}
|
||||
|
||||
if d.opts.ConcurrencyLevel != 0 {
|
||||
d.sem <- struct{}{}
|
||||
defer func() {
|
||||
if err != nil {
|
||||
<-d.sem
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
img, format, err = image.Decode(rd)
|
||||
if err != nil {
|
||||
return nil, "", nil, fmt.Errorf("imaging: failed to decode image: %w", err)
|
||||
}
|
||||
|
||||
var once sync.Once
|
||||
releaseFunc = func() {
|
||||
if d.opts.ConcurrencyLevel == 0 {
|
||||
return
|
||||
}
|
||||
once.Do(func() {
|
||||
if img != nil {
|
||||
releaseImageData(img)
|
||||
}
|
||||
<-d.sem
|
||||
})
|
||||
}
|
||||
|
||||
return img, format, releaseFunc, nil
|
||||
}
|
||||
|
||||
// DecodeConfig returns the image config for the given data.
|
||||
func (d *Decoder) DecodeConfig(rd io.Reader) (image.Config, string, error) {
|
||||
img, format, err := image.DecodeConfig(rd)
|
||||
if err != nil {
|
||||
return image.Config{}, "", fmt.Errorf("imaging: failed to decode image config: %w", err)
|
||||
}
|
||||
return img, format, nil
|
||||
}
|
||||
|
||||
// GetDimensions returns the dimensions for the given encoded image data.
|
||||
func GetDimensions(imageData io.Reader) (width int, height int, err error) {
|
||||
cfg, _, err := image.DecodeConfig(imageData)
|
||||
width, height = cfg.Width, cfg.Height
|
||||
if seeker, ok := imageData.(io.Seeker); ok {
|
||||
_, err2 := seeker.Seek(0, 0)
|
||||
if err == nil && err2 != nil {
|
||||
err = fmt.Errorf("failed to seek back to the beginning of the image data: %w", err2)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// This is only needed to try and simplify GC work.
|
||||
func releaseImageData(img image.Image) {
|
||||
switch raw := img.(type) {
|
||||
case *image.Alpha:
|
||||
raw.Pix = nil
|
||||
case *image.Alpha16:
|
||||
raw.Pix = nil
|
||||
case *image.Gray:
|
||||
raw.Pix = nil
|
||||
case *image.Gray16:
|
||||
raw.Pix = nil
|
||||
case *image.NRGBA:
|
||||
raw.Pix = nil
|
||||
case *image.NRGBA64:
|
||||
raw.Pix = nil
|
||||
case *image.Paletted:
|
||||
raw.Pix = nil
|
||||
case *image.RGBA:
|
||||
raw.Pix = nil
|
||||
case *image.RGBA64:
|
||||
raw.Pix = nil
|
||||
default:
|
||||
return
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user