You are here

API.txt in Security Review 8

Same filename and directory in other branches
  1. 6 API.txt
  2. 7 API.txt
For the latest documentation and code examples go to:
https://www.drupal.org/node/2508415

# Security Review API

  * Defining a security check
    * Identifiers
    * Action and messages
    * Help page
    * Evaluation page (optional)
    * Check-specific settings (optional)
      * Form generation
      * Configuration schema
  * Hooks
  * Alterable variables
  * Drush usage

## Defining a security check

  This part of the documentation lets the developer understand the behavior of
  the module. If anything's unclear it is recommended to look at the examples.

  To define a security check for Security Review, one has to create a class that
  extends Drupal\security_review\Check.
  The functions that must be overridden are the following:
    * getNamespace()
    * getTitle()
    * run()
    * help()
    * getMessage()

  ### Identifiers

    There are 5 kinds of identifiers for a given check:
      * namespace
      * machine namespace
      * title
      * machine title
      * id

    The 'namespace' must be manually set for each check by overriding the
    getNamespace() method. This is the human-readable namespace of the check
    (usually the module's name).

    The 'machine namespace' is the version of namespace that is used internally.
    If getMachineNamespace() isn't overridden, then it is produced from the
    human-readable namespace by removing any non-alphanumeric characters and
    replacing spaces with underscores. When overriding getMachineNamespace()
    this rule must be followed.

    The 'title' must be manually set for each check by overriding the getTitle()
    method. This is the human-readable title of the check.

    The 'machine title' has the same relationship to 'title' as 'machine
    namespace' has to 'namespace'. The machine title should be unique to the
    namespace. This might only be achievable by overriding getMachineTitle().

    The 'id' is only used internally and cannot be overridden. It's constructed
    by taking the 'machine namespace' and 'machine title' and putting a hyphen
    between them.

  ### Action and messages

    The part where the actual security check happens is the run() method. This
    method must be overridden, and should always return an instance of
    Drupal\security_review\CheckResult.

    Instantiating a CheckResult:

    CheckResult defines one constructor:
    (Check $check, $result, array $findings, $visible = TRUE, $time = NULL)
      * $check
        The Check that is responsible for the result
      * $result
        An integer that defines the outcome of the check:
          * CheckResult::SUCCESS  - for a successful check
          * CheckResult::FAIL     - for a failed check
          * CheckResult::WARN     - for a check that only raised a warning
          * CheckResult::INFO     - general result for providing information
      * $findings
        An array of findings that can be evaluated. It can be empty.
      * $visible
        Check results can be hidden from the user by setting $visible to FALSE.
      * $time
        Timestamp indicating the time when the result was produced. If left null
        it will be the current time.

    NOTE:
    It's easier to instantiate a result with Check's createResult() method. It
    has the same parameters as the constructor for CheckResult, except the
    $check is left out (set to $this).

    Human-readable messages for each result integer:

    Must be defined by overriding the getMessage() method. The implementation is
    usually a switch-case. For more details take a look at Security Review's own
    Check implementations.

  ### Help page

    Every Check can have its own help page by overriding the help() method. This
    should return a render array.
    See https://www.drupal.org/developing/api/8/render/arrays

  ### Evaluation page (optional)

    The evaluation page is for providing an evaluation of a CheckResult produced
    by the Check. Overriding this is optional, the default implementation
    returns an empty array. If one chooses to override evaluate(), the function
    must return a render array.
    See https://www.drupal.org/developing/api/8/render/arrays

  ### Check-specific settings (optional)

    If the Check requires storage for settings, it can be accessed via
    $this->settings(). This method returns a
    Drupal\security_review\CheckSettingsInterface. It has get() and set()
    methods for accessing the stored configuration, and buildForm(),
    submitForm(), validateForm() for form building. By default Check's
    implementation contains a Drupal\security_review\CheckSettings, which stores
    the values in the Configuration system, and does nothing in its form
    building methods. Usually it's enough to extend this class if the Check
    needs separate settings on the Security Review settings page.

    When using check-specific settings it's recommended to define a
    configuration schema to store the values in their correct types. The schema
    to declare is called security_review.check_settings.[id of check] .

## Hooks

  ### hook_security_review_checks()

    To let Security Review know of the checks defined in the module it has to
    implement hook_security_review_checks(). This hook is fairly simple. It has
    to return an array of check instances.

    For example implementations see security_review.api.php and
    security_review.module and the examples.

  ### hook_security_review_log()

    Provides logging functions for various events:
    Check skipped / enabled
    Check ran
    Check gave a NULL result

    For example implementations see security_review.api.php and
    security_review.module.

## Alterable variables

  To understand what alterable variables are, take a look at
  https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Extension!ModuleHandler.php/function/ModuleHandler%3A%3Aalter/8
  To modify an alterable variable you have to implement hook_[TYPE]_alter.
  An example:

  <?php
  // ...
  /**
   * Implements hook_security_review_unsafe_extensions_alter().
   */
  function my_module_security_review_unsafe_extensions_alter(array &$variable) {
    // Add the .reg file extension to the list of unsafe extensions.
    $variable[] = 'reg';
  }
  ?>

  ### security_review_unsafe_tags

    The list of HTML tags considered to be unsafe.
    See https://www.owasp.org/index.php/XSS_Filter_Evasion_Cheat_Sheet .

    Default variable content is at Security::unsafeTags().

  ### security_review_unsafe_extensions

    The list of file extensions considered to be unsafe for upload. Untrusted
    users should not be allowed to upload files of these extensions.

    Default variable content is at Security::unsafeExtensions().

  ### security_review_file_ignore

    The list of relative and absolute paths to ignore when running the File
    permissions check.

    Default variable content is at FilePermissions::run().

  ### security_review_temporary_files

    The list of files to check for the Temporary files security check.

    Default variable definition is at TemporaryFiles::run().

## Drush usage

  Run the checklist via Drush with the "drush security-review" command.
  Consult the Drush help on the security-review command for more information.

File

API.txt
View source
  1. For the latest documentation and code examples go to:
  2. https://www.drupal.org/node/2508415
  3. # Security Review API
  4. * Defining a security check
  5. * Identifiers
  6. * Action and messages
  7. * Help page
  8. * Evaluation page (optional)
  9. * Check-specific settings (optional)
  10. * Form generation
  11. * Configuration schema
  12. * Hooks
  13. * Alterable variables
  14. * Drush usage
  15. ## Defining a security check
  16. This part of the documentation lets the developer understand the behavior of
  17. the module. If anything's unclear it is recommended to look at the examples.
  18. To define a security check for Security Review, one has to create a class that
  19. extends Drupal\security_review\Check.
  20. The functions that must be overridden are the following:
  21. * getNamespace()
  22. * getTitle()
  23. * run()
  24. * help()
  25. * getMessage()
  26. ### Identifiers
  27. There are 5 kinds of identifiers for a given check:
  28. * namespace
  29. * machine namespace
  30. * title
  31. * machine title
  32. * id
  33. The 'namespace' must be manually set for each check by overriding the
  34. getNamespace() method. This is the human-readable namespace of the check
  35. (usually the module's name).
  36. The 'machine namespace' is the version of namespace that is used internally.
  37. If getMachineNamespace() isn't overridden, then it is produced from the
  38. human-readable namespace by removing any non-alphanumeric characters and
  39. replacing spaces with underscores. When overriding getMachineNamespace()
  40. this rule must be followed.
  41. The 'title' must be manually set for each check by overriding the getTitle()
  42. method. This is the human-readable title of the check.
  43. The 'machine title' has the same relationship to 'title' as 'machine
  44. namespace' has to 'namespace'. The machine title should be unique to the
  45. namespace. This might only be achievable by overriding getMachineTitle().
  46. The 'id' is only used internally and cannot be overridden. It's constructed
  47. by taking the 'machine namespace' and 'machine title' and putting a hyphen
  48. between them.
  49. ### Action and messages
  50. The part where the actual security check happens is the run() method. This
  51. method must be overridden, and should always return an instance of
  52. Drupal\security_review\CheckResult.
  53. Instantiating a CheckResult:
  54. CheckResult defines one constructor:
  55. (Check $check, $result, array $findings, $visible = TRUE, $time = NULL)
  56. * $check
  57. The Check that is responsible for the result
  58. * $result
  59. An integer that defines the outcome of the check:
  60. * CheckResult::SUCCESS - for a successful check
  61. * CheckResult::FAIL - for a failed check
  62. * CheckResult::WARN - for a check that only raised a warning
  63. * CheckResult::INFO - general result for providing information
  64. * $findings
  65. An array of findings that can be evaluated. It can be empty.
  66. * $visible
  67. Check results can be hidden from the user by setting $visible to FALSE.
  68. * $time
  69. Timestamp indicating the time when the result was produced. If left null
  70. it will be the current time.
  71. NOTE:
  72. It's easier to instantiate a result with Check's createResult() method. It
  73. has the same parameters as the constructor for CheckResult, except the
  74. $check is left out (set to $this).
  75. Human-readable messages for each result integer:
  76. Must be defined by overriding the getMessage() method. The implementation is
  77. usually a switch-case. For more details take a look at Security Review's own
  78. Check implementations.
  79. ### Help page
  80. Every Check can have its own help page by overriding the help() method. This
  81. should return a render array.
  82. See https://www.drupal.org/developing/api/8/render/arrays
  83. ### Evaluation page (optional)
  84. The evaluation page is for providing an evaluation of a CheckResult produced
  85. by the Check. Overriding this is optional, the default implementation
  86. returns an empty array. If one chooses to override evaluate(), the function
  87. must return a render array.
  88. See https://www.drupal.org/developing/api/8/render/arrays
  89. ### Check-specific settings (optional)
  90. If the Check requires storage for settings, it can be accessed via
  91. $this->settings(). This method returns a
  92. Drupal\security_review\CheckSettingsInterface. It has get() and set()
  93. methods for accessing the stored configuration, and buildForm(),
  94. submitForm(), validateForm() for form building. By default Check's
  95. implementation contains a Drupal\security_review\CheckSettings, which stores
  96. the values in the Configuration system, and does nothing in its form
  97. building methods. Usually it's enough to extend this class if the Check
  98. needs separate settings on the Security Review settings page.
  99. When using check-specific settings it's recommended to define a
  100. configuration schema to store the values in their correct types. The schema
  101. to declare is called security_review.check_settings.[id of check] .
  102. ## Hooks
  103. ### hook_security_review_checks()
  104. To let Security Review know of the checks defined in the module it has to
  105. implement hook_security_review_checks(). This hook is fairly simple. It has
  106. to return an array of check instances.
  107. For example implementations see security_review.api.php and
  108. security_review.module and the examples.
  109. ### hook_security_review_log()
  110. Provides logging functions for various events:
  111. Check skipped / enabled
  112. Check ran
  113. Check gave a NULL result
  114. For example implementations see security_review.api.php and
  115. security_review.module.
  116. ## Alterable variables
  117. To understand what alterable variables are, take a look at
  118. https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Extension!ModuleHandler.php/function/ModuleHandler%3A%3Aalter/8
  119. To modify an alterable variable you have to implement hook_[TYPE]_alter.
  120. An example:
  121. // ...
  122. /**
  123. * Implements hook_security_review_unsafe_extensions_alter().
  124. */
  125. function my_module_security_review_unsafe_extensions_alter(array &$variable) {
  126. // Add the .reg file extension to the list of unsafe extensions.
  127. $variable[] = 'reg';
  128. }
  129. ?>
  130. ### security_review_unsafe_tags
  131. The list of HTML tags considered to be unsafe.
  132. See https://www.owasp.org/index.php/XSS_Filter_Evasion_Cheat_Sheet .
  133. Default variable content is at Security::unsafeTags().
  134. ### security_review_unsafe_extensions
  135. The list of file extensions considered to be unsafe for upload. Untrusted
  136. users should not be allowed to upload files of these extensions.
  137. Default variable content is at Security::unsafeExtensions().
  138. ### security_review_file_ignore
  139. The list of relative and absolute paths to ignore when running the File
  140. permissions check.
  141. Default variable content is at FilePermissions::run().
  142. ### security_review_temporary_files
  143. The list of files to check for the Temporary files security check.
  144. Default variable definition is at TemporaryFiles::run().
  145. ## Drush usage
  146. Run the checklist via Drush with the "drush security-review" command.
  147. Consult the Drush help on the security-review command for more information.