Skip to content

Commit 47894a8

Browse files
committed
Correção de vulnerabilidade de Path traversal attack
1 parent 3592451 commit 47894a8

File tree

6 files changed

+40
-0
lines changed

6 files changed

+40
-0
lines changed

DFe.Utils/FuncoesXml.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ public static T ArquivoXmlParaClasse<T>(string arquivo, bool ignorarOrdenacaoEle
8484
serializador = BuscarNoCache(keyNomeClasseEmUso, typeof(T));
8585
}
8686

87+
if(arquivo.Contains("../") || arquivo.Contains("..\\"))
88+
{
89+
throw new Exception("Caminho do arquivo não pode conter ../ ou ..\\");
90+
}
91+
8792
var stream = new FileStream(arquivo, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
8893
try
8994
{

Dev.VersaoAssemblies/ProgramMain.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ private static void FazerAlteracaoDasVersoes(string versaoString)
9191

9292
private static void AlterarVersaoDoAssembly(string infoFile, string versaoString)
9393
{
94+
if(infoFile.Contains("../") || infoFile.Contains("..\\"))
95+
{
96+
throw new Exception("Caminho do arquivo não pode conter ../ ou ..\\");
97+
}
98+
9499
var content = File.ReadAllText(infoFile);
95100

96101
var regexVersion = new Regex("assembly: AssemblyVersion.+\\)", RegexOptions.IgnoreCase);

NFe.Danfe.AppTeste.OpenFast/Funcoes.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ public static string BuscarArquivoXml(string caminho)
2323
throw new Exception("Caminho do arquivo incorreto! Arquivo não foi encontrado.");
2424
}
2525

26+
if(caminho.Contains("../") || caminho.Contains("..\\"))
27+
{
28+
throw new Exception("Caminho do arquivo não pode conter ../ ou ..\\");
29+
}
30+
2631
return File.ReadAllText(caminho);
2732
}
2833

@@ -36,6 +41,12 @@ public static void SalvaArquivoGerado(string caminhoXmlAnterior, string extensao
3641
//https://github.com/FastReports/FastReport/blob/master/Demos/OpenSource/FastReport.OpenSource.Web.Vue/Controllers/ReportsController.cs
3742

3843
var caminhoArquivo = Path.GetDirectoryName(ArrumaCaminho(caminhoXmlAnterior)) + "/generated" + DateTime.Now.ToString("ddMMyyyy_HHmmss") + extensao;
44+
45+
if(caminhoArquivo.Contains("../") || caminhoArquivo.Contains("..\\"))
46+
{
47+
throw new Exception("Caminho do arquivo não pode conter ../ ou ..\\");
48+
}
49+
3950
File.WriteAllBytes(caminhoArquivo, bytes);
4051

4152
Console.Clear();

NFe.Danfe.Html/CrossCutting/Utils.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ public static T XmlStringParaClasse<T>(string input) where T : class
7373
/// <param name="nomeArquivo">Nome do arquivo</param>
7474
public static void DelatarArquivo(string caminho, string nomeArquivo)
7575
{
76+
if(caminho.Contains("../") || caminho.Contains("..\\"))
77+
{
78+
throw new Exception("Caminho do arquivo não pode conter ../ ou ..\\");
79+
}
80+
7681
var c1 = Path.Combine(caminho, nomeArquivo);
7782
File.Delete(c1);
7883
}
@@ -91,6 +96,9 @@ public static void EscreverArquivo(string caminho, string nomeArquivo, string co
9196
throw new ArgumentNullException(nameof(nomeArquivo), "O nome do arquivo deve ser informado");
9297
if (conteudo == null)
9398
throw new ArgumentNullException(nameof(conteudo), "O conteúdo do arquivo deve ser informado");
99+
if(caminho.Contains("../") || caminho.Contains("..\\"))
100+
throw new Exception("Caminho do arquivo não pode conter ../ ou ..\\");
101+
94102
CriarPastaSeNaoExistir(caminho);
95103
var encodedText = Encoding.UTF8.GetBytes(conteudo);
96104
var c1 = Path.Combine(caminho, nomeArquivo); // Combina caminho do arquivo como nome do arquivo

NFe.Danfe.PdfClown/DanfeDoc.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ public void AdicionarLogoPdf(string path)
107107
{
108108
if (string.IsNullOrWhiteSpace(path)) throw new ArgumentException(nameof(path));
109109

110+
if(path.Contains("../") || path.Contains("..\\"))
111+
{
112+
throw new Exception("Caminho do arquivo não pode conter ../ ou ..\\");
113+
}
114+
110115
using (var fs = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read))
111116
{
112117
AdicionarLogoPdf(fs);

NFe.Integracao/Program.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,12 @@ private static void EnviarNFe(string dadosDoEnvio)
490490

491491
try
492492
{
493+
if(strPathArquivoXml.Contains("../") || strPathArquivoXml.Contains("..\\"))
494+
{
495+
Console.WriteLine("Caminho do arquivo não pode conter ../ ou ..\\");
496+
return;
497+
}
498+
493499
File.ReadAllText(strPathArquivoXml);
494500
}
495501
catch (UnauthorizedAccessException ex)

0 commit comments

Comments
 (0)