Skip to content

Commit

Permalink
Merge pull request #448 from atc0005/i442-markdown-formatted-urls
Browse files Browse the repository at this point in the history
Fix decoding of Markdown formatted URLs
  • Loading branch information
atc0005 authored Jul 16, 2024
2 parents 8cb3be0 + 19b7d29 commit 5431bf2
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 1 deletion.
46 changes: 46 additions & 0 deletions internal/safelinks/exported_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ var (
//go:embed testdata/output/standalone/decoded-list-of-urls-with-lf-eol.txt
outputDecodedListOfURLsWithLFEol string

//go:embed testdata/output/standalone/decoded-list-of-markdown-formatted-urls-with-crlf-eol.txt
outputDecodedListOfMarkdownFormattedURLsWithCRLFEol string

//go:embed testdata/output/standalone/decoded-list-of-markdown-formatted-urls-with-lf-eol.txt
outputDecodedListOfMarkdownFormattedURLsWithLFEol string

//go:embed testdata/output/standalone/decoded-single-url-with-angle-brackets-with-crlf-eol.txt
outputDecodedSingleURLWithAngleBracketsWithCRLFEol string

Expand Down Expand Up @@ -129,6 +135,12 @@ var (

//go:embed testdata/input/encoded-all/list-of-urls-with-lf-eol.txt
inputEncodedAllListOfURLsWithLFEol string

//go:embed testdata/input/encoded-all/list-of-markdown-formatted-urls-with-crlf-eol.txt
inputEncodedAllListOfMarkdownFormattedURLsWithCRLFEol string

//go:embed testdata/input/encoded-all/list-of-markdown-formatted-urls-with-lf-eol.txt
inputEncodedAllListOfMarkdownFormattedURLsWithLFEol string
)

// The format used by the test files is VERY specific; trailing space plus
Expand Down Expand Up @@ -248,6 +260,14 @@ func TestURLsFindsAllValidURLs(t *testing.T) {
input: inputEncodedAllListOfURLsWithCRLFEol,
foundURLsCount: 17,
},
"Encoded list of Markdown formatted URLs with LF EOL": {
input: inputEncodedAllListOfMarkdownFormattedURLsWithLFEol,
foundURLsCount: 2,
},
"Encoded list of Markdown formatted URLs with CRLF EOL": {
input: inputEncodedAllListOfMarkdownFormattedURLsWithCRLFEol,
foundURLsCount: 2,
},
"Encoded single URL with angle brackets with LF EOL": {
input: inputEncodedSingleSafelinksURLWithAngleBracketsWithLFEol,
foundURLsCount: 1,
Expand Down Expand Up @@ -489,6 +509,16 @@ func TestFilterURLsCorrectlyFiltersByType(t *testing.T) {
foundEncodedLinksURLsCount: 17,
foundUnencodedURLsCount: 0,
},
"Encoded list of Markdown formatted URLs with CRLF EOL": {
input: inputEncodedAllListOfMarkdownFormattedURLsWithCRLFEol,
foundEncodedLinksURLsCount: 2,
foundUnencodedURLsCount: 0,
},
"Encoded list of Markdown formatted URLs with LF EOL": {
input: inputEncodedAllListOfMarkdownFormattedURLsWithLFEol,
foundEncodedLinksURLsCount: 2,
foundUnencodedURLsCount: 0,
},
"Encoded single URL with angle brackets with LF EOL": {
input: inputEncodedSingleSafelinksURLWithAngleBracketsWithLFEol,
foundEncodedLinksURLsCount: 1,
Expand Down Expand Up @@ -658,6 +688,14 @@ func TestSafeLinkURLsFindsAllValidSafeLinks(t *testing.T) {
input: inputEncodedAllListOfURLsWithLFEol,
foundEncodedURLsCount: 17,
},
"Encoded list of Markdown formatted URLs with CRLF EOL": {
input: inputEncodedAllListOfMarkdownFormattedURLsWithCRLFEol,
foundEncodedURLsCount: 2,
},
"Encoded list of Markdown formatted URLs with LF EOL": {
input: inputEncodedAllListOfMarkdownFormattedURLsWithLFEol,
foundEncodedURLsCount: 2,
},
"Encoded single URL with angle brackets with LF EOL": {
input: inputEncodedSingleSafelinksURLWithAngleBracketsWithLFEol,
foundEncodedURLsCount: 1,
Expand Down Expand Up @@ -753,6 +791,14 @@ func TestDecodeInputSucceedsForValidInput(t *testing.T) {
input: inputEncodedAllListOfURLsWithCRLFEol,
result: outputDecodedListOfURLsWithCRLFEol,
},
"Encoded list of Markdown formatted URLs with LF EOL": {
input: inputEncodedAllListOfMarkdownFormattedURLsWithLFEol,
result: outputDecodedListOfMarkdownFormattedURLsWithLFEol,
},
"Encoded list of Markdown formatted URLs with CRLF EOL": {
input: inputEncodedAllListOfMarkdownFormattedURLsWithCRLFEol,
result: outputDecodedListOfMarkdownFormattedURLsWithCRLFEol,
},
"Encoded single URL with angle brackets with LF EOL": {
input: inputEncodedSingleSafelinksURLWithAngleBracketsWithLFEol,
result: outputDecodedSingleURLWithAngleBracketsWithLFEol,
Expand Down
6 changes: 5 additions & 1 deletion internal/safelinks/safelinks.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ func trimEnclosingURLCharacters(url string) string {
// Remove potential enclosing angle brackets.
url = strings.Trim(url, `<>`)

// Remove potential enclosing parenthesis used with Markdown formatted
// URLs.
url = strings.Trim(url, `()`)

return url
}

Expand Down Expand Up @@ -306,7 +310,7 @@ func GetURLPatternsUsingRegex(input string, evalPlainHTTP bool) ([]FoundURLPatte
// whitespace character or a right angle bracket. The caller is responsible
// for trimming angle brackets and other unwanted characters.
func GetURLPatternsUsingIndex(input string, evalAllHTTPURLs bool) ([]FoundURLPattern, error) {
if !strings.Contains(input, SafeLinksURLRequiredPrefix) && !evalAllHTTPURLs {
if !hasAcceptableURLPrefix(input, evalAllHTTPURLs) {
return nil, ErrNoURLsFound
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[Retirement of Office 365 connectors within Microsoft Teams](https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fdevblogs.microsoft.com%2Fmicrosoft365dev%2Fretirement-of-office-365-connectors-within-microsoft-teams%2F&data=data_placeholder&sdata=sdata_placeholder&reserved=0)
[Retirement of Office 365 connectors within Microsoft Teams](https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Faka.ms%2FO365ConnectorDeprecation&data=data_placeholder&sdata=sdata_placeholder&reserved=0)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[Retirement of Office 365 connectors within Microsoft Teams](https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fdevblogs.microsoft.com%2Fmicrosoft365dev%2Fretirement-of-office-365-connectors-within-microsoft-teams%2F&data=data_placeholder&sdata=sdata_placeholder&reserved=0)
[Retirement of Office 365 connectors within Microsoft Teams](https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Faka.ms%2FO365ConnectorDeprecation&data=data_placeholder&sdata=sdata_placeholder&reserved=0)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[Retirement of Office 365 connectors within Microsoft Teams](https://devblogs.microsoft.com/microsoft365dev/retirement-of-office-365-connectors-within-microsoft-teams/)
[Retirement of Office 365 connectors within Microsoft Teams](https://aka.ms/O365ConnectorDeprecation)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[Retirement of Office 365 connectors within Microsoft Teams](https://devblogs.microsoft.com/microsoft365dev/retirement-of-office-365-connectors-within-microsoft-teams/)
[Retirement of Office 365 connectors within Microsoft Teams](https://aka.ms/O365ConnectorDeprecation)

0 comments on commit 5431bf2

Please sign in to comment.