Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 19 additions & 22 deletions NFe.Utils/Validacao/Validador.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ internal static string ObterArquivoSchema(ServicoNFe servicoNFe, VersaoServico v

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

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

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

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

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

// Define o tipo de validação
var cfg = new XmlReaderSettings { ValidationType = ValidationType.Schema };

// Previne ataques XXE: nao permite resolver recursos externos
cfg.DtdProcessing = DtdProcessing.Prohibit;
cfg.XmlResolver = null;
XmlReaderSettings cfg = new XmlReaderSettings
{
ValidationType = ValidationType.Schema, // Previne ataques XXE: nao permite resolver recursos externos
DtdProcessing = DtdProcessing.Prohibit,
XmlResolver = null
};

// Carrega o arquivo de esquema
var schemas = new XmlSchemaSet();
schemas.XmlResolver = null;
XmlSchemaSet schemas = new XmlSchemaSet
{
XmlResolver = new XmlUrlResolver()
};

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

if (!(

//Está errado o schema. Pois o certo é ser 20 o length e não 28 como está no schema envIECTE_v4.00xsd
(message.Contains("hashtentativaentrega") && message.Contains("o comprimento atual nao e igual")) ||
(message.Contains("hashtentativaentrega") && message.Contains("o comprimento atual nao e igual")) ||

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

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

// cria um leitor para validação
var validator = XmlReader.Create(new StringReader(stringXml), cfg);
XmlReader validator = XmlReader.Create(new StringReader(stringXml), cfg);
try
{
// Faz a leitura de todos os dados XML
while (validator.Read())
{
}
}
catch
{
while (validator.Read()) { }
}
catch { }
finally
{
validator.Close();
Expand All @@ -180,7 +179,5 @@ public static string[] Valida(ServicoNFe servicoNFe, VersaoServico versaoServico

return falhas.ToString().Trim().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
}


}
}
Loading