-
Notifications
You must be signed in to change notification settings - Fork 172
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
SNOW-760534 Added URLValidator and URLEncoder #1297
Merged
sfc-gh-hchaturvedi
merged 5 commits into
master
from
hchaturvedi_snow_760534_validate_sso_url
Mar 16, 2023
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3e5bb58
Addressed potential vulnerability + added validator and url encoder
sfc-gh-hchaturvedi f93cb65
Updated the pattern to OWASP based valid url string
sfc-gh-hchaturvedi fdb4392
Format update
sfc-gh-hchaturvedi 96e10ed
Added test for File protocol
sfc-gh-hchaturvedi eeaad90
Merge branch 'master' into hchaturvedi_snow_760534_validate_sso_url
sfc-gh-hchaturvedi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright (c) 2012-2023 Snowflake Computing Inc. All rights reserved. | ||
*/ | ||
package net.snowflake.client.core; | ||
|
||
import net.snowflake.client.jdbc.ErrorCode; | ||
import net.snowflake.client.log.SFLogger; | ||
import net.snowflake.client.log.SFLoggerFactory; | ||
|
||
import javax.annotation.Nullable; | ||
import java.io.UnsupportedEncodingException; | ||
import java.net.MalformedURLException; | ||
import java.net.URISyntaxException; | ||
import java.net.URL; | ||
import java.net.URLEncoder; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
public class URLUtil { | ||
|
||
static final SFLogger logger = SFLoggerFactory.getLogger(URLUtil.class); | ||
public static boolean isValidURL(String url) { | ||
try { | ||
new URL(url).toURI(); | ||
return true; | ||
} catch(MalformedURLException mex) { | ||
logger.debug("Invalid URL found - ", url); | ||
return false; | ||
} catch (URISyntaxException uex) { | ||
logger.debug("Invalid URL found - ", url); | ||
return false; | ||
} | ||
} | ||
|
||
@Nullable | ||
public static String urlEncode(String url) throws UnsupportedEncodingException { | ||
String encodedURL; | ||
try { | ||
encodedURL = URLEncoder.encode(url, | ||
StandardCharsets.UTF_8.toString()); | ||
} catch(UnsupportedEncodingException uex) { | ||
return null; | ||
} | ||
return encodedURL; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright (c) 2012-2023 Snowflake Computing Inc. All rights reserved. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add test for file: URL. It should fail. |
||
*/ | ||
package net.snowflake.client.core; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class URLUtilTest { | ||
|
||
@Test | ||
public void testValidURL() throws Exception { | ||
assertTrue(URLUtil.isValidURL("https://ssoTestURL.okta.com")); | ||
assertTrue(URLUtil.isValidURL("https://ssoTestURL.okta.com:8080")); | ||
assertTrue(URLUtil.isValidURL("https://ssoTestURL.okta.com/testpathvalue")); | ||
} | ||
|
||
@Test | ||
public void testInvalidURL() throws Exception { | ||
assertFalse(URLUtil.isValidURL("-a Calculator")); | ||
assertFalse(URLUtil.isValidURL("This is random text")); | ||
} | ||
|
||
@Test | ||
public void testEncodeURL() throws Exception { | ||
assertEquals(URLUtil.urlEncode("Hello @World"), "Hello+%40World"); | ||
assertEquals(URLUtil.urlEncode("Test//String"), "Test%2F%2FString"); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please check .NET PR. URL still valid if it start with "file://" and it can do command injectsion. We need reg expresion for http and https prefix same as .NET.