Repository navigation
Description
What to do
Add one experimental C# query, githubsecuritylab/cs/forms-auth-ticket-protection-disabled, with its query help and its tests. It reports a protection="None" attribute on the <forms> element of an <authentication mode="Forms"> element in a Web.config file. With None, ASP.NET neither encrypts nor validates the forms authentication ticket, so anyone who can set the cookie can forge a ticket and act as any user.
The query, the help and the tests were written and run before: format, compile with --warnings=error, the unit test and the selection by the default suite all passed. They exist only on a local branch, so recreate them from this issue, starting from main. Create exactly the 15 files below with exactly this content, then verify as described.
Files
All paths are relative to the repository root.
csharp/src/security/CWE-287/FormsAuthTicketProtectionDisabled.ql
/**
* @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'."csharp/src/security/CWE-287/FormsAuthTicketProtectionDisabled.qhelp
<!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><forms></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.<name>.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><forms></code> element is reported only when the <code>mode</code> of its
<code><authentication></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>csharp/test/security/CWE-287/FormsAuthTicketProtectionDisabled/FormsAuthTicketProtectionDisabled.qlref
query: security/CWE-287/FormsAuthTicketProtectionDisabled.ql
csharp/test/security/CWE-287/FormsAuthTicketProtectionDisabled/FormsAuthTicketProtectionDisabled.expected
| 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'. |
csharp/test/security/CWE-287/FormsAuthTicketProtectionDisabled/Program.cs
class Program
{
static void Main() { }
}The next ten files are the test inputs. Four of them must give a result (marked BAD) and the others must not (marked GOOD). Each path below is relative to csharp/test/security/CWE-287/FormsAuthTicketProtectionDisabled/.
Unprotected/Web.config
<?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>UnprotectedInLocation/Web.config
<?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>UnprotectedNamespaced/Web.config
<?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>UnprotectedTransform/Web.Release.config
<?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>Protected/Web.config
<?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>NotFormsMode/Web.config
<?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>InvalidSpelling/Web.config
<?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>Misplaced/Web.config
<?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>NotWebConfig/Program.exe.config
<?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>NotWebConfig/Web-Copy.config
<?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>How to check it
All commands run from the repository root. The pinned CodeQL CLI, the packs and the test stubs are installed by .github/workflows/copilot-setup-steps.yml.
-
Run the test. It must pass against the
.expectedfile above, which has exactly four rows:codeql test run --additional-packs csharp -J=-XX:-UsePerfData --verbosity=progress --ram=2048 csharp/test/security/CWE-287/FormsAuthTicketProtectionDisabledIf the output is not identical, stop and describe the difference in the pull request. Do not edit the expected file to make the test pass.
-
Format and compile with warnings as errors:
codeql query format --check-only csharp/src/security/CWE-287/FormsAuthTicketProtectionDisabled.ql codeql query compile --additional-packs csharp --warnings=error csharp/src/security/CWE-287/FormsAuthTicketProtectionDisabled.ql -
Check that the query help is valid:
codeql generate query-help --format=markdown --output=/tmp/forms-auth-help csharp/src/security/CWE-287/FormsAuthTicketProtectionDisabled.qhelp -
Check the suites. The query must be listed by the first command and by none of the other two (it is selected by kind and precision, so no suite file changes):
codeql resolve queries --additional-packs csharp csharp/src/suites/csharp.qls codeql resolve queries --additional-packs csharp csharp/src/suites/csharp-audit.qls codeql resolve queries --additional-packs csharp csharp/src/suites/csharp-external-api.qls -
Check that no other query in the repository has the same
@id.
Decisions that were made already
Do not reopen them. They are in the file contents above.
- The query requires
mode="Forms"on the<authentication>element. The default mode isWindows, and a<forms>element is not used in the other modes (theNotFormsModetest). - It matches the exact spelling
Noneonly, because the runtime rejects other spellings as an invalid configuration (theInvalidSpellingtest). - It reads only
Web.configandWeb.<name>.configfiles. ASP.NET does not read other.configfiles for this setting (theNotWebConfigtest). - The precision is
highand the tags includeexperimental. The query goes tocsharp/src/security/CWE-287/. The id keeps the shape ofgithubsecuritylab/cs/request-forgery.
Rules for the change
- Create only the 15 files above. Do not change
csharp/src/qlpack.yml, any pack version, lock file (a pack install can rewrite one: revert it), suite, configuration or release file. An earlier local branch bumped the pack version. Do not. - Keep every name neutral. Do not cite or search for any real project, repository or application in code, comments, commit messages or the pull request.
- Do not commit
*.actualfiles,*.testprojdirectories or scratch files. - Pull request title:
[New Query - githubsecuritylab/cs/forms-auth-ticket-protection-disabled] - detect forms authentication tickets that are neither encrypted nor validated. - In the description say what the query detects, list the design decisions above and the test cases, and give your own test and compile results.
CONTRIBUTING.mdasks for a true positive on a real project. The maintainer adds that. Write a section called "Real project true positive" that contains only the sentenceTo be added by the maintainer. - The maintainer measured the query on a database of public sample projects that was built with
--build-mode none: the C# extractor does include.configfiles in such a database, so the query works with the code scanning default setup, its own evaluation cost was 74 ms, and all 13 of its results were true positives. Do not repeat these numbers in the pull request unless you reproduce them.
Activity