Skip to content

Add security improvements to the codebase#2

Open
hungvminh wants to merge 2 commits into
tiendc:mainfrom
hungvminh:main
Open

Add security improvements to the codebase#2
hungvminh wants to merge 2 commits into
tiendc:mainfrom
hungvminh:main

Conversation

@hungvminh
Copy link
Copy Markdown

Add security improvements to the codebase.

  • linkpreview.go: Add input validation for link parameter in Parse and ParseFromReader functions. Add security headers to HTTP request in Parse function.
  • parser.go: Add error handling for potential security vulnerabilities in Parse function. Add validation for MetaTags and LinkTags in readAllTags function.
  • parser_og.go: Add validation for tagContent in parseOGMeta, parseOGImages, and parseOGVideos functions.
  • parser_twitter.go: Add validation for tagContent in parseTwitterMeta function.
  • parser_favicons.go: Add validation for tag.Href in parseMaybeFavicon function.
  • README.md: Add section on security guidelines and best practices.
  • go.mod: Add security-related dependencies.
  • go.sum: Update to include new dependencies.

Add security improvements to the codebase.

* **linkpreview.go**: Add input validation for `link` parameter in `Parse` and `ParseFromReader` functions. Add security headers to HTTP request in `Parse` function.
* **parser.go**: Add error handling for potential security vulnerabilities in `Parse` function. Add validation for `MetaTags` and `LinkTags` in `readAllTags` function.
* **parser_og.go**: Add validation for `tagContent` in `parseOGMeta`, `parseOGImages`, and `parseOGVideos` functions.
* **parser_twitter.go**: Add validation for `tagContent` in `parseTwitterMeta` function.
* **parser_favicons.go**: Add validation for `tag.Href` in `parseMaybeFavicon` function.
* **README.md**: Add section on security guidelines and best practices.
* **go.mod**: Add security-related dependencies.
* **go.sum**: Update to include new dependencies.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/hungvminh/go-linkpreview?shareId=XXXX-XXXX-XXXX-XXXX).
Improve security in linkpreview package
@sonarqubecloud
Copy link
Copy Markdown

Comment thread linkpreview.go
Comment on lines +32 to +34
request.Header.Set("X-Content-Type-Options", "nosniff")
request.Header.Set("X-Frame-Options", "DENY")
request.Header.Set("X-XSS-Protection", "1; mode=block")
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I think this is incorrect usage of secure headers. They are for response headers only.
Please see: https://cheatsheetseries.owasp.org/cheatsheets/HTTP_Headers_Cheat_Sheet.html

Comment thread linkpreview.go
}

func Parse(link string, options ...ConfigOption) (ParserResult, error) {
if err := validateURL(link); err != nil {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Maybe this is not necessary as http.NewRequest() and http.DefaultClient.Do() should handle the invalid URL correctly.

Comment thread parser_favicons.go
Comment on lines +32 to +36
wh := strings.Split(tag.Sizes, "x")
if len(wh) == 2 {
image.Width = parseInt(wh[0])
image.Height = parseInt(wh[1])
}
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Please restore indentation of this block.

Comment thread parser.go
}
}

func validateMetaTag(tag *MetaTag) error {
Copy link
Copy Markdown
Owner

@tiendc tiendc Oct 30, 2024

Choose a reason for hiding this comment

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

HTML content may be not correct 100%. The use of this function can cause error to be returned due to a single incorrect piece of data. I suggest adding a config item to ParserConfig, says SkipInvalidTags, with default value false (to be back-ward compatible). When it is turned on, every invalid tags will be ignored instead of returning error to client.

Comment thread parser.go

func validateMetaTag(tag *MetaTag) error {
if tag.Name == "" && tag.Property == "" {
return errors.New("meta tag must have either a name or property attribute")
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

It is recommended not to use dynamic error. You should place this error in top of the file like:

var (
     errTagInvalid = errors.New("tag is invalid")
)

// In here
return fmt.Errorf("%w: meta tag must have either a name or property attribute", errTagInvalid)

Another option is to make this function just return bool as we only need to know whether we should keep the tag or not.

Comment thread parser.go
return nil
}

func validateLinkTag(tag *LinkTag) error {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

The same comment for this function.

Comment thread linkpreview.go
"github.com/PuerkitoBio/goquery"
)

func validateURL(link string) error {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Please move this to utils.go

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants