Skip to content

Java: Password in Java EE configuration files#4814

Merged
smowton merged 7 commits into
github:mainfrom
luchua-bc:java/password-in-configuration
Jan 5, 2021
Merged

Java: Password in Java EE configuration files#4814
smowton merged 7 commits into
github:mainfrom
luchua-bc:java/password-in-configuration

Conversation

@luchua-bc
Copy link
Copy Markdown
Contributor

Storing a plaintext password in a configuration file allows anyone who can read the file to access the password-protected resources. Therefore it is a common attack vector.

This is a common issue with many GitHub repositories. The query detects cleartext passwords stored in XML configuration files.

Please consider to merge the PR. Thanks.

Comment thread java/ql/src/experimental/Security/CWE/CWE-555/PasswordInConfigurationFile.qhelp Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-555/PasswordInConfigurationFile.ql Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-555/context.xml Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-555/context.xml
Comment thread java/ql/test/experimental/query-tests/security/CWE-555/applicationContext.xml Outdated
Comment thread java/ql/test/experimental/query-tests/security/CWE-555/context.xml Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-555/PasswordInConfigurationFile.ql Outdated
Comment thread java/ql/test/experimental/query-tests/security/CWE-555/context.xml Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-555/PasswordInConfigurationFile.qhelp Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-555/PasswordInConfigurationFile.ql Outdated
Comment thread java/ql/src/experimental/Security/CWE/CWE-555/PasswordInConfigurationFile.ql Outdated
@smowton
Copy link
Copy Markdown
Contributor

smowton commented Dec 14, 2020

I note from the code scanning warnings (which come from the C# analysis) that this is a near-copy of https://github.com/github/codeql/blob/main/csharp/ql/src/Configuration/PasswordInConfigurationFile.ql -- @github/codeql-csharp, what do you make of the differences (stronger isNotPassword predicate vs. the C# version's third disjunct a.getValue().regexpMatch("(?is).*(pwd|password)\\s*=(?!\\s*;).*") whose meaning is hazy to me but I think it accepts strings like "password=dfsdfdsf", plus whatever (?!\\s*;) is doing)

@hvitved
Copy link
Copy Markdown
Contributor

hvitved commented Dec 16, 2020

@github/codeql-csharp, what do you make of the differences (stronger isNotPassword predicate vs. the C# version's third disjunct a.getValue().regexpMatch("(?is).*(pwd|password)\\s*=(?!\\s*;).*") whose meaning is hazy to me but I think it accepts strings like "password=dfsdfdsf", plus whatever (?!\\s*;) is doing)

Looks like this query also has that disjunct: https://github.com/github/codeql/pull/4814/files#diff-a072f910060223db1a33816cd6374601e46f0bb2759b46e60edd0e83e3c90636R36. Not sure why isNotPassword is not applied to all disjuncts.

@smowton
Copy link
Copy Markdown
Contributor

smowton commented Dec 16, 2020

Doh yeah. @luchua-bc can we usefully use isNotPassword for the cases not currently covered?

@luchua-bc
Copy link
Copy Markdown
Contributor Author

luchua-bc commented Dec 16, 2020

Thanks @hvitved for reviewing this PR.

I copied the third disjunct from the C# query. Typically .NET applications have a database configuration like the following:

  <connectionStrings>
    <add name="sqlServerConnection" connectionString="Data Source=servername;Database=dbname;User ID=username;Password=dfsdfdsf;Connection Timeout=30;" providerName="System.Data.SqlClient" />
  </connectionStrings>

This is the reason for this disjunct.

While Java EE applications have a more structured configuration like:

<Resource name="jdbc/exampleDS" auth="Container" type="javax.sql.DataSource"
               maxTotal="100" maxIdle="30" maxWaitMillis="10000"
               username="root" password="1234"
               driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://www.example.com:3306/proj" />

I did a quick search on Java GitHub repositories and cannot find any repository with the .NET style configuration of connection strings.

Will it be a good idea to simply delete this disjunct since it won't be useful to most Java projects? Or I can apply the same isNotPassword predicate to the pattern connection="...Password=dfsdfdsf..." for completeness purpose.

Please advise.

Thanks,
@luchua-bc

@luchua-bc
Copy link
Copy Markdown
Contributor Author

I've modified the query to apply the same check (blank, placeholder and encrypted) to embedded passwords. As method parameters cannot be string, I developed a new method with the parameter XMLAttribute.

Please let me know if this is not what we want.

@smowton
Copy link
Copy Markdown
Contributor

smowton commented Dec 17, 2020

They can be string -- do you say that because you got a message relating to binding? If so you might need to write something like the following example, which uses bindingset to indicate that the set of matches is bounded if the parameter name is itself bounded. This is necessary only when the body of the predicate doesn't make it obviously finite -- for example, name = ["hello", "world"] does not need this annotation.

/**
   * Holds if the name of this algorithm matches `name` modulo case,
   * white space, dashes and underscores.
   */
  bindingset[name]
  predicate matchesName(string name) {
    exists(name.regexpReplaceAll("[-_]", "").regexpFind("(?i)\\Q" + getName() + "\\E", _, _))
  }

See also https://codeql.github.com/docs/ql-language-reference/predicates/#binding-sets

@luchua-bc
Copy link
Copy Markdown
Contributor Author

Great. Thanks a lot @smowton. I've revamped the functions to take a string parameter, which helps to remove the duplicate code and make the query more clear.

Copy link
Copy Markdown
Contributor

@smowton smowton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, will get a code-owner review


import java

/* Holds if the attribute value is not a cleartext password */
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regular comments should be /** doc comments (here and below)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @smowton. I've made the change.

@smowton smowton self-assigned this Jan 4, 2021
@smowton smowton merged commit e87fd86 into github:main Jan 5, 2021
@luchua-bc luchua-bc deleted the java/password-in-configuration branch January 5, 2021 13:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants