-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Search: Add support for Youtube URLs #4146
Open
SamantazFox
wants to merge
5
commits into
iv-org:master
Choose a base branch
from
SamantazFox:url-search
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+160
−5
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0249f03
Add a youtube URL sanitizer
SamantazFox 96cf640
Search: Add support for youtu.be and youtube.com URLs
SamantazFox 9d115d8
Search: Add URL search inhibition logic
SamantazFox 441ea89
Misc: Clean some code in UrlSanitizer
SamantazFox d3bff46
Search: Change smart search inhibitor to a backslash
SamantazFox File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
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,121 @@ | ||
require "uri" | ||
|
||
module UrlSanitizer | ||
extend self | ||
|
||
ALLOWED_QUERY_PARAMS = { | ||
channel: ["u", "user", "lb"], | ||
playlist: ["list"], | ||
search: ["q", "search_query", "sp"], | ||
watch: [ | ||
"v", # Video ID | ||
"list", "index", # Playlist-related | ||
"playlist", # Unnamed playlist (id,id,id,...) (embed-only?) | ||
"t", "time_continue", "start", "end", # Timestamp | ||
"lc", # Highlighted comment (watch page only) | ||
], | ||
} | ||
|
||
# Returns whether the given string is an ASCII word. This is the same as | ||
# running the following regex in US-ASCII locale: /^[\w-]+$/ | ||
private def ascii_word?(str : String) : Bool | ||
return false if str.bytesize != str.size | ||
|
||
str.each_byte do |byte| | ||
next if 'a'.ord <= byte <= 'z'.ord | ||
next if 'A'.ord <= byte <= 'Z'.ord | ||
next if '0'.ord <= byte <= '9'.ord | ||
next if byte == '-'.ord || byte == '_'.ord | ||
|
||
return false | ||
end | ||
|
||
return true | ||
end | ||
|
||
# Return which kind of parameters are allowed based on the | ||
# first path component (breadcrumb 0). | ||
private def determine_allowed(path_root : String) | ||
case path_root | ||
when "watch", "w", "v", "embed", "e", "shorts", "clip" | ||
return :watch | ||
when .starts_with?("@"), "c", "channel", "user", "profile", "attribution_link" | ||
return :channel | ||
when "playlist", "mix" | ||
return :playlist | ||
when "results", "search" | ||
return :search | ||
else # hashtag, post, trending, brand URLs, etc.. | ||
return nil | ||
end | ||
end | ||
|
||
# Create a new URI::Param containing only the allowed parameters | ||
private def copy_params(unsafe_params : URI::Params, allowed_type) : URI::Params | ||
new_params = URI::Params.new | ||
|
||
ALLOWED_QUERY_PARAMS[allowed_type].each do |name| | ||
if unsafe_params[name]? | ||
# Only copy the last parameter, in case there is more than one | ||
new_params[name] = unsafe_params.fetch_all(name)[-1] | ||
end | ||
end | ||
|
||
return new_params | ||
end | ||
|
||
# Transform any user-supplied youtube URL into something we can trust | ||
# and use across the code. | ||
def process(str : String) : URI | ||
# Because URI follows RFC3986 specifications, URL without a scheme | ||
# will be parsed as a relative path. So we have to add a scheme ourselves. | ||
str = "https://#{str}" if !str.starts_with?(/https?:\/\//) | ||
|
||
unsafe_uri = URI.parse(str) | ||
unsafe_host = unsafe_uri.host | ||
unsafe_path = unsafe_uri.path | ||
|
||
new_uri = URI.new(path: "/") | ||
|
||
# Redirect to homepage for bogus URLs | ||
return new_uri if (unsafe_host.nil? || unsafe_path.nil?) | ||
|
||
breadcrumbs = unsafe_path | ||
.split('/', remove_empty: true) | ||
.compact_map do |bc| | ||
# Exclude attempts at path trasversal | ||
next if bc == "." || bc == ".." | ||
|
||
# Non-alnum characters are unlikely in a genuine URL | ||
next if !ascii_word?(bc) | ||
syeopite marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
bc | ||
end | ||
|
||
# If nothing remains, it's either a legit URL to the homepage | ||
# (who does that!?) or because we filtered some junk earlier. | ||
return new_uri if breadcrumbs.empty? | ||
|
||
# Replace the original query parameters with the sanitized ones | ||
case unsafe_host | ||
when .ends_with?("youtube.com") | ||
# Use our sanitized path (not forgetting the leading '/') | ||
new_uri.path = "/#{breadcrumbs.join('/')}" | ||
|
||
# Then determine which params are allowed, and copy them over | ||
if allowed = determine_allowed(breadcrumbs[0]) | ||
new_uri.query_params = copy_params(unsafe_uri.query_params, allowed) | ||
end | ||
when "youtu.be" | ||
# Always redirect to the watch page | ||
new_uri.path = "/watch" | ||
|
||
new_params = copy_params(unsafe_uri.query_params, :watch) | ||
new_params["id"] = breadcrumbs[0] | ||
|
||
new_uri.query_params = new_params | ||
end | ||
|
||
return new_uri | ||
end | ||
end |
Oops, something went wrong.
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.
Shouldn't this be under an
@inhibit_ssf
check as so we're just sending the query as-is when its off?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.
if the query starts with a space, the check below becomes useless, so we need to remove at least the leading spaces.
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.
That's a good point, with the way as is though is sending a raw query like
" youtube.com/watch?v=dQw4w9WgXcQ"
impossible then?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.
Yes, though Youtube pretty much ignores the extra space. Search result for "query" and " query" are identical on average (they change from request to request, even with the exact same query)
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.
I realized that you can search for
\ youtube.com/xxx
, tho. It will disable smart search, then remove the backslash, and pass the rest of the string.