Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@W-8522881@ Invalidate input URL if it contains special characters #534

Merged
merged 8 commits into from
Jan 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import java.nio.charset.StandardCharsets
import java.util.regex.Pattern

import com.twitter.chill.Base64.{InputStream => Base64InputStream}
import org.apache.commons.httpclient.URI
import org.apache.commons.io.input.CharSequenceInputStream
import org.apache.commons.validator.routines.UrlValidator

Expand Down Expand Up @@ -181,11 +182,11 @@ class URL(value: Option[String]) extends Text(value){
/**
* Extracts url domain, i.e. 'salesforce.com', 'data.com' etc.
*/
def domain: Option[String] = value map (new java.net.URL(_).getHost)
def domain: Option[String] = value map (s => new java.net.URL(new URI(s, false).toString).getHost)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would rather make it a parameter with a default value, e.g.

/**
 * Extracts url domain, i.e. 'salesforce.com', 'data.com' etc.
 *
 * @param escaped true if URI character sequence is in escaped form. false otherwise. 
 */ 
def domain(escaped: Boolean = false): Option[String] = value map (s => new java.net.URL(new URI(s, escaped).toString).getHost)

/**
* Extracts url protocol, i.e. http, https, ftp etc.
*/
def protocol: Option[String] = value map (new java.net.URL(_).getProtocol)
def protocol: Option[String] = value map (s => new java.net.URL(new URI(s, false).toString).getProtocol)
winterslu marked this conversation as resolved.
Show resolved Hide resolved
}
object URL {
def apply(value: Option[String]): URL = new URL(value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ class URLTest extends PropSpec with PropertyChecks with TestCommon {
"http://nothingthere.com?Chr=%E5%85&Raj=%E7%B5%AE%A1&Hir=%8F%E0%B4%A3" -> "nothingthere.com",
"ftp://my.red.book.com/amorcito.mio" -> "my.red.book.com",
"http://secret.gov?Cla=%E9%99%B9%E4%8A%93&Cha=%E3&Eve=%EC%91%90%E8%87%B1" -> "secret.gov",
"ftp://nukes.mil?Lea=%E2%BC%84%EB%91%A3&Mur=%E2%83%BD%E1%92%83" -> "nukes.mil"
"ftp://nukes.mil?Lea=%E2%BC%84%EB%91%A3&Mur=%E2%83%BD%E1%92%83" -> "nukes.mil",
"http://specialchars.漢字.com/@" -> "specialchars.%E6%BC%A2%E5%AD%97.com"
)

URL(None).domain shouldBe None
Expand Down