CVE-2017-0929 (published 2018-07-03T21:29Z)

DNN (aka DotNetNuke) before 9.2.0 suffers from a Server-Side Request Forgery (SSRF) vulnerability in the DnnImageHandler class. Attackers may be able to access information about internal network resources.

Severity: MEDIUM
CVSS v2 base score: 5.0 (AV:N/AC:L/Au:N/C:P/I:N/A:N)
CVSS v3 base score: 7.5, HIGH (CVSS:3.0/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:N/A:N)

Weakness information:
cwe_id: CWE-918
cwe_name: Server-Side Request Forgery (SSRF)
description: The web server receives a URL or similar request from an upstream component and retrieves the contents of this URL, but it does not sufficiently ensure that the request is being sent to the expected destination.
extended_description: By providing URLs to unexpected hosts or ports, attackers can make it appear that the server is sending the request, possibly bypassing access controls such as firewalls that prevent the attackers from accessing the URLs directly. The server can be used as a proxy to conduct port scanning of hosts in internal networks, use other URLs such as that can access documents on the system (using file://), or use other protocols such as gopher:// or tftp://, which may provide greater control over the contents of requests.
         

Fix information:
repo_url: https://github.com/dnnsoftware/Dnn.Platform
commit_details: repo_url: https://github.com/dnnsoftware/Dnn.Platform
author: George Alatrash
author_date: 2018-01-04 17:08:02-08:00
msg: DNN-10601: preventing handling external URLs in image handler.
repository: repo_url: https://github.com/dnnsoftware/Dnn.Platform
repo_name: dnnsoftware/Dnn.Platform
description: DNN (formerly DotNetNuke) is the leading open source web content management platform (CMS) in the Microsoft ecosystem.
repo_language: C#
owner: dnnsoftware
file_changes: filename: DnnImageHandler.cs
change_type: ModificationType.MODIFY
diff: @@ -1,4 +1,5 @@
 ﻿using System;
+using System.Collections.Generic;
 using System.Collections.Specialized;
 using System.Configuration;
 using System.Drawing;
@@ -233,11 +234,14 @@ public override ImageInfo GenerateImage(NameValueCollection parameters)
                         }
                         else if (!string.IsNullOrEmpty(parameters["Url"]))
                         {
-                            if (!parameters["Url"].StartsWith("http"))
+                            var url = parameters["Url"];
+                            // allow only site resources when using the url parameter
+                            if (!url.StartsWith("http") || !UriBelongsToSite(new Uri(url)))
                             {
                                 return GetEmptyImageInfo();
                             }
-                            imgUrl = parameters["Url"];
+
+                            imgUrl = url;
                         }
 
                         if (string.IsNullOrEmpty(parameters["format"]))
@@ -555,5 +559,20 @@ private static ImageFormat GetImageFormat(string extension)
                     return ImageFormat.Png;
             }
         }
+
+        // checks whether the uri belongs to any of the site-wide aliases
+        private static bool UriBelongsToSite(Uri uri)
+        {
+            IEnumerable<string> hostAliases =
+                from PortalAliasInfo alias in PortalAliasController.Instance.GetPortalAliases().Values
+                 select alias.HTTPAlias.ToLowerInvariant();
+
+            // if URI, for example, = "http(s)://myDomain:80/DNNDev/myPage?var=name" , then the two strings will be
+            // uriNoScheme1 = "mydomain/dnndev/mypage"  -- lower case
+            // uriNoScheme2 = "mydomain:80/dnndev/mypage"  -- lower case
+            var uriNoScheme1 = (uri.DnsSafeHost + uri.LocalPath).ToLowerInvariant();
+            var uriNoScheme2 = (uri.Authority + uri.LocalPath).ToLowerInvariant();
+            return hostAliases.Any(alias => uriNoScheme1.StartsWith(alias) || uriNoScheme2.StartsWith(alias));
+        }
     }
 }
\ No newline at end of file

