gitbeaker icon indicating copy to clipboard operation
gitbeaker copied to clipboard

PackageRegistry.publish not properly uploading files from html file input

Open BurritoSpray opened this issue 9 months ago • 6 comments

PackageRegistry.publish not properly uploading files from html file input

  • Node.js version: Chrome Browser M124 (electron v30.0.1)
  • Gitbeaker version: 40.0.3
  • Gitbeaker release (cli, rest, core, requester-utils): rest
  • OS & version: Linux Mint 21.2 Cinnamon

When publishing a generic package the multipart header is not removed so the integrity of the file is compromised. Its working as expected when using directly the gitlab API with fetch, but when im doing the same with gitbeaker the file still has the headers for multipart stuff in it.

Maybe its just me doing it wrong but I haven't seen any example in the documentation about publishing a package.

Here's the code i used to get the issue

    const handleSubmit = async (e) => {
        e.preventDefault();
        const api = data.api;
        const project = data.project;

        // Validate inputs
        if (files === null || packageName === "" || tagName === "") {
            return;
        }

        // Upload the files one by one
        for (let file of files){
            try{
                const result = await api.PackageRegistry.publish(
                    project.id,
                    packageName,
                    tagName,
                    {
                        filename: file.name,
                        content: file
                    },
                    {
                        contentType: "multipart/form-data",
                        select: "package_file",
                        status: "default"
                    }
                )
                console.log(result);
            } catch (e) {
                console.error(e);
            }

        }
    }

Here's the headers im talking about Screenshot from 2024-04-30 13-02-30

Working example with fetch

    const handleSubmit = async (e) => {
        e.preventDefault();
        const api = data.api;
        const project = data.project;
        const token = await window.git.getToken();
        const url = await window.git.getGitURL();

        // Validate inputs
        if (files === null || packageName === "" || tagName === "") {
            return;
        }

        // Upload the files one by one
        for (let file of files){
            try{
                const response = await fetch(new URL(`/api/v4/projects/${project.id}/packages/generic/${packageName}/${tagName}/${file.name}?status=default&select=package_file`, url),{
                    method: "PUT",
                    headers: {
                        "Content-Type": "multipart/form-data",
                        "Authorization": `Bearer ${token}`
                    },
                    body: file
                });

                console.log(`Uploaded new package: ${await response.json()}`);
            } catch (e) {
                console.error(e);
            }

        }
    }

Result with fetch Screenshot from 2024-04-30 13-14-37

Steps to reproduce Try to upload a binary file from an html file input

Expected behaviour The data should be the same as the original file

Actual behaviour The headers are not removed so the file is no longer the same as the original

Possible fixes The contentType in the options does not seems to be doing anything no matter what I put the result is the same, it looks like it defaults to application/octet-steam

Checklist

  • [x] I have checked that this is not a duplicate issue.
  • [x] I have read the documentation.

BurritoSpray avatar Apr 30 '24 18:04 BurritoSpray