Skip to content

Commit

Permalink
Reject requests for big packages.
Browse files Browse the repository at this point in the history
  • Loading branch information
dotnetjunky authored and Tim Lovell-Smith committed Dec 12, 2012
1 parent d258c7c commit 60cca34
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 19 deletions.
12 changes: 10 additions & 2 deletions Website/Controllers/PackageFilesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace NuGetGallery.Controllers
{
public partial class PackageFilesController : Controller
{
private const long MaximumAllowedPackageFileSize = 3L * 1024 * 1024; // maximum package size = 3MB

private readonly IPackageService packageSvc;
private readonly IPackageFileService packageFileSvc;
private readonly ICacheService cacheSvc;
Expand All @@ -29,6 +31,11 @@ public virtual ActionResult Contents(string id, string version)
return HttpNotFound();
}

if (package.PackageFileSize > MaximumAllowedPackageFileSize)
{
return View("PackageTooBig");
}

IPackage packageFile = NuGetGallery.Helpers.PackageHelper.GetPackageFromCacheOrDownloadIt(package, cacheSvc, packageFileSvc);
PackageItem rootFolder = PathToTreeConverter.Convert(packageFile.GetFiles());

Expand All @@ -45,6 +52,7 @@ public virtual ActionResult ShowFileContent(string id, string version, string fi
return HttpNotFound();
}

// treat image files specially
if (FileHelper.IsImageFile(file.Path))
{
return new ImageResult(file.GetStream(), FileHelper.GetMimeType(file.Path));
Expand Down Expand Up @@ -93,7 +101,7 @@ private bool TryGetPackageFile(string id, string version, string filePath, out I
}

Package package = packageSvc.FindPackageByIdAndVersion(id, version);
if (package == null)
if (package == null || package.PackageFileSize > MaximumAllowedPackageFileSize)
{
return false;
}
Expand All @@ -112,4 +120,4 @@ private bool TryGetPackageFile(string id, string version, string filePath, out I
return true;
}
}
}
}
20 changes: 4 additions & 16 deletions Website/Helpers/FileHelper.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Web;

namespace NuGetGallery.Helpers
{
Expand All @@ -19,7 +17,7 @@ internal static class FileHelper

private static readonly string[] ImageFileExtensions = new[]
{
".PNG", ".GIF", ".JPG"
".PNG", ".GIF", ".JPG", ".BMP", ".JPEG"
};

public static bool IsBinaryFile(string path)
Expand All @@ -36,20 +34,10 @@ public static bool IsImageFile(string path)

internal static string GetMimeType(string filePath)
{
string extension = Path.GetExtension(filePath).ToLowerInvariant();
if (extension.Equals(".png"))
string extension = Path.GetExtension(filePath).ToUpperInvariant();
if (ImageFileExtensions.Contains(extension))
{
return "image/png";
}

if (extension.Equals(".jpg"))
{
return "image/jpeg";
}

if (extension.Equals(".gif"))
{
return "image/gif";
return "image/" + extension.Substring(1); // omit the dot in front of extension
}

return "image";
Expand Down
2 changes: 1 addition & 1 deletion Website/Views/PackageFiles/Contents.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@
$('#fileContentArea').text('Loading...');


var errorMessage = '*** An error occurred while loading this file\'s contents.';
var errorMessage = '*** An error occurred while loading this file\'s contents. ***';
$.ajax(viewUrl,
{
success: function (data, status) {
Expand Down
1 change: 1 addition & 0 deletions Website/Views/PackageFiles/PackageTooBig.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>The package that you requested is too big. Please use <a href="http://nuget.codeplex.com/releases/view/59864" target="_blank">NuGet Package Explorer</a> to view the contents of it.</p>

0 comments on commit 60cca34

Please sign in to comment.