programming_language: C#
filename: ImageResizeTransform.cs
change_type: ModificationType.MODIFY
diff: @@ -113,7 +113,10 @@ public int Border
         /// <param name="image">Input image</param>
         /// <returns>Image result after image transformation</returns>
         public override Image ProcessImage(Image image)
-		{
+        {
+            if (image == null)
+                return null;    
+
             if (MaxWidth > 0)
             {
                 Width = image.Width > MaxWidth ? MaxWidth : image.Width;

programming_language: C#
filename: ImageHandlerInternal.cs
change_type: ModificationType.MODIFY
diff: @@ -426,7 +426,7 @@ private void RenderImage(Image image, Stream outStream)
                         Param = { [0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, ImageCompression) }
                     };
                     var ici = GetEncoderInfo(GetImageMimeType(ContentType));
-                    image.Save(outStream, ici, eps);
+                    image?.Save(outStream, ici, eps);
                 }
             }
             finally

programming_language: C#
filename: DNN.Integration.Test.Framework.csproj
change_type: ModificationType.MODIFY
diff: @@ -81,6 +81,7 @@
     <Compile Include="Helpers\FolderHelper.cs" />
     <Compile Include="Helpers\HostSettingsHelper.cs" />
     <Compile Include="Helpers\MockComponentHelper.cs" />
+    <Compile Include="Helpers\PortalAliasHelper.cs" />
     <Compile Include="Helpers\PortalInfoHelper.cs" />
     <Compile Include="Helpers\PortalSettingsHelper.cs" />
     <Compile Include="Helpers\StoredProcedureStatsHelper.cs" />

programming_language: PowerShell
filename: PortalAliasHelper.cs
change_type: ModificationType.ADD
diff: @@ -0,0 +1,40 @@
+﻿// DotNetNuke® - http://www.dnnsoftware.com
+// Copyright (c) 2002-2018, DNN Corp.
+// All Rights Reserved
+
+using System.Collections.Generic;
+using System.Linq;
+using DotNetNuke.Entities.Portals;
+
+namespace DNN.Integration.Test.Framework.Helpers
+{
+    public class PortalAliasHelper
+    {
+        public static PortalAliasInfo GetPrimaryPortalAlias(int portalId = 0)
+        {
+            return DatabaseHelper.ExecuteStoredProcedure<PortalAliasInfo>("GetPortalAliases").First(p => p.PortalID == portalId && p.IsPrimary);
+        }
+
+        public static IEnumerable<PortalAliasInfo> GetPortalAliaes(int portalId = 0)
+        {
+            return DatabaseHelper.ExecuteStoredProcedure<PortalAliasInfo>("GetPortalAliases").Where(p => p.PortalID == portalId);
+        }
+
+        public static IEnumerable<PortalAliasInfo> GetAllPortalAliaes()
+        {
+            return DatabaseHelper.ExecuteStoredProcedure<PortalAliasInfo>("GetPortalAliases");
+        }
+
+        public static void AddPortalAlias(PortalAliasInfo portal)
+        {
+            DatabaseHelper.ExecuteStoredProcedure("AddPortalAlias",
+                portal.PortalID,
+                portal.HTTPAlias,
+                portal.CultureCode,
+                portal.Skin,
+                portal.BrowserType,
+                portal.IsPrimary,
+                -1);
+        }
+    }
+}

programming_language: C#
filename: DotNetNuke.Tests.Integration.csproj
change_type: ModificationType.MODIFY
diff: @@ -98,6 +98,7 @@
     <Compile Include="PersonaBar\Content\Pages\AddPageTests.cs" />
     <Compile Include="PersonaBar\Content\Pages\PageUrlsTests.cs" />
     <Compile Include="PersonaBar\Manage\Users\UsersFiltersTests.cs" />
+    <Compile Include="Tests\Library\ImageHandlerTests.cs" />
     <Compile Include="Tests\Portals\PortalInfoTests.cs" />
     <Compile Include="Tests\Portals\PortalSettingsTests.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />

