Exemple #1
0
        /// <summary>
        /// Checks the url string for authentication violations.
        /// </summary>
        /// <param name="url">The url to check.</param>
        /// <param name="options">The options to consider. Ignored.</param>
        /// <returns>True if it meets the authorization standards, false otherwise.</returns>
        private static CheckOutput CheckAuth(string url, UrlOptions options)
        {
            var output    = new CheckOutput();
            var authIndex = url.IndexOf("@", StringComparison.InvariantCultureIgnoreCase);

            output.NewUrl = url;
            if (authIndex > -1)
            {
                var authValue  = url.Substring(0, authIndex);
                var colonIndex = authValue.IndexOf(":", StringComparison.InvariantCultureIgnoreCase);
                if (colonIndex > -1)
                {
                    var user = authValue.Substring(0, colonIndex);
                    var pass = authValue.Substring(colonIndex + 1);
                    output.IsValid = !string.IsNullOrEmpty(user) && !string.IsNullOrEmpty(pass);
                }
                else
                {
                    output.IsValid = true;
                }
            }
            else
            {
                output.IsValid = true;
            }

            return(output);
        }
Exemple #2
0
        public override CheckOutput ExecuteCheck(Excel.Worksheet ws, DefinedNames nomiDefiniti, CheckObj check)
        {
            _ws           = ws;
            _nomiDefiniti = nomiDefiniti;
            _check        = check;

            CheckOutput n = new CheckOutput();

            switch (check.Type)
            {
            case 1:
                n = CheckFunc1();
                break;

            case 2:
                n = CheckFunc2();
                break;

            case 3:
                n = CheckFunc3();
                break;

            case 4:
                n = CheckFunc4();
                break;

            case 5:
                n = CheckFunc5();
                break;
            }

            return(n);
        }
Exemple #3
0
        /// <summary>
        /// Checks the url string for path violations.
        /// </summary>
        /// <param name="url">The url to check.</param>
        /// <param name="options">The options to consider. Ignored.</param>
        /// <returns>True if it meets the path standards, false otherwise.</returns>
        private static CheckOutput CheckPath(string url, UrlOptions options)
        {
            var output    = new CheckOutput();
            var pathIndex = url.IndexOf("/", StringComparison.InvariantCultureIgnoreCase);

            if (pathIndex > -1)
            {
                var queryStringValue = url.Substring(pathIndex + 1);
                output.NewUrl = url.Substring(0, pathIndex);
                if (string.IsNullOrEmpty(queryStringValue))
                {
                    output.IsValid = true;
                }
                else
                {
                    output.IsValid = !queryStringValue.Contains(" ");
                }
            }
            else
            {
                output.NewUrl  = url;
                output.IsValid = true;
            }

            return(output);
        }
Exemple #4
0
        /// <summary>
        /// Checks the url string for authentication violations.
        /// </summary>
        /// <param name="url">The url to check.</param>
        /// <param name="options">The options to consider. Ignored.</param>
        /// <returns>True if it meets the authorization standards, false otherwise.</returns>
        private static CheckOutput CheckAuth(string url, UrlOptions options)
        {
            var output = new CheckOutput();
            var authIndex = url.IndexOf("@", StringComparison.InvariantCultureIgnoreCase);
            output.NewUrl = url;
            if (authIndex > -1)
            {
                var authValue = url.Substring(0, authIndex);
                var colonIndex = authValue.IndexOf(":", StringComparison.InvariantCultureIgnoreCase);
                if (colonIndex > -1)
                {
                    var user = authValue.Substring(0, colonIndex);
                    var pass = authValue.Substring(colonIndex + 1);
                    output.IsValid = !string.IsNullOrEmpty(user) && !string.IsNullOrEmpty(pass);
                }
                else
                {
                    output.IsValid = true;
                }
            }
            else
            {
                output.IsValid = true;
            }

            return output;
        }
