Lua whitelist to sanitize and allow clean URL string only - lua

So I am trying to come up with a method that will allow clean URL's only and make the string empty if it contains any character(s) it should not.
Whitelist of Characters i want the url to accept only
A-Z a-z 0-9 _ - . /
Lua Code Example :
local bar = "com/url/index.php/html/path/stuff.html.html123/..lol"
bar = bar:gsub("%.html.*$","")
bar = bar:gsub("%/$","")
bar = bar:gsub("%.$","")
print(bar)
--TODO: if characters not in whitelist then make bar empty
bar = ""
Clean output:
com/url/index.php/html/path/stuff
Dirty output: (This is what I want to get rid of because characters not whitelisted or multiple unnecessary slashes)
com/url///////index.php/html//////path///////stuff
com/url///////+index.php/=html//////path///////stuff#[

I'm not sure what you mean. Try this.
if bar:gsub("[A-Za-z0-9_%-%./]","")=="" then
-- bar is ok
else
bar=""
end

Related

Check if String Contains an Emoji in Ruby

In ruby, here is how you can check for a substring in a string:
str = "hello world"
str.include?("lo")
=> true
When I am attempting to save an emoji in a text column in a rails application (the text column within a mysql database is utf8), it comes back with this error:
Incorrect string value: \xF0\x9F\x99\x82
For my situation in a rails application, it suffices to see if an emoji is present in the submitted text. If an emoji is present: raise a validation error. Example:
class MyModel < ApplicationRecord
validate :cannot_contain_emojis
private
def cannot_contain_emojis
if my_column.include?("/\xF0")
errors.add(:my_column, 'Cannot include emojis")
end
end
end
Note: The reason I am checking for \xF0 is because according to this site, it appears that all, or most, emoji's begin with this signature.
This however does not work. It continues to return false even when it is true. I'm pretty sure the issue is that my include statement doesn't work because the emoji is not converted to bytes for the comparison.
Question
How can I make a validation to check that an emoji is not passed in?
Example bytes for a smiley face in UTF8: \xF0\x9F\x99\x82
You can use the Emoji Unicode property to test for Emoji using a Regexp, something like this:
def cannot_contain_emojis
if /\p{Emoji}/ =~ my_column
errors.add(:my_column, 'Cannot include emojis')
end
end
Unicode® Technical Standard #51 "UNICODE EMOJI" contains a more sophisticated regex:
\p{RI} \p{RI}
| \p{Emoji}
( \p{EMod}
| \x{FE0F} \x{20E3}?
| [\x{E0020}-\x{E007E}]+ \x{E007F} )?
(\x{200D} \p{Emoji}
( \p{EMod}
| \x{FE0F} \x{20E3}?
| [\x{E0020}-\x{E007E}]+ \x{E007F} )?
)*
[Note: some of those properties are not implemented in Onigmo / Ruby.]
However, checking for Emojis probably not going to be enough. It is pretty clear that your text processing is somehow broken at some point. And if it is broken by an Emoji, then there is a chance it will also be broken by my name, or the name of Ruby's creator 松本 行弘, or by the completely normal English word “naïve”.
Instead of playing a game of whack-a-mole trying to detect every Emoji, mathematical symbol, Arabic letter, typographically correct punctuation mark, etc., it would be much better simply the fix the text processing.
I found Jörg's solution was only working when passing in the string itself and not a variable. Not sure why that is.
/\p{Emoji}/ =~ "🎃"
=> 0
value = "1f383"
=> "1f383"
/\p{Emoji}/ =~ value
=> 0
/\p{Emoji}/ =~ "hello"
=> nil
Regardless I'd recommend using the unicode-emoji gem, as its approach is comprehensive. Its source code and documentation can be found on GitHub.

Search and Replace tab key in Openoffice

I haved wrote a next code to find a replace strings, I need to use this code to find "=" and replace with tab key, I trying with "\t" but this doesn't work, Could you help me please? What is a character tab key for use in macro of openoffice?
Public Function findReplace(oDoc As Object, findStr As String, replaceStr As String) As Integer
oSearch = oDoc.createSearchDescriptor
oSearch.searchAll = False
oSearch.SearchString = findStr
oSearch.ReplaceString = replaceStr
oDoc.replaceAll(oSearch)
End Function
alfetta is right - just enable RegularExpression
oSearch.SearchRegularExpression = TRUE
to have \t as replaceStr working, replacing with a Tab.
You should be able to find a tab with Search and Replace using \t with "Regular Expressions" checked
after starting Find & Replace StrgF

Remove space from query params in url dynamically

I want to remove the space in query params in the request url in ruby
Here is my sample request url:-
URL = 'www.test.com/a?q1=john&q2=US&q3= 92832832&q4=test&q5= foo'
I want to my output as below:-
URL = 'www.test.com/a?q1=john&q2=US&q3=92832832&q4=test&q5=foo'
I suggest trimming the white space. This can be achieved as stated by Joel:
If you want to remove only leading and trailing whitespace (like PHP's trim) you can use .strip, but if you want to remove all whitespace, you can use .gsub(/\s+/, "") instead.
(Ruby function to remove all white spaces?)
To remove whitespace you can use the following on the string
URL = 'www.test.com/a?q1=john&q2=US&q3= 92832832&q4=test&q5= foo'.gsub(/\s+/, "")
url = 'www.test.com/a?q1=john&q2=US&q3= 92832832&q4=test&q5= foo'.gsub!(/\s+/, "")

Cleaning a URL with PHP

I've been looking though google and stackflow for an answer for this and testing a few finds but I still can't get this working.
All of these end my link at a space. For example www.website.com/movies/movie
Where I'm trying to get it to read www.website.com/movies/movie with spaces here.mp4
$namehref = "movie/" . $dirArray[$index]. " download";
$DoStream = "Watch";
$DoDownload = "Download";
However this code does not remove the spaces???
$name = $dirArray[$index];
$movienameonly = substr($name, 0, -4);
example www.website.com/movies/movie with spaces here
So my questions are - Why does the first section of code remove the spaces and how do I correct it. In addition to spaces I also hit errors with 's as well.
example They're here.mp4
To remove the spaces completely:
preg_replace("/\s/", "", $your_url);
To replace the spaces with %20 (best way):
preg_replace("/\s/", "%20", $your_url);
To replace spaces with + like url_encode($url) does:
preg_replace("/\s/", "+", $your_url);
To replace ' with %27:
preg_replace("/\s/", "%20", $your_url);
You get errors because spaces can't be inputted in the browser and converts the spaces to %20 and the apostrophe to %27
I found it:
$DoStream = "Watch";
Should have been
$DoStream = "<a href='$the_dir'>Watch</a>";

Generated URL by Html.RouteLink with special characters

I have the following line
#Html.RouteLink(type.Description, "ListingsWithTypeSpecified", new { country = Model.CountryCode, state = Model.State, city = Model.CurrentCity.Name.ToLower(), description = type.Description.ToLower(), id = type.TypeID })
which produces
http://some.com:9609/ca/on/london/physiotherapy%20%20%26%20acupuncture/2
and
http://some.com:9609/ca/on/london/health%20department/19
first one has spaces and a &, second one just has a space
for the first one, I would like to still show
http://localhost:9609/ca/on/london/physiotherapy and acupuncture/2
for this one I understand replacing the & with "and" will work, however I still do not want %20 as spaces instead I would like to have a clean url.
Which method should I be using to properly have friendly url shown by what Html.RouteLink creates?
Replace the space with a -
e.g.
type.Description.ToLower().Replace(" ", "-")

Resources