programming_language: PowerShell
filename: ImageHandlerTests.cs
change_type: ModificationType.ADD
diff: @@ -0,0 +1,76 @@
+﻿// DotNetNuke® - http://www.dnnsoftware.com
+// Copyright (c) 2002-2018, DNN Corp.
+// All Rights Reserved
+
+using System.Linq;
+using DNN.Integration.Test.Framework;
+using DNN.Integration.Test.Framework.Helpers;
+using DotNetNuke.Entities.Portals;
+using NUnit.Framework;
+
+namespace DotNetNuke.Tests.Integration.Tests.Library
+{
+    [TestFixture]
+    public class ImageHandlerTests : IntegrationTestBase
+    {
+        // we assume this GIF image exists in all DNN sites and it shouldn't be PNG
+        private const string HandlerPath = "/DnnImageHandler.ashx?mode=file&w=1&url={0}/images/1x1.GIF";
+
+        [Test]
+        public void Using_Image_Handler_For_Foreign_Site_ShouldFail()
+        {
+            var session = WebApiTestHelper.GetAnnonymousConnector();
+            var relativeUrl = string.Format(HandlerPath, "https://google.com");
+
+            var response = session.GetContent(relativeUrl).Content.ReadAsStringAsync().Result;
+            Assert.IsTrue(response.StartsWith("�PNG\r\n"), $"Content = {response}");
+        }
+
+        [Test]
+        public void Using_Image_Handler_For_Main_Alias_ShouldPass()
+        {
+            var session = WebApiTestHelper.GetAnnonymousConnector();
+            var relativeUrl = string.Format(HandlerPath, AppConfigHelper.SiteUrl);
+
+            var response = session.GetContent(relativeUrl).Content.ReadAsStringAsync().Result;
+            Assert.IsTrue(response.StartsWith("GIF89a"), $"Content = {response}");
+        }
+
+        [Test]
+        public void Using_Image_Handler_From_All_Alias_ShouldPass()
+        {
+            PortalAliasInfo pai;
+            var aliases = PortalAliasHelper.GetPortalAliaes().ToList();
+            if (aliases.Count == 1)
+            {
+                var primary = aliases.First();
+                pai = new PortalAliasInfo
+                {
+                    PortalID = primary.PortalID,
+                    HTTPAlias = "my-" + primary.HTTPAlias,
+                    CultureCode = primary.CultureCode,
+                    Skin = primary.Skin,
+                    BrowserType = primary.BrowserType,
+                    IsPrimary = false,
+
+                };
+            }
+            else
+            {
+                pai = aliases.First(a => a.PortalAliasID > 1);
+            }
+
+            var session = WebApiTestHelper.GetAnnonymousConnector();
+            aliases = PortalAliasHelper.GetPortalAliaes().ToList();
+            foreach (var alias in aliases)
+            {
+                var relativeUrl = string.Format(HandlerPath, AppConfigHelper.SiteUrl);
+                var absoluteUrl = $"http://{alias.HTTPAlias}{relativeUrl}";
+                LogText("Getting image from " + absoluteUrl);
+
+                var response = session.GetContent(absoluteUrl).Content.ReadAsStringAsync().Result;
+                Assert.IsTrue(response.StartsWith("GIF89a"), $"Url: {absoluteUrl} / Content = {response}");
+            }
+        }
+    }
+}
\ No newline at end of file

programming_language: C#
filename: AuthCookieTests.cs
change_type: ModificationType.MODIFY
diff: @@ -1,8 +1,10 @@
-﻿using System;
+﻿// DotNetNuke® - http://www.dnnsoftware.com
+// Copyright (c) 2002-2018, DNN Corp.
+// All Rights Reserved
+
+using System;
 using System.Net;
 using System.Net.Http;
-using System.Net.Http.Headers;
-using System.Threading;
 using DNN.Integration.Test.Framework;
 using DNN.Integration.Test.Framework.Helpers;
 using NUnit.Framework;

programming_language: C#