Skip to content

Commit 8f908a1

Browse files
authored
Merge pull request #121 from adrbarros/master
Correção na Validação do XML
2 parents 7da22e5 + 63119d0 commit 8f908a1

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

NFe.Utils/Validacao/Validador.cs

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ internal static string ObterArquivoSchema(ServicoNFe servicoNFe, VersaoServico v
101101

102102
public static void Valida(ServicoNFe servicoNFe, VersaoServico versaoServico, string stringXml, bool loteNfe = true, ConfiguracaoServico cfgServico = null)
103103
{
104-
var pathSchema = String.Empty;
104+
string pathSchema = string.Empty;
105105

106106
if (cfgServico == null || (cfgServico != null && string.IsNullOrWhiteSpace(cfgServico.DiretorioSchemas)))
107107
pathSchema = ConfiguracaoServico.Instancia.DiretorioSchemas;
@@ -113,23 +113,26 @@ public static void Valida(ServicoNFe servicoNFe, VersaoServico versaoServico, st
113113

114114
public static string[] Valida(ServicoNFe servicoNFe, VersaoServico versaoServico, string stringXml, bool loteNfe = true, string pathSchema = null)
115115
{
116-
var falhas = new StringBuilder();
116+
StringBuilder falhas = new StringBuilder();
117117

118118
if (!Directory.Exists(pathSchema))
119119
throw new Exception("Diretório de Schemas não encontrado: \n" + pathSchema);
120120

121-
var arquivoSchema = Path.Combine(pathSchema, ObterArquivoSchema(servicoNFe, versaoServico, stringXml, loteNfe));
121+
string arquivoSchema = Path.Combine(pathSchema, ObterArquivoSchema(servicoNFe, versaoServico, stringXml, loteNfe));
122122

123123
// Define o tipo de validação
124-
var cfg = new XmlReaderSettings { ValidationType = ValidationType.Schema };
125-
126-
// Previne ataques XXE: nao permite resolver recursos externos
127-
cfg.DtdProcessing = DtdProcessing.Prohibit;
128-
cfg.XmlResolver = null;
124+
XmlReaderSettings cfg = new XmlReaderSettings
125+
{
126+
ValidationType = ValidationType.Schema, // Previne ataques XXE: nao permite resolver recursos externos
127+
DtdProcessing = DtdProcessing.Prohibit,
128+
XmlResolver = null
129+
};
129130

130131
// Carrega o arquivo de esquema
131-
var schemas = new XmlSchemaSet();
132-
schemas.XmlResolver = null;
132+
XmlSchemaSet schemas = new XmlSchemaSet
133+
{
134+
XmlResolver = new XmlUrlResolver()
135+
};
133136

134137
cfg.Schemas = schemas;
135138
// Quando carregar o eschema, especificar o namespace que ele valida
@@ -141,14 +144,14 @@ public static string[] Valida(ServicoNFe servicoNFe, VersaoServico versaoServico
141144
string message = args.Message.ToLower().RemoverAcentos();
142145

143146
if (!(
144-
147+
145148
//Está errado o schema. Pois o certo é ser 20 o length e não 28 como está no schema envIECTE_v4.00xsd
146-
(message.Contains("hashtentativaentrega") && message.Contains("o comprimento atual nao e igual")) ||
149+
(message.Contains("hashtentativaentrega") && message.Contains("o comprimento atual nao e igual")) ||
147150

148151
//erro de orgaoibge que duplicou em alguns xsds porem a receita federal veio a arrumar posteriormente, mesmo assim alguns não atualizam os xsds
149152
(message.Contains("tcorgaoibge") && message.Contains("ja foi declarado"))
150153

151-
//no futuro adicionar novos aqui...
154+
//no futuro adicionar novos aqui...
152155
))
153156
{
154157
falhas.AppendLine($"[{args.Severity}] - {message} {args.Exception?.Message} " +
@@ -159,17 +162,13 @@ public static string[] Valida(ServicoNFe servicoNFe, VersaoServico versaoServico
159162
};
160163

161164
// cria um leitor para validação
162-
var validator = XmlReader.Create(new StringReader(stringXml), cfg);
165+
XmlReader validator = XmlReader.Create(new StringReader(stringXml), cfg);
163166
try
164167
{
165168
// Faz a leitura de todos os dados XML
166-
while (validator.Read())
167-
{
168-
}
169-
}
170-
catch
171-
{
169+
while (validator.Read()) { }
172170
}
171+
catch { }
173172
finally
174173
{
175174
validator.Close();
@@ -180,7 +179,5 @@ public static string[] Valida(ServicoNFe servicoNFe, VersaoServico versaoServico
180179

181180
return falhas.ToString().Trim().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
182181
}
183-
184-
185182
}
186183
}

0 commit comments

Comments
 (0)