# HG changeset patch # User Adam Chlipala # Date 1290790624 18000 # Node ID 0d8bd8ae841728140ac17add10a43b2da78c9172 # Parent 80bff6449f4162fb16a7c1f9035f6a0d3ad8ec0f Fix JavaScript unit unurlification; URL blessing client-side diff -r 80bff6449f41 -r 0d8bd8ae8417 lib/js/urweb.js --- a/lib/js/urweb.js Sun Nov 21 15:43:24 2010 -0500 +++ b/lib/js/urweb.js Fri Nov 26 11:57:04 2010 -0500 @@ -1182,5 +1182,26 @@ } +// URL blessing + +var urlRules = null; + +function checkUrl(s) { + for (var r = urlRules; r; r = r.next) { + var ru = r.data; + if (ru.prefix ? s.indexOf(ru.pattern) == 0 : s == ru.pattern) + return ru.allow ? s : null; + } + + return null; +} + +function bless(s) { + u = checkUrl(s); + if (u == null) + er("Disallowed URL: " + s); + return u; +} + // App-specific code diff -r 80bff6449f41 -r 0d8bd8ae8417 src/jscomp.sml --- a/src/jscomp.sml Sun Nov 21 15:43:24 2010 -0500 +++ b/src/jscomp.sml Fri Nov 26 11:57:04 2010 -0500 @@ -278,8 +278,8 @@ fun unurlifyExp loc (t : typ, st) = case #1 t of - TRecord [] => ("null", st) - | TFfi ("Basis", "unit") => ("null", st) + TRecord [] => ("(i++,null)", st) + | TFfi ("Basis", "unit") => ("(i++,null)", st) | TRecord [(x, t)] => let val (e, st) = unurlifyExp loc (t, st) @@ -1285,9 +1285,22 @@ | SOME line => lines (line :: acc) val lines = lines [] + val urlRules = foldr (fn (r, s) => + "cons({allow:" + ^ (if #action r = Settings.Allow then "true" else "false") + ^ ",prefix:" + ^ (if #kind r = Settings.Prefix then "true" else "false") + ^ ",pattern:\"" + ^ #pattern r + ^ "\"}," + ^ s + ^ ")") "null" (Settings.getUrlRules ()) + + val urlRules = "urlRules = " ^ urlRules ^ ";\n\n" + val script = if !foundJavaScript then - lines ^ String.concat (rev (#script st)) + lines ^ urlRules ^ String.concat (rev (#script st)) else "" in diff -r 80bff6449f41 -r 0d8bd8ae8417 src/settings.sml --- a/src/settings.sml Sun Nov 21 15:43:24 2010 -0500 +++ b/src/settings.sml Fri Nov 26 11:57:04 2010 -0500 @@ -207,7 +207,10 @@ ("isspace", "isSpace"), ("isxdigit", "isXdigit"), ("tolower", "toLower"), - ("toupper", "toUpper")] + ("toupper", "toUpper"), + + ("checkUrl", "checkUrl"), + ("bless", "bless")] val jsFuncs = ref jsFuncsBase fun setJsFuncs ls = jsFuncs := foldl (fn ((k, v), m) => M.insert (m, k, v)) jsFuncsBase ls fun jsFunc x = M.find (!jsFuncs, x)