Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d892db9

Browse files
committed
Release 2.4.
1 parent 12c20af commit d892db9

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

CHANGES

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
Changes with lua-resty-validation 2.4 16 Sep 2016
2+
3+
*) Feature: added support for custom (inline) validatiors
4+
*) Feature: added resty.validation.injection extension
5+
(uses libinjection)
6+
17
Changes with lua-resty-validation 2.3 22 Mar 2016
28

39
*) Feature: added resty.validation.utf8 extension (uses utf8rewind)

README.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ Validation factory consist of different validators and filters used to validate
198198
* `reverse`, reverses the value (string or number) (UTF-8)
199199
* `coalesce(...)`, if the value is nil, returns first non-nil value passed as arguments
200200
* `email()`, validates that the value is email address
201+
* `call(function)`, validates / filters the value against custom inline validator / filter
201202
* `optional([default])`, stops validation if the value is empty string `""` or `nil` and returns `true`, and either, `default` or `value`
202203

203204
#### Conditional Validation Factory Validators
@@ -299,6 +300,20 @@ function()
299300
end
300301
```
301302

303+
### Custom (Inline) Validators and Filters
304+
305+
Sometimes you may just have one-off validators / filters that you are not using elsewhere, or that you just
306+
want to supply quickly an additional validator / filter for a specific case. To make that easy and straight
307+
forward, we introduced `call` factory method with `lua-resty-validation` 2.4. Here is an example:
308+
309+
```lua
310+
validation:call(function(value)
311+
-- now validate / filter the value, and return the results
312+
-- here we just return false (aka making validation to fail)
313+
return false
314+
end)("Check this value"))
315+
```
316+
302317
### Built-in Validator Extensions
303318

304319
Currently `lua-resty-validation` has support for two extensions or plugins that you can enable:
@@ -307,7 +322,6 @@ Currently `lua-resty-validation` has support for two extensions or plugins that
307322
* `resty.validation.tz`
308323
* `resty.validation.utf8`
309324

310-
311325
These are something you can look at if you want to build your own validator extension. If you do
312326
so, and think that it would be usable for others as well, mind you to send your extension as a pull-request
313327
for inclusion in this project, thank you very much, ;-).
@@ -471,6 +485,31 @@ local validation = require "resty.validation"
471485
local valid, ts = validation:utf8category("LETTER_UPPERCASE")("TEST")
472486
```
473487

488+
#### resty.validation.injection extension
489+
490+
This set of validators and filters is based on the great [`libinjection`](https://github.com/client9/libinjection)
491+
library by Nick Galbreath - a SQL / SQLI / XSS tokenizer parser analyzer. It needs my LuaJIT FFI wrapper
492+
[`lua-resty-injection`](https://github.com/bungle/lua-resty-injection) to work. When the mentioned requirements
493+
are installed, the rest is easy. To use this extension, all you need to do is:
494+
495+
```lua
496+
require "resty.validation.injection"
497+
```
498+
499+
It will monkey patch the adapters that it will provide in `resty.validation`, and those are currently:
500+
501+
* `sqli`, returns `false` if SQL injection was detected, otherwise returns `true`
502+
* `xss`, returns `false` if Cross-Site Scripting injection was detected, otherwise returns `true`
503+
504+
##### Example
505+
506+
```lua
507+
require "resty.validation.injection"
508+
local validation = require "resty.validation"
509+
local valid, ts = validation.sqli("test'; DELETE FROM users;")
510+
local valid, ts = validation.xss("test <script>alert('XSS');</script>")
511+
```
512+
474513
## API
475514

476515
I'm not going here for details for all the different validators and filters there is because they all follow the

lib/resty/validation.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,11 @@ function factory.email()
415415
return match(value, "%w*%p*@+%w*%.?%w*") ~= nil
416416
end
417417
end
418+
function factory.call(func)
419+
return function(value)
420+
return func(value)
421+
end
422+
end
418423
function factory.optional(default)
419424
return function(value)
420425
if value == nil or value == "" then

lib/resty/validation/injection.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
local i = require "resty.injection"
2+
local validation = require "resty.validation"
3+
local validators = validation.validators
4+
function validators.sqli(value)
5+
return not i.sql(value)
6+
end
7+
function validators.xss(value)
8+
return not i.xss(value)
9+
end
10+
return {
11+
sqli = validators.sqli,
12+
xss = validators.xss
13+
}

0 commit comments

Comments
 (0)