UNPKG

urisanity

Version:

vet URIs in web and web-like applications with confidence

3 lines (2 loc) 8.03 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).urisanity=t()}(this,(function(){"use strict";var e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},t=/^([^\w]*)(unsafe|javascript|vbscript|app|admin|icloud-sharing|icloud-vetting|help|aim|facetime-audio|applefeedback|ibooks|macappstore|udoc|ts|st|jar|x-apple-helpbasic|(?:x\-)?radar)/im,a=/^(?:(?:f|ht)tps?|cid|xmpp|mms|webcal|aaa|acap|bolo|data|blob|file|local|wss?|irc|udp)/im,r=/\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/?)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\)){0,}(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s\!()\[\]{};:\'\"\.\,<>?«»“”‘’]){0,})/i,o=/^(whatsapp|zoommtg|slack|mailto|tel|callto|sms|skype)/im,i=/^(jdbc(:sqlserver|:mysql|:mariadb|:sqlite)?|odbc|postgres(ql)?|mongodb)/im,s=/^(view-source|moz-extension|resource|res|symres|(?:filesystem:|blob:)?chrome-extension|safari|chrome|mxaddon-pkg|mx|mxwebcore|mxjscall|mbinit|safari-resource|opera|webviewprogressproxy|chromenull|chromeinvoke|chromeinvokeimmediate)/im,n=/^((?:jar:)?file|local|blob)/im,c=/^(cloudinary|obsidian|gs|s3|grpc|pptr|tmtbff)/im,l=["widgets.amung.us","v.zilionfast.in","js.blinkadr.com","www.superfish.com","nzj.divdriver.net","istatic.datafastguru.info","widgets.amung.us","xls.searchfun.in","static.image2play.com"],d=/[\u0000-\u001F\u007F-\u009F\u2000-\u200D\uFEFF]/gim,u=/^data:(?:image\/(?:bmp|gif|jpeg|jpg|png|tiff|webp|svg\+xml)|video\/(?:mpeg|mp4|ogg|webm)|audio\/(?:mp3|oga|ogg|opus));base64,(?:[a-zA-Z0-9\/+\n=]+)$/i,f=/^data:(?:text\/(?:javascript|html|css|plain))(?:;charset=(UTF-8|iso-8859-7))?,(?:.*)$/im,m=/^(?:vb|java)(?:\s*)?script/im,p=/^(?:(blob:)?https?|wss?|about|mailto|tel)/im,h=[".","/"],v="undefined"==typeof self?global||{}:self,b=v.process||{versions:{node:"."},env:{}},g=parseInt(b.versions.node.split(".")[0]);g<=12&&(v.URL||(v.URL=function(e){var t=require("url"),a=g<=11?t.parse(e):t.URL(e);return Object.assign({},a,{hostname:a.host})}));var w,y=void 0!==(w=v.navigator)&&null===w.product.match(/^(ReactNative|NativeScript|NS)$/i)?v.location.origin:(v.constants||b.env).ORIGIN;function F(e){return"string"==typeof e&&h.indexOf(e.charAt(0))>-1}function L(e){var t=void 0,a=void 0,r=void 0,o=void 0,i=void 0;o={};var s=e instanceof v.FormData?e.entries():new v.FormData(e).entries();try{for(;null!==(a=s.next().value);)r=a[0],i=a[1],Object.prototype.hasOwnProperty.call(o,r)?(t=o[r],Array.isArray(t)||(t=o[r]=[t]),t.push(i)):o[r]=i}catch(e){o=Object.fromEntries(s)}return JSON.stringify(o)}var A=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"";return"string"==typeof e&&/&(?:[a-z]+|#x?\d+);/gim.test(e)};function C(e){var f=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!e||null!==e.match(/:\/\/(?:[#$@=*.!]|[/]){0,}$/)||e.includes("////////////")||A(e))return"about:blank";var h=e.replace(d,"").trim();if(F(h)){var b=h.startsWith("/")?y+h:y+"/"+h;h=r.test(b)?b:"//"}var g=h.match(a)||h.match(o)||h.match(i)||h.match(s)||h.match(c)||h.match(p),w=(g=null!==g?g:[])[0]||"";try{var L=new v.URL(h.toLowerCase()),C=L.hostname,x=L.pathname,j=L.search,k=L.hash,O=j.match(/\?/g)||[];if(O.length>1)return"about:blank";if(/^(?:((?:www|[a-z]{1,11})\.)(?!\1)(?:[a-z\-\d]{1,63})\.(?:[a-z.\-\d]{2,63}))$/i.test(C)||/^(((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$/.test(C)||/^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/.test(C)||/^(?:((?:[a-z0-9-._~]{0,}|%[1-9a-f]?[0-9a-f]|[!$&'()*+,;=])|\:|\@)*)/i.test(x)||/^((?:[a-z0-9-._~]{0,}|%[1-9a-f]?[0-9a-f]|[!$&'()*+,;=])|\:|\@|\/|\?)*$/gi.test(j)||/^((?:[a-z0-9-._~]{0,}|%[1-9a-f]?[0-9a-f]|[!$&'()*+,;=])|\:|\@|\/|\?)*$/gi.test(k)){if(C.includes(".00")||-1!==l.indexOf(C))return"about:blank";if(x.toLowerCase().includes("&apos;")||x.toLowerCase().includes("%29")||x.toLowerCase().includes("%28")||x.toLowerCase().includes("%22")||x.toLowerCase().includes("&quot;")||x.toLowerCase().includes("&#39")||x.toLowerCase().includes("(")||x.toLowerCase().includes(")")||x.toLowerCase().includes("%3e")||x.toLowerCase().includes("%3c")||x.toLowerCase().includes("><")||x.toLowerCase().includes("</")||null!==x.toLowerCase().match(/\.(?:jar|dmg|exe|bin|sh|sed|py)/g))return"about:blank";if(null!==j.toLowerCase().match(/%3c(?=\/)?/g)||j.toLowerCase().includes("%3e")||j.toLowerCase().includes("%3f")||j.toLowerCase().includes("%3d")||j.toLowerCase().includes("%27")||j.toLowerCase().includes("%22")||j.toLowerCase().includes("&#39")||j.toLowerCase().includes("&apos;")||j.toLowerCase().includes("</")||j.toLowerCase().includes("&quot;")||null!==j.toLowerCase().match(/\.(?:jar|dmg|exe|bin|sh|sed|py)/g))return"about:blank"}}catch(e){if(e)return"about:blank"}if(!t.test(w)||r.test(h)){var R=!1;return f.allowScriptOrDataURI&&null!==w.match(/^(java|vb)script|data/)&&(u.test(h)||m.test(h))&&(R=!0),f.allowCommsAppURI&&o.test(h)&&(R=!0),f.allowDBConnectionStringURI&&i.test(h)&&(R=!0),f.allowServiceAPIURI&&c.test(h)&&(R=!0),f.allowBrowserSpecificURI&&s.test(h)&&(R=!0),f.allowWebTransportURI&&p.test(h)&&(R=!0),f.allowFileSystemURI&&n.test(h)&&(R=!0),""!==w&&R?h:"about:blank"}}var x={isSameOrigin:function(e){if(!e||f.test(e.trim())||u.test(e.trim()))return!1;try{var t=new v.URL(e.trim());return y===t.origin}catch(e){if(e)return!1}},extractParamValueFromUri:function(e,t){if(!e||f.test(e.trim())||u.test(e.trim()))return null;var a=new RegExp("[\\?&#]"+t+"=([^&#]*)").exec(e.trim());return null!=a?unescape(a[1]):void 0},checkParamsOverWhiteList:function(t){var a=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[],r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"";if(!t||f.test(t.trim())||u.test(t.trim()))return!1;if("object"!==(void 0===a?"undefined":e(a)))return!1;var o=new v.URL(t.trim()),i=[],s=[],n=null;try{var c="";if("FormData"in v&&r instanceof v.FormData)if("function"==typeof Object.fromEntries)c=L(r);else{var l={};r.forEach((function(e,t){l[t]=e})),c=JSON.stringify(l)}else c=r;"string"==typeof c&&(n=JSON.parse(c))}catch(e){n=r}if(""===n?o.searchParams.forEach((function(){for(var e=arguments.length,t=Array(e),a=0;a<e;a++)t[a]=arguments[a];var r=t[0],o=t[1];s.push(unescape(r)),i.push(o)})):n instanceof Object&&s.concat(Object.values(n)),i.length===s.length)if(a instanceof Array){if(i.length===a.length&&i.slice(0).sort().join("|")===a.slice(0).sort().join("|"))return!0}else if(a instanceof Object){for(var d=0;d<i.length;d++){var m=i[d],p=s[d],h=a[m];if(!(h instanceof RegExp))throw new Error('"'+m+'" does not have a matching regex pattern to match "'+p+'"');if(!h.test(p))break}if(d===s.length)return!0}return!1},vet:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return C(e,t||{})}};return x})); //# sourceMappingURL=urisanity.min.js.map