Skip to content

c.SaveUploadedFile permissions #4068

Open

Description

@reversersed

Description

I run into problem when I saving uploaded file to a folder and trying to get it from nginx it gives me permission error.
I found out that folders created by SaveUploadedFile function has 0750 rights, which I can't get access to.
After changing folder permissions problem is gone. Is there some way to specify the permissions for folders created besides manually creating it or chaning permissions by hand?

How to reproduce

package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

func main() {
	g := gin.Default()
	g.POST("/upload", func(c *gin.Context) {
	        form, _ := c.MultipartForm()
                file := form.File["file"]

		c.SaveUploadedFile(file, "data/files/img.png")
                c.Status(http.StatusCreated)
	})
	g.Run(":9000")
}

Expectations

I excepted there is some method that allows to specify folder permissions I need (e.g. SaveUploadedFile(file *multipart.FileHeader, dst string, perm ...os.FileMode)).
You could specify permissions you need or leave it empty and get default (as it now) permissions.

Actual result

I got hard-written permissions made by source code:

if err = os.MkdirAll(filepath.Dir(dst), 0750); err != nil {
		return err
	}

Environment

  • go version: 1.22
  • gin version (or commit ref): v1.10.0
  • operating system: linux/windows

Activity

  1. haesuo566 commented on Oct 27, 2024

    @haesuo566
    Contributor

    Are you saying that we need a method that can provide additional permissions to the SaveUploadedFile method?

  2. reversersed commented on Oct 29, 2024

    @reversersed
    Author

    Are you saying that we need a method that can provide additional permissions to the SaveUploadedFile method?

    We could have an additional method like SaveUploadedFileWithPermission which can allow us to decide what permissions we need for file are. Or just modify existing method adding 3rd parameter allowing us to override default permissions. The details is up to maintainers.

  3. haesuo566 commented on Oct 30, 2024

    @haesuo566
    Contributor

    Are you saying that we need a method that can provide additional permissions to the SaveUploadedFile method?

    We could have an additional method like SaveUploadedFileWithPermission which can allow us to decide what permissions we need for file are. Or just modify existing method adding 3rd parameter allowing us to override default permissions. The details is up to maintainers.

    Can i work this issue?

  4. reversersed commented on Nov 1, 2024

    @reversersed
    Author

    Are you saying that we need a method that can provide additional permissions to the SaveUploadedFile method?

    We could have an additional method like SaveUploadedFileWithPermission which can allow us to decide what permissions we need for file are. Or just modify existing method adding 3rd parameter allowing us to override default permissions. The details is up to maintainers.

    Can i work this issue?

    Well, ofc, it's all up to you

  5. phplego commented on Apr 7, 2026

    @phplego

    Thanks for the change adding the optional perm argument - it solves a real use case.

    However, the current implementation applies os.Chmod unconditionally, even when perm is not explicitly provided. This leads to unexpected behavior and breaks backward compatibility (e.g., when writing to /tmp or other existing directories).

    Would it make sense to limit chmod to cases where perm is explicitly passed?

    In other words:

    • if perm is provided --> apply chmod
    • if perm is not provided --> preserve previous behavior (no chmod on existing directories)

    This would better match the semantics of an optional parameter and avoid breaking existing code.

    An alternative (even safer) approach could be:

    Both approaches would prevent unintended permission changes on existing system-managed directories.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ 换网址