Skip to content
Draft
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>

<overview>
<p>
ASP.NET forms authentication keeps the identity of the signed-in user in an authentication ticket,
which is sent to the browser in a cookie. The <code>protection</code> attribute of the
<code>&lt;forms&gt;</code> element in <code>Web.config</code> controls how the ticket is protected.
The value <code>None</code> turns off both encryption and validation, so anyone who can set the
cookie can create or change a ticket and act as any user, including an administrator.
</p>
</overview>

<recommendation>
<p>
Use <code>protection="All"</code>, or remove the <code>protection</code> attribute because
<code>All</code> is the default. It encrypts the ticket and validates it with a message
authentication code.
</p>
</recommendation>

<example>
<p>
The following configuration turns off the protection of the forms authentication ticket:
</p>
<sample language="xml"><![CDATA[<configuration>
<system.web>
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" protection="None" />
</authentication>
</system.web>
</configuration>]]></sample>

<p>
The following configuration encrypts and validates the ticket:
</p>
<sample language="xml"><![CDATA[<configuration>
<system.web>
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" protection="All" />
</authentication>
</system.web>
</configuration>]]></sample>
</example>

<section title="Known limitations">
<ul>
<li>
Only <code>Web.config</code> files and <code>Web.&lt;name&gt;.config</code> transformation files are
examined. A copy under another name, and an application configuration file such as
<code>Program.exe.config</code>, are not used for the settings of a web application and are not
reported.
</li>
<li>
A <code>&lt;forms&gt;</code> element is reported only when the <code>mode</code> of its
<code>&lt;authentication&gt;</code> element is <code>Forms</code>. The default mode is
<code>Windows</code>, and ASP.NET does not use forms authentication in the other modes. A mode that
a parent configuration sets is not considered.
</li>
<li>
The runtime accepts only the exact spelling <code>None</code>. Other spellings, such as
<code>none</code>, make the configuration invalid, so they are not reported.
</li>
<li>
Configuration inheritance and transformations are not evaluated. A value that a transformation file,
such as <code>Web.Release.config</code>, overrides at publish time is still reported in the base
configuration file, and a transformation file that sets the value is reported on its own.
</li>
<li>
The documentation of the <code>protection</code> attribute allows <code>None</code> for sites that
use cookies only for personalization and have less stringent security requirements. Such a site is
reported too.
</li>
</ul>
</section>

<references>
<li>Microsoft Learn: <a href="https://learn.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/1d3t3c61(v=vs.100)">forms Element for authentication (ASP.NET Settings Schema)</a>.</li>
<li>Microsoft Learn: <a href="https://learn.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/532aee0e(v=vs.100)">authentication Element (ASP.NET Settings Schema)</a>.</li>
<li>Microsoft Learn: <a href="https://learn.microsoft.com/en-us/dotnet/api/system.web.configuration.formsprotectionenum">FormsProtectionEnum Enum</a>.</li>
</references>

</qhelp>
47 changes: 47 additions & 0 deletions csharp/src/security/CWE-287/FormsAuthTicketProtectionDisabled.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* @name Forms authentication ticket protection disabled
* @description Setting 'protection' to 'None' on the forms authentication element disables both
* encryption and validation of the forms authentication ticket, so an attacker can
* forge or tamper with the ticket and impersonate any user.
* @kind problem
* @id githubsecuritylab/cs/forms-auth-ticket-protection-disabled
* @problem.severity error
* @security-severity 8.1
* @precision high
* @tags security
* experimental
* external/cwe/cwe-287
*/

import csharp
import semmle.code.asp.WebConfig

/**
* Holds if `file` is a `Web.config` file or a `Web.<name>.config` transformation file. A copy
* under another name, such as `Web - Copy.config`, and an application configuration, such as
* `Program.exe.config`, are not used for the authentication settings of a web application.
*/
private predicate isWebConfigFile(XmlFile file) {
exists(string name | name = file.getBaseName().toLowerCase() |
name = "web.config" or name.matches("web.%.config")
)
}

/**
* Holds if forms authentication is the authentication mode of the `<authentication>` element
* that contains `forms`. The default mode is `Windows`, and forms authentication is only active
* in `Forms` mode.
*/
private predicate isFormsAuthenticationMode(FormsElement forms) {
forms.getParent().(XmlElement).getAttribute("mode").getValue() = "Forms"
}

