JsonSQL.pm untested

filter: Statementstmt 0.0% 0 / 11 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 3 Podpod n/a totaltotal 0.0% 0 / 14 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 # ABSTRACT: JsonSQL distribution. A collection of modules for generating safe SQL from JSON strings.
2  
3  
4  
5  
6  
7  
8 use strict;
9 use warnings;
10 use 5.014;
11  
12 package JsonSQL;
13  
14 our $VERSION = '0.4'; # VERSION
15  
16  
17 1;
18  
19 __END__
20  
21 =pod
22  
23 =encoding UTF-8
24  
25 =head1 NAME
26  
27 JsonSQL - JsonSQL distribution. A collection of modules for generating safe SQL from JSON strings.
28  
29 =head1 VERSION
30  
31 version 0.4
32  
33 =head1 SYNOPSIS
34  
35 This is a set of modules used to turn a JSON string representing an SQL query into an appropriate SQL statement.
36  
37 For example,
38  
39 use JsonSQL::Query::Select;
40
41 my $jsonString = '{
42 "fields": [
43 {"column": "*"}
44 ],
45 "from": [
46 {"table": "my_table"}
47 ]
48 }';
49
50 my $whitelisting_rules = [
51 { schema => '#anySchema', 'my_table' => [ '#anyColumn' ] }
52 ];
53
54 my $selectObj = JsonSQL::Query::Select->new($whitelisting_rules, $jsonString);
55 my ( $sql, $binds ) = $selectObj->get_select;
56  
57 Generates:
58  
59 $sql = 'SELECT * FROM 'my_table';
60 $binds = <arrayref of parameterized values, if applicable>
61  
62 Now you can go ahead and use $sql and $binds directly with the L<DBI> module to do the query.
63  
64 =head1 DESCRIPTION
65  
66 The purpose of this distribution is to provide a reasonably safe mechanism for SQL query generation using data from untrusted sources, namely
67 web browsers. JSON is a convenient format native to JavaScript (ECMAScript), which can be translated to and from Perl objects fairly easily.
68 JSON was selected to provide a structured format for representing SQL statements such that it can be validated, checked for appropriate
69 access restrictions, and used to generate a well-formed and parameterized SQL statement that can be passed off to the L<DBI> module.
70  
71 The format is somewhat verbose in a few places, with the idea that the user/developer needs to be very explicit when passing parameters
72 to the query. This makes it well-suited for handling untrusted data (for example, from HTML forms), but if you just need a basic SQL
73 generator, you would probably be better off looking at L<SQL::Abstract> or L<SQL::Maker> instead.
74  
75 A simple SELECT statement,
76  
77 {
78 "fields": [
79 {"column": "*"}
80 ],
81 "from": [
82 {"table": "my_table"}
83 ]
84 }
85  
86 A more complicated SELECT statement,
87  
88 {
89 "fields": [
90 {"column": "field1"},
91 {"column": "field2", "alias": "test"}
92 ],
93 "from": [
94 {"table": "table1", "schema": "MySchema"}
95 ],
96 "where": {
97 "and": [
98 { "eq": {"field": {"column": "field2"}, "value": "Test.Field2"} },
99 { "eq": {"field": {"column": "field1"}, "value": "453.6"} },
100 { "or": [
101 { "eq": {"field": {"column": "field2"}, "value": "field3"} },
102 { "gt": {"field": {"column": "field3"}, "value": "45"} }
103 ]}
104 ]
105 }
106 }
107  
108 A SELECT statement with JOINs,
109  
110 {
111 "fields": [
112 {"column": "field1"},
113 {"column": "field2", "alias": "test"}
114 ],
115 "joins": [
116 {"jointype": "inner", "from": {"table": "table1", "schema": "MySchema"}, "to": {"table": "table2", "schema": "MySchema"}, "on": {"eq": {"field": {"column": "field2"}, "value": {"column": "field1"}} }}
117 ],
118 "where": {
119 "and": [
120 { "eq": {"field": {"column": "field2"}, "value": "Test.Field2"} },
121 { "eq": {"field": {"column": "field1"}, "value": "453.6"} },
122 { "or": [
123 { "eq": {"field": {"column": "field2"}, "value": "field3"} },
124 { "gt": {"field": {"column": "field3"}, "value": "45"} }
125 ]}
126 ]
127 }
128 }
129  
130 A couple of INSERT statements,
131  
132 {
133 "inserts": [
134 {
135 "table": {"table": "table1", "schema": "MySchema"},
136 "values": [
137 {"column": "column1", "value": "value1"},
138 {"column": "column2", "value": "value2"}
139 ],
140 "returning": [{"column": "column1", "as": "bestcolumn"}, {"column": "column2"}]
141 },
142 {
143 "table": {"table": "table2"},
144 "values": [
145 {"column": "columnA", "value": "valueA"},
146 {"column": "columnB", "value": "valueB"}
147 ]
148 }
149 ]}
150  
151 For more detailed information, see the main query modules,
152  
153 =over
154  
155 =item L<JsonSQL::Query::Select> for SELECT statements
156  
157 =item L<JsonSQL::Query::Insert> for INSERT statements
158  
159 =back
160  
161 An important feature of this distribution is whitelisting of allowed table and column identifiers. The whitelisting rules are defined
162 in the format,
163  
164 [
165 {
166 schema => 'schemaName' || '#anySchema',
167 <'#anyTable' || allowedTableName1 => [ '#anyColumn' || allowedFieldName1, allowedFieldName2, ... ]>,
168 <... additional table rules ...>
169 },
170 < ... additional rule sets ... >
171 ]
172  
173 and are saved in the query object when it is created. Subsequent building of the SQL statement examines this whitelist and returns an
174 error if table/column identifiers are used that have not been explicitly allowed. This allows JSON query generation and processing to
175 be safely separated and handled by different modules. The generating module (ex: a JavaScript client) is responsible for generating
176 the query in stringified JSON format, and the processing module (ex: CGI script) is responsible for validating and processing that JSON
177 query into an SQL statement.
178  
179 ** Important Takeaway: JsonSQL query object construction and SQL generation will fail if you have not defined any whitelisting rules. **
180  
181 It is not recommended, but you can disable the whitelisting module by defining a permissive rule,
182  
183 [ { schema => '#anySchema', '#anyTable' } ]
184  
185 For more information on the whitelisting module, and how to construct rule sets, see the L<JsonSQL::Validator> module.
186  
187 =head1 METHODS
188  
189 =head2 Methods
190  
191 This module is a documentation stub and does not contain any code. For detailed API information, see the appropriate modules.
192  
193 For users, the main JsonSQL Query modules,
194  
195 =over
196  
197 =item * L<JsonSQL::Query::Select>
198  
199 =item * L<JsonSQL::Query::Insert>
200  
201 =back
202  
203 For a description of the whitelisting feature,
204  
205 =over
206  
207 =item * L<JsonSQL::Validator>
208  
209 =back
210  
211 For developers,
212  
213 =over
214  
215 =item * The module used for returning errors:
216  
217 L<JsonSQL::Error>
218  
219 =item * To create a new schema,
220  
221 L<JsonSQL::Schemas::Schema>
222  
223 Examples: L<JsonSQL::Schemas::select> and L<JsonSQL::Schemas::insert>
224  
225 =item * To create a new query object,
226  
227 L<JsonSQL::Query::Query>
228  
229 If your aim is to add additional features to an existing query object, you may also need to extend the schema.
230 If you are supporting a completely new query type, you will need to write an appropriate schema for it.
231  
232 =item * To create additional query parameters,
233  
234 The individual JsonSQL::Param modules have a lot of documentation.
235 Start with L<JsonSQL::Param::Tables> or L<JsonSQL::Param::Fields> to see how they work and how they integrate whitelist validation.
236 For WHERE-like parameters, see L<JsonSQL::Param::Condition> and L<JsonSQL::Param::ConditionDispatcher>.
237  
238 =back
239  
240 =head1 Changes
241  
242 =over
243  
244 =item I<0.1 - 0.3>
245  
246 Internal Development
247  
248 =item I<0.4>
249  
250 First public release
251  
252 =back
253  
254 =head1 TODO
255  
256 A short list, in more-or-less relative priority, of things I would like to change/fix as time allows.
257  
258 =over
259  
260 =item * Deprecate SQL::Maker dependency.
261  
262 This is only useful for SELECT queries, and there are a fair amount of bugs that need to be worked around. To strengthen feature support,
263 a full SQL-generating backend needs to be written.
264  
265 =item * Support Common Table Expressions (CTEs) and subqueries.
266  
267 =item * Support additional query types: UPDATE, DELETE ( and maybe CREATE, DROP ).
268  
269 =item * Support database-specific drivers to better deal with database-specific nuances.
270  
271 =back
272  
273 =head1 AUTHOR
274  
275 Chris Hoefler <bhoefler@draper.com>
276  
277 =head1 COPYRIGHT AND LICENSE
278  
279 This software is copyright (c) 2017 by Chris Hoefler.
280  
281 This is free software; you can redistribute it and/or modify it under
282 the same terms as the Perl 5 programming language system itself.
283  
284 =cut