Exemple #5
0
        /// <summary>
        /// Checks the url string for host violations.
        /// </summary>
        /// <param name="url">The url to check.</param>
        /// <param name="options">The options to consider. Specifically, it will check white and blaclist entries, if available.</param>
        /// <returns>True if it meets the host standards, false otherwise.</returns>
        private static CheckOutput CheckHost(string url, UrlOptions options)
        {
            var output  = new CheckOutput();
            var atIndex = url.IndexOf("@", StringComparison.InvariantCultureIgnoreCase);
            //if atIndex is -1, then we'll just get the whole substring
            var hostName   = url.Substring(atIndex + 1);
            var colonIndex = hostName.IndexOf(":", StringComparison.InvariantCultureIgnoreCase);
            var host       = string.Empty;

            // don't care about modifiedUrl here since this is the last method and there's nothing left to check.
            output.NewUrl = string.Empty;

            if (colonIndex == -1)
            {
                host = hostName;
            }
            else
            {
                host = hostName.Substring(0, colonIndex);
                var port = -1;
                Int32.TryParse(hostName.Substring(colonIndex + 1), out port);
                if (port <= 0 || port > 65535)
                {
                    output.IsValid = false;
                    return(output);
                }
            }

            // broke these out from the below if statement simply for readability
            var isIp   = Validator.IsIp(host, IpVersion.Four) || Validator.IsIp(host, IpVersion.Six);
            var isFqdn = Validator.IsFqdn(host);

            if (!isIp && !isFqdn &&
                !string.Equals(host, "localhost", StringComparison.InvariantCultureIgnoreCase))
            {
                output.IsValid = false;
                return(output);
            }

            if (options.HostWhitelist != null && !options.HostWhitelist.Contains(host))
            {
                output.IsValid = false;
                return(output);
            }

            if (options.HostBlacklist != null && !options.HostBlacklist.Contains(host))
            {
                output.IsValid = false;
                return(output);
            }

            output.IsValid = true;
            return(output);
        }
Exemple #6
0
        public override CheckOutput ExecuteCheck(Excel.Worksheet ws, DefinedNames definedNames, CheckObj check)
        {
            _ws           = ws;
            _nomiDefiniti = definedNames;
            _check        = check;

            CheckOutput n = new CheckOutput();

            switch (check.Type)
            {
            case 1:
                n = CheckFunc1();
                break;
            }

            return(n);
        }
Exemple #7
0
        /// <summary>
        /// Checks the url string for query string violations.
        /// </summary>
        /// <param name="url">The url to check.</param>
        /// <param name="options">The options to consider. Ignored.</param>
        /// <returns>True if it meets the query string standards, false otherwise.</returns>
        private static CheckOutput CheckQueryString(string url, UrlOptions options)
        {
            var output           = new CheckOutput();
            var queryStringIndex = url.IndexOf("?", StringComparison.InvariantCultureIgnoreCase);

            if (queryStringIndex > -1)
            {
                var queryStringValue = url.Substring(queryStringIndex + 1);
                output.NewUrl  = url.Substring(0, queryStringIndex);
                output.IsValid = !string.IsNullOrEmpty(queryStringValue);
            }
            else
            {
                output.NewUrl  = url;
                output.IsValid = true;
            }

            return(output);
        }
Exemple #8
0
        /// <summary>
        /// Checks the url string for protocol violations.
        /// </summary>
        /// <param name="url">The url to check.</param>
        /// <param name="options">The options to consider. Specifically, this will conditionally check the RequireProtocol option.</param>
        /// <returns>True if it meets the protocol standards, false otherwise.</returns>
        private static CheckOutput CheckProtocol(string url, UrlOptions options)
        {
            var output           = new CheckOutput();
            var protocolEndIndex = url.IndexOf("://", StringComparison.InvariantCultureIgnoreCase);

            if (protocolEndIndex > -1)
            {
                var protocol = url.Substring(0, protocolEndIndex);
                // this is not a one character indexof, so need to account for all three character we were looking for
                output.NewUrl  = url.Substring(protocolEndIndex + 3);
                output.IsValid = options.Protocols.Contains(protocol);
            }
            else
            {
                output.NewUrl  = url;
                output.IsValid = !options.RequireProtocol;
            }

            return(output);
        }
Exemple #9
0
        /// <summary>
        /// Determines whether the given string value, <paramref name="url"/>, qualifies as a Url.
        /// </summary>
        /// <param name="url">Value to check.</param>
        /// <param name="options">Options to consider.</param>
        /// <returns>True if a Url, false otherwise.</returns>
        public static bool IsUrl(string url, UrlOptions options = null)
        {
            options = options ?? new UrlOptions();

            if (string.IsNullOrEmpty(url) || url.Length >= 2083 || url.StartsWith("mailto:", StringComparison.InvariantCultureIgnoreCase))
            {
                return(false);
            }

            var output = new CheckOutput();
            // i purposely structured each of these "check" methods the same way so this list of Funcs works.
            var checkFunctions = new List <Func <string, UrlOptions, CheckOutput> >
            {
                CheckProtocol,
                CheckHash,
                CheckQueryString,
                CheckPath,
                CheckAuth,
                CheckHost
            };

            // Mimicking the source validator.js means we are monkeying with the url value in many of these functions
            // as we trim down the url string to verify. In other words, most of these methods remove the portion of the string that they validate
            // only leaving behind that which has yet to be validated.
            foreach (var f in checkFunctions)
            {
                output = f(url, options);
                if (!output.IsValid)
                {
                    break;
                }

                url = output.NewUrl;
            }

            return(output.IsValid);
        }
