https://github.com/github/codeql/blob/main/docs/ql-style-guide.md

    image.png

    image.png

    https://github.com/github/codeql/blob/main/java/ql/src/Security/CWE/CWE-338/JHipsterGeneratedPRNG.ql

    1. /**
    2. * @name Detect JHipster Generator Vulnerability CVE-2019-16303
    3. * @description Using a vulnerable version of JHipster to generate random numbers makes it easier for attackers to take over accounts.
    4. * @kind problem
    5. * @problem.severity error
    6. * @security-severity 7.8
    7. * @precision very-high
    8. * @id java/jhipster-prng
    9. * @tags security
    10. * external/cwe/cwe-338
    11. */
    12. import java
    13. import semmle.code.java.frameworks.apache.Lang
    14. private class PredictableApacheRandomStringUtilsMethod extends Method {
    15. PredictableApacheRandomStringUtilsMethod() {
    16. this.getDeclaringType() instanceof TypeApacheRandomStringUtils and
    17. // The one valid use of this type that uses SecureRandom as a source of data.
    18. not this.getName() = "random"
    19. }
    20. }
    21. private class PredictableApacheRandomStringUtilsMethodAccess extends MethodAccess {
    22. PredictableApacheRandomStringUtilsMethodAccess() {
    23. this.getMethod() instanceof PredictableApacheRandomStringUtilsMethod
    24. }
    25. }
    26. private class VulnerableJHipsterRandomUtilClass extends Class {
    27. VulnerableJHipsterRandomUtilClass() {
    28. // The package name that JHipster generated the 'RandomUtil' class in was dynamic. Thus 'hasQualifiedName' can not be used here.
    29. getName() = "RandomUtil"
    30. }
    31. }
    32. private class VulnerableJHipsterRandomUtilMethod extends Method {
    33. VulnerableJHipsterRandomUtilMethod() {
    34. this.getDeclaringType() instanceof VulnerableJHipsterRandomUtilClass and
    35. this.getName().matches("generate%") and
    36. this.getReturnType() instanceof TypeString and
    37. exists(ReturnStmt s |
    38. s = this.getBody().(SingletonBlock).getStmt() and
    39. s.getResult() instanceof PredictableApacheRandomStringUtilsMethodAccess
    40. )
    41. }
    42. }
    43. from VulnerableJHipsterRandomUtilMethod method
    44. select method,
    45. "Weak random number generator used in security sensitive method (JHipster CVE-2019-16303)."