# HG changeset patch # User František Kučera # Date 1549546242 -3600 # Node ID 087b8621fb3e34cb1edcd70a0908b39c44e92360 # Parent fde0cd94fde67853ebb07ec5873f40422ce313fc examples: Parametrized queries with Guile diff -r fde0cd94fde6 -r 087b8621fb3e relpipe-data/examples-guile-parametrized-queries.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/relpipe-data/examples-guile-parametrized-queries.xml Thu Feb 07 14:30:42 2019 +0100 @@ -0,0 +1,79 @@ + + + Parametrized queries with Guile + passing input parameters and avoiding code-injections + 01600 + + + +

+ are not only for ad-hoc commands but – they could (and probably often should) be used for creating reusable programs. + Such programs are once written, stored in a shell script or shell function or alias and then called many times. +

+ +

+ For example, we need a script which prints records from our fstab that have given filesystem type. + We could do it this way: +

+ + + +

It seems working – e.g. if we call fstab-where-type btrfs, we get:

+ + + +

+ But it is fundamentally wrong. The input parameter is blindly pasted in middle of the Guile code. + So if we call e.g. fstab-where-type 'ext4"', it crashes terribly. + Do you remember SQL injections in your first PHP scripts when you were 14? + Do you remember XKCD: Exploits of a Mom? + Don't do it again! +

+ +

+ The relpipe-tr-guile tool has a safe way for passing parameters from the outside. And such parameters are even strongly typed. + So this is, how our program should be written: +

+ + + +

+ So when we call fstab-where-type 'ext4"' again, there is no crash, no code-injection. + Just empty relation is returned because there is no record WHERE type = 'ext4"' (said in SQL words). +

+ +

+ Now it is like we do a parametrized query in SQL: +

+ + + +

+ And bind the myRequestedType parameter. +

+ + +
+ +