Exemple #10
0
        /// <summary>
        /// Determines whether the given string value, <paramref name="url"/>, qualifies as a Url.
        /// </summary>
        /// <param name="url">Value to check.</param>
        /// <param name="options">Options to consider.</param>
        /// <returns>True if a Url, false otherwise.</returns>
        public static bool IsUrl(string url, UrlOptions options = null)
        {
            options = options ?? new UrlOptions();

            if (string.IsNullOrEmpty(url) || url.Length >= 2083 || url.StartsWith("mailto:", StringComparison.InvariantCultureIgnoreCase))
            {
                return false;
            }

            var output = new CheckOutput();
            // i purposely structured each of these "check" methods the same way so this list of Funcs works.
            var checkFunctions = new List<Func<string, UrlOptions, CheckOutput>>
            {
                CheckProtocol,
                CheckHash,
                CheckQueryString,
                CheckPath,
                CheckAuth,
                CheckHost
            };

            // Mimicking the source validator.js means we are monkeying with the url value in many of these functions
            // as we trim down the url string to verify. In other words, most of these methods remove the portion of the string that they validate
            // only leaving behind that which has yet to be validated.
            foreach (var f in checkFunctions)
            {
                output = f(url, options);
                if (!output.IsValid)
                {
                    break;
                }
                url = output.NewUrl;
            }

            return output.IsValid;
        }
Exemple #11
0
        /// <summary>
        /// Checks the url string for host violations.
        /// </summary>
        /// <param name="url">The url to check.</param>
        /// <param name="options">The options to consider. Specifically, it will check white and blaclist entries, if available.</param>
        /// <returns>True if it meets the host standards, false otherwise.</returns>
        private static CheckOutput CheckHost(string url, UrlOptions options)
        {
            var output = new CheckOutput();
            var atIndex = url.IndexOf("@", StringComparison.InvariantCultureIgnoreCase);
            //if atIndex is -1, then we'll just get the whole substring
            var hostName = url.Substring(atIndex + 1);
            var colonIndex = hostName.IndexOf(":", StringComparison.InvariantCultureIgnoreCase);
            string host;

            // don't care about modifiedUrl here since this is the last method and there's nothing left to check.
            output.NewUrl = string.Empty;

            if (colonIndex == -1)
                host = hostName;
            else
            {
                host = hostName.Substring(0, colonIndex);
                int port;
                TryParse(hostName.Substring(colonIndex + 1), out port);
                if (port <= 0 || port > 65535)
                {
                    output.IsValid = false;
                    return output;
                }
            }

            var isIp = Validator.IsIp(host, IpVersion.Four) || Validator.IsIp(host, IpVersion.Six);
            var isFqdn = Validator.IsFqdn(host);

            if (!isIp && !isFqdn && !string.Equals(host, "localhost", StringComparison.InvariantCultureIgnoreCase))
            {
                output.IsValid = false;
                return output;
            }

            if (options.HostWhitelist != null && !options.HostWhitelist.Contains(host))
            {
                output.IsValid = false;
                return output;
            }

            if (options.HostBlacklist != null && !options.HostBlacklist.Contains(host))
            {
                output.IsValid = false;
                return output;
            }

            output.IsValid = true;
            return output;
        }
Exemple #12
0
        /// <summary>
        /// Checks the url string for path violations.
        /// </summary>
        /// <param name="url">The url to check.</param>
        /// <param name="options">The options to consider. Ignored.</param>
        /// <returns>True if it meets the path standards, false otherwise.</returns>
        private static CheckOutput CheckPath(string url, UrlOptions options)
        {
            var output = new CheckOutput();
            var pathIndex = url.IndexOf("/", StringComparison.InvariantCultureIgnoreCase);
            if (pathIndex > -1)
            {
                var queryStringValue = url.Substring(pathIndex + 1);
                output.NewUrl = url.Substring(0, pathIndex);
                if (string.IsNullOrEmpty(queryStringValue))
                {
                    output.IsValid = true;
                }
                else
                {
                    output.IsValid = !queryStringValue.Contains(" ");
                }
            }
            else
            {
                output.NewUrl = url;
                output.IsValid = true;
            }

            return output;
        }
Exemple #13
0
        /// <summary>
        /// Checks the url string for query string violations.
        /// </summary>
        /// <param name="url">The url to check.</param>
        /// <param name="options">The options to consider. Ignored.</param>
        /// <returns>True if it meets the query string standards, false otherwise.</returns>
        private static CheckOutput CheckQueryString(string url, UrlOptions options)
        {
            var output = new CheckOutput();
            var queryStringIndex = url.IndexOf("?", StringComparison.InvariantCultureIgnoreCase);
            if (queryStringIndex > -1)
            {
                var queryStringValue = url.Substring(queryStringIndex + 1);
                output.NewUrl = url.Substring(0, queryStringIndex);
                output.IsValid = !string.IsNullOrEmpty(queryStringValue);
            }
            else
            {
                output.NewUrl = url;
                output.IsValid = true;
            }

            return output;
        }
