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 3 commits
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 @@ -840,15 +840,15 @@ object RichTextFeatureLambdas {
}

class URLDomainToPickList extends Function1[URL, PickList] with Serializable {
def apply(v: URL): PickList = if (v.isValid) v.domain.toPickList else PickList.empty
def apply(v: URL): PickList = if (v.isValid) v.domain().toPickList else PickList.empty
}

class URLDomainToText extends Function1[URL, Text] with Serializable {
def apply(v: URL): Text = v.domain.toText
def apply(v: URL): Text = v.domain().toText
}

class URLProtocolToText extends Function1[URL, Text] with Serializable {
def apply(v: URL): Text = v.protocol.toText
def apply(v: URL): Text = v.protocol().toText
}

class URLIsValid extends Function1[URL, Boolean] with Serializable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class UrlMapToPickListMapTransformer(uid: String = UID[UrlMapToPickListMapTransf
extends UnaryTransformer[URLMap, PickListMap](operationName = "urlMapToPickListMap", uid = uid) {

override def transformFn: URLMap => PickListMap = _.value
.mapValues(v => if (v.toURL.isValid) v.toURL.domain else None)
.mapValues(v => if (v.toURL.isValid) v.toURL.domain() else None)
.collect { case (k, Some(v)) => k -> v }.toPickListMap

}
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 @@ -180,12 +181,16 @@ class URL(value: Option[String]) extends Text(value){
def isValid(protocols: Array[String]): Boolean = value.exists(new UrlValidator(protocols).isValid)
/**
* 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: Option[String] = value map (new java.net.URL(_).getHost)
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.
*
* @param escaped true if URI character sequence is in escaped form. false otherwise.
*/
def protocol: Option[String] = value map (new java.net.URL(_).getProtocol)
def protocol(escaped: Boolean = false): Option[String] = value map (s => new java.net.URL(new URI(s, escaped).toString).getProtocol)
}
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,15 +77,16 @@ 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.%EF%BC%A0.com"
)

URL(None).domain shouldBe None

forAll(samples) {
case (sample, expected) =>
val url = URL(sample)
val domain = url.domain
val domain = url.domain()
domain shouldBe Some(expected)
}
}
Expand All @@ -102,7 +103,7 @@ class URLTest extends PropSpec with PropertyChecks with TestCommon {
forAll(samples) {
case (sample, expected) =>
val url = URL(sample)
val domain = url.protocol
val domain = url.protocol()
domain shouldBe Some(expected)
}
}
Expand Down