Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly handle the name query param in create issue attachment API #30778

Conversation

kemzeb
Copy link
Contributor

@kemzeb kemzeb commented Apr 30, 2024

Fixes #30766 by passing both the original file name and an attachment name to the lower API calls and performing verification on just the original file name. This behavior is pretty much in-line with how we handle attachment names in EditIssueAttachment(), where we don't perform any verification against the new name and just go straight to the DB to change it.

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Apr 30, 2024
@pull-request-size pull-request-size bot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Apr 30, 2024
@github-actions github-actions bot added modifies/api This PR adds API routes or modifies them modifies/go Pull requests that update Go code labels Apr 30, 2024

// UploadAttachmentWithCustomizableName is like UploadAttachment, but you pass both the original file name and the attachment name
// (which could be the same).
func UploadAttachmentWithCustomizableName(ctx context.Context, file io.Reader, allowedTypes string, fileSize int64, originalFileName string, attach *repo_model.Attachment) (*repo_model.Attachment, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function has almost the exact same definition as UploadAttachment(). I would need to tweak a couple areas if I were to change its signature so if that's alright to do let me know!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should change UploadAttachment and fix all the places but not only this one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed UploadAttachment() and updated the affected areas 👍

@kemzeb
Copy link
Contributor Author

kemzeb commented Apr 30, 2024

An example using the swagger API:

# URL
http://localhost:3000/api/v1/repos/korvax/test-repo/issues/1/assets?name=MyNameIsNobody

# Response
{
  "id": 5,
  "name": "MyNameIsNobody",
  "size": 12,
  "download_count": 0,
  "created_at": "2024-04-30T07:32:31Z",
  "uuid": "132694fe-f70c-428e-82d1-66edc88a80b2",
  "browser_download_url": "http://localhost:3000/attachments/132694fe-f70c-428e-82d1-66edc88a80b2"
}

@pull-request-size pull-request-size bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Apr 30, 2024
ctx.Error(http.StatusBadRequest, "CreateReleaseAttachment", "Could not determine name of attachment.")
return
}

// Create a new attachment and save the file
attach, err := attachment.UploadAttachment(ctx, content, setting.Repository.Release.AllowedTypes, size, &repo_model.Attachment{
Name: filename,
attach, err := attachment.UploadAttachment(ctx, content, setting.Repository.Release.AllowedTypes, size, attachmentName, &repo_model.Attachment{
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original file name string is hiding within an if block that I can't reach. The original behavior shouldn't have changed, but we may encounter the same problem as the issue we're trying to fix had

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can still also have a variable named filename and assign it like you did for attachmentName

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, and I fallback to assigning attachmentName to it in the else block

@lunny
Copy link
Member

lunny commented Apr 30, 2024

Maybe we should have a filename on the Attachment struct as a final resolution?

Copy link
Member

@KN4CK3R KN4CK3R left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now you can bypass the extension check by uploading file.good with ?name=file.bad.

@GiteaBot GiteaBot added lgtm/blocked A maintainer has reservations with the PR and thus it cannot be merged and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Apr 30, 2024
@kemzeb
Copy link
Contributor Author

kemzeb commented Apr 30, 2024

Now you can bypass the extension check by uploading file.good with ?name=file.bad.

Ah, I think I might not have understood well enough/was confused why we looked at file extensions (when we already performed MIME sniffing). Now, it looks to me like we perform these extension checks to reject extensions that may look malicious to users or otherwise undesirable (e.g. file.exe).

If this is the case, would it be alright if I close this PR and open a new one that performs a extension check when users edit attachment names (as it appears that we don't do it)? Thank you both for the reviews!

@kemzeb
Copy link
Contributor Author

kemzeb commented May 8, 2024

Going to close this as bypassing the file extension check will surprise admins since they expect either the default extensions we provide or their own to be honored.

@kemzeb kemzeb closed this May 8, 2024
@kemzeb kemzeb deleted the handle-naming-when-creating-issue-attachments branch May 8, 2024 17:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lgtm/blocked A maintainer has reservations with the PR and thus it cannot be merged modifies/api This PR adds API routes or modifies them modifies/go Pull requests that update Go code size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot create issue attachment via the API when using query parameter
4 participants