from FormsElement forms, XmlAttribute protection
where
isWebConfigFile(forms.getFile()) and
isFormsAuthenticationMode(forms) and
protection = forms.getAttribute("protection") and
// The runtime rejects other spellings, such as `none` or ` None `, as an invalid configuration.
protection.getValue() = "None"
select protection,
"The forms authentication ticket is neither encrypted nor validated because 'protection' is set to 'None'."
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
| Unprotected/Web.config:5:7:5:74 | protection=None | The forms authentication ticket is neither encrypted nor validated because 'protection' is set to 'None'. |
| UnprotectedInLocation/Web.config:6:9:6:53 | protection=None | The forms authentication ticket is neither encrypted nor validated because 'protection' is set to 'None'. |
| UnprotectedNamespaced/Web.config:5:7:5:61 | protection=None | The forms authentication ticket is neither encrypted nor validated because 'protection' is set to 'None'. |
| UnprotectedTransform/Web.Release.config:5:7:5:61 | protection=None | The forms authentication ticket is neither encrypted nor validated because 'protection' is set to 'None'. |
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
query: security/CWE-287/FormsAuthTicketProtectionDisabled.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Each location holds one case. The runtime rejects these spellings, so the site does not start. -->
<configuration>
<location path="lowercase">
<system.web>
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" protection="none" /> <!-- GOOD -->
</authentication>
</system.web>
</location>
<location path="uppercase">
<system.web>
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" protection="NONE" /> <!-- GOOD -->
</authentication>
</system.web>
</location>
<location path="padded">
<system.web>
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" protection=" None " /> <!-- GOOD -->
</authentication>
</system.web>
</location>
<location path="mode-lowercase">
<system.web>
<authentication mode="forms">
<forms loginUrl="~/Account/Login" protection="None" /> <!-- GOOD -->
</authentication>
</system.web>
</location>
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<forms protection="None" /> <!-- GOOD: not an authentication setting -->
</appSettings>
<system.web>
<forms protection="None" /> <!-- GOOD: not inside the authentication element -->
<!--
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" protection="None" />
</authentication>
-->
</system.web>
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" protection="None" /> <!-- GOOD: not inside system.web -->
</authentication>
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Each location holds one case. The forms element is not used unless the mode is Forms. -->
<configuration>
<location path="windows">
<system.web>
<authentication mode="Windows">
<forms loginUrl="~/Account/Login" protection="None" /> <!-- GOOD -->
</authentication>
</system.web>
</location>
<location path="none">
<system.web>
<authentication mode="None">
<forms loginUrl="~/Account/Login" protection="None" /> <!-- GOOD -->
</authentication>
</system.web>
</location>
<location path="passport">
<system.web>
<authentication mode="Passport">
<forms loginUrl="~/Account/Login" protection="None" /> <!-- GOOD -->
</authentication>
</system.web>
</location>
<location path="default">
<system.web>
<authentication>
<forms loginUrl="~/Account/Login" protection="None" /> <!-- GOOD: the default mode is Windows -->
</authentication>
</system.web>
</location>
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" protection="None" /> <!-- GOOD: not read by ASP.NET -->
</authentication>
</system.web>
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" protection="None" /> <!-- GOOD: a copy that ASP.NET does not read -->
</authentication>
</system.web>
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Program
{
static void Main() { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Each location holds one case. -->
<configuration>
<location path="all">
<system.web>
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" protection="All" /> <!-- GOOD -->
</authentication>
</system.web>
</location>
<location path="encryption">
<system.web>
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" protection="Encryption" /> <!-- GOOD -->
</authentication>
</system.web>
</location>
<location path="validation">
<system.web>
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" protection="Validation" /> <!-- GOOD -->
</authentication>
</system.web>
</location>
<location path="default">
<system.web>
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" /> <!-- GOOD: the default is All -->
</authentication>
</system.web>
</location>
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" protection="None" timeout="30" /> <!-- BAD -->
</authentication>
</system.web>
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.web>
<authentication mode="Forms">
<forms name=".SITEAUTH" protection="None" /> <!-- BAD -->
</authentication>
</system.web>
</location>
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<system.web>
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" protection="None" /> <!-- BAD -->
</authentication>
</system.web>
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.web>
<authentication mode="Forms" xdt:Transform="Replace">
<forms loginUrl="~/Account/Login" protection="None" /> <!-- BAD -->
</authentication>
</system.web>
</configuration>