koltyakov / gosip

⚡️ SharePoint SDK for Go
https://go.spflow.com
MIT License
144 stars 32 forks source link

`index out of range [0] with length 0` at function `checkGetRelativeURL` when site url does not have .com #70

Closed bangbaew closed 8 months ago

bangbaew commented 8 months ago

Describe the bug This is a bug in code, not a bug SharePoint Server, When site url is set to

"siteUrl": "https://www.contoso.com/sites/test"

this function works fine, but when the site url is set like this (does not have .com, i don't know how it relates)

"siteUrl": "https://www.contoso/sites/test"

the program panics at function

// checkGetRelativeURL checks if URL is relative, prepends relative part if missed
func checkGetRelativeURL(relativeURI string, ctxURL string) string {
    // Prepend web relative URL to "Lists/ListPath" URIs
    if string([]rune(relativeURI)[0]) != "/" {
        absoluteURL := getPriorEndpoint(ctxURL, "/_api")
        relativeURL := getRelativeURL(absoluteURL)
        relativeURI = fmt.Sprintf("%s/%s", relativeURL, relativeURI)
    }
    return relativeURI
}

this is because the string relativeURI somehow becomes empty in the recursions, and []rune(relativeURI)[0] becomes a null value, panicking the program, so I tried debugging it this way

// checkGetRelativeURL checks if URL is relative, prepends relative part if missed
func checkGetRelativeURL(relativeURI string, ctxURL string) string {
    // Prepend web relative URL to "Lists/ListPath" URIs
    if relativeURI != "" {
        if string([]rune(relativeURI)[0]) != "/" {
            absoluteURL := getPriorEndpoint(ctxURL, "/_api")
            relativeURL := getRelativeURL(absoluteURL)
            relativeURI = fmt.Sprintf("%s/%s", relativeURL, relativeURI)
        }
    } else {
        panic("relativeURI is empty")
    }

    return relativeURI
}

it panics panic: relativeURI is empty as expected

Versions SharePoint 2019

To Reproduce Use site url that does not have .com ex: https://sharepointHost:8080/sites/TestSite

{
    "siteUrl": "https://www.contoso/sites/test",
    "username": "contoso\\john.doe",
    "password": "this-is-not-a-real-password"
}

and call function EnsureFolderByPath

sp.Web().EnsureFolderByPath("/Test Path") // doesn't panic if "Test Path" does not have "/" prefix

Expected behavior No runtime panic, handling empty string correctly

Screenshots

panic: runtime error: index out of range [0] with length 0

goroutine 1 [running]:
github.com/koltyakov/gosip/api.checkGetRelativeURL({0x6dc6c7, 0x0}, {0xc0001200f0, 0x27})
        /go/pkg/mod/github.com/koltyakov/gosip@v0.0.0-20231220150131-60203a6b7c36/api/utils.go:107 +0x14b
github.com/koltyakov/gosip/api.(*Web).GetFolderByPath(0xc000115320, {0x6dc6c7?, 0x2?})
        /go/pkg/mod/github.com/koltyakov/gosip@v0.0.0-20231220150131-60203a6b7c36/api/web.go:296 +0x3d
github.com/koltyakov/gosip/api.ensureFolder.func1({0x6dc6c7?, 0xc00020c060?})
        /go/pkg/mod/github.com/koltyakov/gosip@v0.0.0-20231220150131-60203a6b7c36/api/folder.go:194 +0x37
github.com/koltyakov/gosip/api.ensureFolder(0xc000115320, {0x6dc6c7, 0xa}, {0x6dc6c7, 0x0}, {0x6db70d, 0x6})
        /go/pkg/mod/github.com/koltyakov/gosip@v0.0.0-20231220150131-60203a6b7c36/api/folder.go:201 +0x355
github.com/koltyakov/gosip/api.ensureFolder(0xc000115320, {0x6dc6c7, 0xa}, {0x6dc6c7, 0xa}, {0x6db70d, 0x6})
        /go/pkg/mod/github.com/koltyakov/gosip@v0.0.0-20231220150131-60203a6b7c36/api/folder.go:209 +0x42d
github.com/koltyakov/gosip/api.(*Web).EnsureFolderByPath(...)
        /go/pkg/mod/github.com/koltyakov/gosip@v0.0.0-20231220150131-60203a6b7c36/api/web.go:319
main.main()
        /root/app/main.go:441 +0x3e
exit status 2

Additional context I'll create a pull request for a workaround (handling if relativeURI != "" ), not sure if I handled it correctly, so please review it. Thanks

bangbaew commented 8 months ago

Opened a pull request at #71 , please review

koltyakov commented 8 months ago

Thank you for catching this up!

I pushed a fix and a test for the edge case: https://github.com/koltyakov/gosip/pull/72. The fix is a bit different from your PR, as the expectation from the function is different.

The design of checkGetRelativeURL is:

When site URL or list/folder/etc URIs provided with incorrect values it's expected to get failures.

P.S. I'm not sure, why you providing site URL like https://www.contoso/sites/test this is definitely sounds a misconfiguration. The base URL should be a valid web app URL.

bangbaew commented 8 months ago

https://www.contoso/sites/test it isn't a valid URL, it's just an example of a site url that caused app panic, my sharepoint hostname has a port, so I tried to use this as example, sorry.