adamc@643
|
1 (* Copyright (c) 2009, Adam Chlipala
|
adamc@643
|
2 * All rights reserved.
|
adamc@643
|
3 *
|
adamc@643
|
4 * Redistribution and use in source and binary forms, with or without
|
adamc@643
|
5 * modification, are permitted provided that the following conditions are met:
|
adamc@643
|
6 *
|
adamc@643
|
7 * - Redistributions of source code must retain the above copyright notice,
|
adamc@643
|
8 * this list of conditions and the following disclaimer.
|
adamc@643
|
9 * - Redistributions in binary form must reproduce the above copyright notice,
|
adamc@643
|
10 * this list of conditions and the following disclaimer in the documentation
|
adamc@643
|
11 * and/or other materials provided with the distribution.
|
adamc@643
|
12 * - The names of contributors may not be used to endorse or promote products
|
adamc@643
|
13 * derived from this software without specific prior written permission.
|
adamc@643
|
14 *
|
adamc@643
|
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
adamc@643
|
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
adamc@643
|
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
adamc@643
|
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
adamc@643
|
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
adamc@643
|
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
adamc@643
|
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
adamc@643
|
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
adamc@643
|
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
adamc@643
|
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
adamc@643
|
25 * POSSIBILITY OF SUCH DAMAGE.
|
adamc@643
|
26 *)
|
adamc@643
|
27
|
adamc@643
|
28 structure ScriptCheck :> SCRIPT_CHECK = struct
|
adamc@643
|
29
|
adamc@643
|
30 open Cjr
|
adamc@643
|
31
|
adamc@643
|
32 structure SS = BinarySetFn(struct
|
adamc@643
|
33 type ord_key = string
|
adamc@643
|
34 val compare = String.compare
|
adamc@643
|
35 end)
|
adamc@643
|
36 structure IS = IntBinarySet
|
adamc@643
|
37
|
adamc@643
|
38 val csBasis = SS.addList (SS.empty,
|
adamc@643
|
39 ["new_client_source",
|
adamc@643
|
40 "get_client_source",
|
adamc@668
|
41 "set_client_source",
|
adamc@668
|
42 "new_channel",
|
adamc@668
|
43 "subscribe",
|
adamc@670
|
44 "send",
|
adamc@668
|
45 "recv"])
|
adamc@645
|
46
|
adamc@645
|
47 val scriptWords = ["<script",
|
adamc@670
|
48 " onclick=",
|
adamc@670
|
49 " onload="]
|
adamc@643
|
50
|
adamc@643
|
51 fun classify (ds, ps) =
|
adamc@643
|
52 let
|
adamc@643
|
53 fun inString {needle, haystack} =
|
adamc@643
|
54 let
|
adamc@643
|
55 val (_, suffix) = Substring.position needle (Substring.full haystack)
|
adamc@643
|
56 in
|
adamc@643
|
57 not (Substring.isEmpty suffix)
|
adamc@643
|
58 end
|
adamc@643
|
59
|
adamc@643
|
60 fun hasClient csids =
|
adamc@643
|
61 let
|
adamc@643
|
62 fun hasClient e =
|
adamc@643
|
63 case #1 e of
|
adamc@645
|
64 EPrim (Prim.String s) => List.exists (fn n => inString {needle = n, haystack = s}) scriptWords
|
adamc@643
|
65 | EPrim _ => false
|
adamc@643
|
66 | ERel _ => false
|
adamc@643
|
67 | ENamed n => IS.member (csids, n)
|
adamc@643
|
68 | ECon (_, _, NONE) => false
|
adamc@643
|
69 | ECon (_, _, SOME e) => hasClient e
|
adamc@643
|
70 | ENone _ => false
|
adamc@643
|
71 | ESome (_, e) => hasClient e
|
adamc@643
|
72 | EFfi ("Basis", x) => SS.member (csBasis, x)
|
adamc@643
|
73 | EFfi _ => false
|
adamc@643
|
74 | EFfiApp ("Basis", x, es) => SS.member (csBasis, x)
|
adamc@643
|
75 orelse List.exists hasClient es
|
adamc@643
|
76 | EFfiApp (_, _, es) => List.exists hasClient es
|
adamc@643
|
77 | EApp (e, es) => hasClient e orelse List.exists hasClient es
|
adamc@643
|
78 | EUnop (_, e) => hasClient e
|
adamc@643
|
79 | EBinop (_, e1, e2) => hasClient e1 orelse hasClient e2
|
adamc@643
|
80 | ERecord (_, xes) => List.exists (hasClient o #2) xes
|
adamc@643
|
81 | EField (e, _) => hasClient e
|
adamc@643
|
82 | ECase (e, pes, _) => hasClient e orelse List.exists (hasClient o #2) pes
|
adamc@643
|
83 | EError (e, _) => hasClient e
|
adamc@643
|
84 | EWrite e => hasClient e
|
adamc@643
|
85 | ESeq (e1, e2) => hasClient e1 orelse hasClient e2
|
adamc@643
|
86 | ELet (_, _, e1, e2) => hasClient e1 orelse hasClient e2
|
adamc@643
|
87 | EQuery {query, body, initial, ...} => hasClient query orelse hasClient body
|
adamc@643
|
88 orelse hasClient initial
|
adamc@643
|
89 | EDml {dml, ...} => hasClient dml
|
adamc@643
|
90 | ENextval {seq, ...} => hasClient seq
|
adamc@643
|
91 | EUnurlify (e, _) => hasClient e
|
adamc@643
|
92 in
|
adamc@643
|
93 hasClient
|
adamc@643
|
94 end
|
adamc@643
|
95
|
adamc@643
|
96 fun decl ((d, _), csids) =
|
adamc@643
|
97 let
|
adamc@643
|
98 val hasClient = hasClient csids
|
adamc@643
|
99 in
|
adamc@643
|
100 case d of
|
adamc@643
|
101 DVal (_, n, _, e) => if hasClient e then
|
adamc@643
|
102 IS.add (csids, n)
|
adamc@643
|
103 else
|
adamc@643
|
104 csids
|
adamc@643
|
105 | DFun (_, n, _, _, e) => if hasClient e then
|
adamc@643
|
106 IS.add (csids, n)
|
adamc@643
|
107 else
|
adamc@643
|
108 csids
|
adamc@643
|
109 | DFunRec xes => if List.exists (fn (_, _, _, _, e) => hasClient e) xes then
|
adamc@643
|
110 foldl (fn ((_, n, _, _, _), csids) => IS.add (csids, n))
|
adamc@643
|
111 csids xes
|
adamc@643
|
112 else
|
adamc@643
|
113 csids
|
adamc@643
|
114 | _ => csids
|
adamc@643
|
115 end
|
adamc@643
|
116
|
adamc@643
|
117 val csids = foldl decl IS.empty ds
|
adamc@643
|
118
|
adamc@643
|
119 val ps = map (fn (ek, x, n, ts, t, _) =>
|
adamc@643
|
120 (ek, x, n, ts, t,
|
adamc@643
|
121 if IS.member (csids, n) then
|
adamc@643
|
122 ServerAndClient
|
adamc@643
|
123 else
|
adamc@643
|
124 ServerOnly)) ps
|
adamc@643
|
125 in
|
adamc@643
|
126 (ds, ps)
|
adamc@643
|
127 end
|
adamc@643
|
128
|
adamc@643
|
129 end
|
adamc@643
|
130
|