[bugfix] Update GetURL to extract url from Link objects with href (#4249)

# Description

> If this is a code change, please include a summary of what you've coded, and link to the issue(s) it closes/implements.
>
> If this is a documentation change, please briefly describe what you've changed and why.

This pull request updates our parsing of the `url` property in incoming ActivityPub items to also include Link items, and not just bare URIs.

The first discovered url is still used as the *gtsmodel.Account or *gtsmodel.Status `url` property, so this change only really affects our dereference URL anti-spoof check thingy.

~~Should fix https://codeberg.org/superseriousbusiness/gotosocial/issues/4248 but I need to run it and test it myself first to be sure.~~

Fixes https://codeberg.org/superseriousbusiness/gotosocial/issues/4248

## Checklist

Please put an x inside each checkbox to indicate that you've read and followed it: `[ ]` -> `[x]`

If this is a documentation change, only the first checkbox must be filled (you can delete the others if you want).

- [x] I/we have read the [GoToSocial contribution guidelines](https://codeberg.org/superseriousbusiness/gotosocial/src/branch/main/CONTRIBUTING.md).
- [x] I/we have discussed the proposed changes already, either in an issue on the repository, or in the Matrix chat.
- [x] I/we have not leveraged AI to create the proposed changes.
- [x] I/we have performed a self-review of added code.
- [x] I/we have written code that is legible and maintainable by others.
- [x] I/we have commented the added code, particularly in hard-to-understand areas.
- [ ] I/we have made any necessary changes to documentation.
- [x] I/we have added tests that cover new code.
- [x] I/we have run tests and they pass locally with the changes.
- [x] I/we have run `go fmt ./...` and `golangci-lint run`.

Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4249
Co-authored-by: tobi <tobi.smethurst@protonmail.com>
Co-committed-by: tobi <tobi.smethurst@protonmail.com>
This commit is contained in:
tobi 2025-06-09 12:32:00 +02:00 committed by tobi
commit 32738d83a8
5 changed files with 332 additions and 38 deletions

View file

@ -357,10 +357,14 @@ func ExtractIconURI(i WithIcon) (*url.URL, error) {
continue
}
imageURL, err := ExtractURL(image)
if err == nil && imageURL != nil {
return imageURL, nil
imageURL := GetURL(image)
if len(imageURL) == 0 {
// Nothing here.
continue
}
// Got a hit.
return imageURL[0], nil
}
return nil, gtserror.New("could not extract valid image URI from icon")
@ -399,10 +403,14 @@ func ExtractImageURI(i WithImage) (*url.URL, error) {
continue
}
imageURL, err := ExtractURL(image)
if err == nil && imageURL != nil {
return imageURL, nil
imageURL := GetURL(image)
if len(imageURL) == 0 {
// Nothing here.
continue
}
// Got a hit.
return imageURL[0], nil
}
return nil, gtserror.New("could not extract valid image URI from image")
@ -488,28 +496,6 @@ func ExtractFields(i WithAttachment) []*gtsmodel.Field {
return fields
}
// ExtractURL extracts the first URI it can find from the
// given WithURL interface, or an error if no URL was set.
// The ID of a type will not work, this function wants a URI
// specifically.
func ExtractURL(i WithURL) (*url.URL, error) {
urlProp := i.GetActivityStreamsUrl()
if urlProp == nil {
return nil, gtserror.New("url property was nil")
}
for iter := urlProp.Begin(); iter != urlProp.End(); iter = iter.Next() {
if !iter.IsIRI() {
continue
}
// Found it.
return iter.GetIRI(), nil
}
return nil, gtserror.New("no valid URL property found")
}
// ExtractPubKeyFromActor extracts the public key, public key ID, and public
// key owner ID from an interface, or an error if something goes wrong.
func ExtractPubKeyFromActor(i WithPublicKey) (
@ -676,15 +662,15 @@ func ExtractAttachments(i WithAttachment) ([]*gtsmodel.MediaAttachment, error) {
// (just remote URL, description, and blurhash) from the given
// Attachmentable interface, or an error if no remote URL is set.
func ExtractAttachment(i Attachmentable) (*gtsmodel.MediaAttachment, error) {
// Get the URL for the attachment file.
// Get the first URL for the attachment file.
// If no URL is set, we can't do anything.
remoteURL, err := ExtractURL(i)
if err != nil {
return nil, gtserror.Newf("error extracting attachment URL: %w", err)
remoteURL := GetURL(i)
if len(remoteURL) == 0 {
return nil, gtserror.New("empty attachment URL")
}
return &gtsmodel.MediaAttachment{
RemoteURL: remoteURL.String(),
RemoteURL: remoteURL[0].String(),
Description: ExtractDescription(i),
Blurhash: ExtractBlurhash(i),
FileMeta: gtsmodel.FileMeta{