Exemple #14
0
        /// <summary>
        /// Checks the url string for protocol violations.
        /// </summary>
        /// <param name="url">The url to check.</param>
        /// <param name="options">The options to consider. Specifically, this will conditionally check the RequireProtocol option.</param>
        /// <returns>True if it meets the protocol standards, false otherwise.</returns>
        private static CheckOutput CheckProtocol(string url, UrlOptions options)
        {
            var output = new CheckOutput();
            var protocolEndIndex = url.IndexOf("://", StringComparison.InvariantCultureIgnoreCase);
            if (protocolEndIndex > -1)
            {
                var protocol = url.Substring(0, protocolEndIndex);
                // this is not a one character indexof, so need to account for all three character we were looking for
                output.NewUrl = url.Substring(protocolEndIndex + 3);
                output.IsValid = options.Protocols.Contains(protocol);
            }
            else
            {
                output.NewUrl = url;
                output.IsValid = !options.RequireProtocol;
            }

            return output;
        }
Exemple #15
0
        public void RefreshCheck(Check checkFunctions)
        {
            if (Struct.tipoVisualizzazione == "R")
            {
                //sfrutto questo spazio per fare l'aggiornamento dei dati di previsione nel caso di Previsione GAS
                //TODO vedere se si può fare meglio...
                checkFunctions.ExecuteCheck(null, null, null);
            }
            else if (Struct.tipoVisualizzazione == "O")
            {
                SplashScreen.UpdateStatus("Aggiorno Check");
                DefinedNames gotos = new DefinedNames("Main", DefinedNames.InitType.GOTOs);

                //Reset delle celle GOTO di tutto il Workbook
                List <string> gotoRanges = gotos.GetAllFromAddressGOTO();
                foreach (string gotoCell in gotoRanges)
                {
                    Style.RangeStyle(Workbook.Application.Range[gotoCell], backColor: 2, foreColor: 1);
                }

                foreach (Excel.Worksheet ws in Workbook.CategorySheets)
                {
                    DefinedNames definedNames = new DefinedNames(ws.Name, DefinedNames.InitType.CheckNaming);

                    if (definedNames.HasCheck())
                    {
                        foreach (CheckObj check in definedNames.Checks)
                        {
                            CheckOutput o = checkFunctions.ExecuteCheck(ws, definedNames, check);

                            if (o.Node.Nodes.Count > 0)
                            {
                                if (treeViewErrori.Nodes.ContainsKey(o.Node.Name))
                                {
                                    treeViewErrori.Nodes.RemoveByKey(o.Node.Name);
                                }
                                treeViewErrori.Nodes.Add(o.Node);


                                //Coloro le celle GOTO del Main e della scheda corrente
                                List <string> rngToCheck = gotos.GetFromAddressGOTO(check.SiglaEntita);
                                foreach (string gotoCell in rngToCheck)
                                {
                                    if (o.Status == CheckOutput.CheckStatus.Error)
                                    {
                                        Style.RangeStyle(Workbook.Application.Range[gotoCell], backColor: 3, foreColor: 6);
                                    }
                                    else if (o.Status == CheckOutput.CheckStatus.Alert)
                                    {
                                        Style.RangeStyle(Workbook.Application.Range[gotoCell], backColor: 6, foreColor: 3);
                                    }
                                }

                                //Coloro la barra del titolo verticale
                                Range titoloVert = new Range(check.Range);
                                //riduco il range ad una sola cella alla colonna B
                                titoloVert.StartColumn = 2;
                                titoloVert.ColOffset   = 1;
                                titoloVert.RowOffset   = 1;
                                if (o.Status == CheckOutput.CheckStatus.Error)
                                {
                                    Style.RangeStyle(ws.Range[titoloVert.ToString()].MergeArea, backColor: 3, foreColor: 6);
                                }
                                else if (o.Status == CheckOutput.CheckStatus.Alert)
                                {
                                    Style.RangeStyle(ws.Range[titoloVert.ToString()].MergeArea, backColor: 6, foreColor: 3);
                                }
                            }
                            else
                            {
                                if (treeViewErrori.Nodes.ContainsKey(check.SiglaEntita))
                                {
                                    treeViewErrori.Nodes.RemoveByKey(check.SiglaEntita);
                                }
                                //Reset della barra del titolo verticale
                                Range titoloVert = new Range(check.Range);
                                //riduco il range ad una sola cella alla colonna B
                                titoloVert.StartColumn = 2;
                                titoloVert.ColOffset   = 1;
                                titoloVert.RowOffset   = 1;
                                Style.RangeStyle(ws.Range[titoloVert.ToString()].MergeArea, backColor: 2, foreColor: 1);
                            }
                        }
                    }
                }
            }
        }