# SonarPHP security good practices

**URL:** <https://community.sonarsource.com/t/sonarphp-security-good-practices/878>\
**Category:** Writing rules\
**Tags:** php\
**Created:** [July 10, 2018, 8:33am UTC](https://community.sonarsource.com/t/sonarphp-security-good-practices/878 "2018-07-10T08:33:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pierre-Loup](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/pierre-loup/32/371_2.png) [@Pierre-Loup](https://community.sonarsource.com/u/Pierre-Loup)\
**Post date:** [July 10, 2018, 8:33am UTC](https://community.sonarsource.com/t/sonarphp-security-good-practices/878/1 "2018-07-10T08:33:48Z")

</div>

Hi,

I have been working on a few security rules for SonarPHP.  
Those rules might not perform as well as the ones used in SAST software (no taint or call flow analyses) but they help enforcing security good practices.

Some rules have been designed for Drupal 7 & Drupal 8 codebase.

Here is a list of the rules already written:  
**PHP**

- Functions used for system command execution are forbidden.
- TLS trust chain verification should not be disabled.
- TLS configuration should not be changed dynamically.
- Namespace importing should be preferred over include/require functions.
- unserialize function should not be used on untrusted data.
- Superglobal variable that contains user input should not be used directly. Prefer using the filter\_input() function.
- filter\_input() should not be used with FILTER\_DEFAULT nor FILTER\_UNSAFE\_RAW filter option.

**Drupal**

- Drupal static query should use proper argument substitution.
- Drupal dynamic query should use proper argument substitution.
- Drupal Form API unsanitized user input should not be used.

Are those types of rules wanted on the SonarPHP rules set?

Regards,  
Pierre-Loup

---

<div class="post-metadata">

**Author:** ![Alexandre\_Gigleux](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/alexandre_gigleux/32/26_2.png) [@Alexandre\_Gigleux](https://community.sonarsource.com/u/Alexandre_Gigleux)\
**Post date:** [July 10, 2018, 4:37pm UTC](https://community.sonarsource.com/t/sonarphp-security-good-practices/878/2 "2018-07-10T16:37:49Z")

</div>

Hello Pierre-Loup,

I can recognize just by looking at the title some rules similar to the ones we are working on at SonarSource for PHP but also for other languages.

Do you have a public repository where I can look at your implementation to get more details about what these rules are catching, then we could decide to provide similar feature natively in SonarPHP.  
Also, if you have a way to provide explanations what is the risk covered by each rules, that would be awesome to help developers understand what will be the impact if they don’t follow your recommendations.

Also, do not hesitate to dump your additional rule ideas not yet implemented here: [https://community.sonarsource.com/c/suggestions/rules](https://community.sonarsource.com/c/suggestions/rules)

Thanks

---

<div class="post-metadata">

**Author:** ![Pierre-Loup](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/pierre-loup/32/371_2.png) [@Pierre-Loup](https://community.sonarsource.com/u/Pierre-Loup)\
**Post date:** [July 26, 2018, 4:33pm UTC](https://community.sonarsource.com/t/sonarphp-security-good-practices/878/3 "2018-07-26T16:33:34Z")

</div>

Hello @Alexandre_Gigleux ,

I have no public repository yet but I will publish the source code in the next days on my company’s GitHub.  
I’ll keep you informed.

I had a look at the new security rules visible on SonarPHP source code on GitHub.  
I noticed this pull request : [https://github.com/SonarSource/sonar-php/pull/307](https://github.com/SonarSource/sonar-php/pull/307)

It’s a custom rule I have also writen. I think the SonarPHP GitHub’s implementation is incomplete : it only detects the `exec` function usage. More than one PHP funtion can be used to execute OS commands :

- `passthru`
- `proc_open`
- `popen`
- `shell_exec`
- `system`
- `pcntl_exec`
- `` (the “backticks” PHP Operator)

I am not sure which channel I should use to contibute and discus the new rules impementations. Should I add a comment on the closed pull request ? Should I open an issue ? Or should I add a comment on JIRA ?

Thanks.

---

<div class="post-metadata">

**Author:** ![Alexandre\_Gigleux](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/alexandre_gigleux/32/26_2.png) [@Alexandre\_Gigleux](https://community.sonarsource.com/u/Alexandre_Gigleux)\
**Post date:** [July 27, 2018, 7:49am UTC](https://community.sonarsource.com/t/sonarphp-security-good-practices/878/4 "2018-07-27T07:49:58Z")

</div>

Hello,

I believe the best is to open one thread on the “Suggest new features” in this forum. One thread for one rule to keep things clean.

For “Rule S4721: Executing OS commands is security-sensitive”, we are going to adjust the implementation, no need to do an extra post.

Thanks

---

<div class="post-metadata">

**Author:** ![Pierre-Loup](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/pierre-loup/32/371_2.png) [@Pierre-Loup](https://community.sonarsource.com/u/Pierre-Loup)\
**Post date:** [August 28, 2018, 7:34am UTC](https://community.sonarsource.com/t/sonarphp-security-good-practices/878/5 "2018-08-28T07:34:50Z")

</div>

Hello,

> [@Alexandre\_Gigleux](#):
>
> Do you have a public repository where I can look at your implementation to get more details about what these rules are catching, then we could decide to provide similar feature natively in SonarPHP.  
> Also, if you have a way to provide explanations what is the risk covered by each rules, that would be awesome to help developers understand what will be the impact if they don’t follow your recommendations.
> 
> Also, do not hesitate to dump your additional rule ideas not yet implemented here: [https://community.sonarsource.com/c/suggestions/rules](https://community.sonarsource.com/c/suggestions/rules)

@Alexandre_Gigleux, I have added rules sugestions. You can find rules implementations [here](https://github.com/NijiDigital/sonar-php-rules/) (some rules need to be reworked/improved).

Regards,  
Pierre-Loup

---

<div class="post-metadata">

**Author:** ![saberduck](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/saberduck/32/27_2.png) [@saberduck](https://community.sonarsource.com/u/saberduck)\
**Post date:** [December 8, 2018, 3:26pm UTC](https://community.sonarsource.com/t/sonarphp-security-good-practices/878/6 "2018-12-08T15:26:21Z")

</div>

Split this topic on December 8, 2018, 3:26pm UTC

A post was split to a new topic: [No issue detected in “Damn Vulnerable Web App”](https://community.sonarsource.com/t/no-issue-detected-in-damn-vulnerable-web-app/4969)
