博客

Automatically Adding a Private Constructor with Sensei

十二月 14, 2020
Alan Richardson

Automatically Adding a Private Constructor with Sensei


In a utility class, when the fields and methods are static,  there is no obvious reason why I would ever instantiate it.

e.g. UtilityClass utility = new UtilityClass();

The code below is a simple implementation of a Utility class.

public class UtilityClass {
    public static final Boolean ULTIMATE_TRUTH = true;
    public static boolean getTrue(){
        return ULTIMATE_TRUTH;
    }
}


This is the type of coding pattern that Static Analysis tools can pick up, but they often don't supply the ability to fix the issue.

I can use Sensei to identify the coding pattern, and automatically generate a private constructor to make it impossible for me to instantiate the class.

Searching for the Class

I will add a new recipe on the Utility class called:

  •  Static Classes: create private constructor.


General Settings For Static Classes


And initially, I'll create a simple matcher to search for a class.

search:

  class: {}


This will match any class, which is good enough to let me get started writing a Quick Fix. Once I have a Quick Fix that works, I'll refine the search to make it highlight when there is more likely to be a class that requires a private constructor.

Quick Fix

For the Quick Fix, I will want to generate a private constructor.

In the example class this would look like:

   private UtilityClass(){}

To add the above code to my class, my Quick Fix will add a Method, and the name of the method will be a Mustache template that uses the name of the class.

availableFixes:

- name: "add private constructor"
  actions:
  - addMethod:
      method: "private {{{ name }}}(){}"


In the GUI Editor, I use the Show Variables to create the Mustache template and then edit the field to add the private modifier, brackets, and braces to make it syntactically correct.

Add Private Constructor

This would now allow me to add a private constructor to any class.

The QuickFix preview helps me because I can see the generated code as I write the Mustache template.

Utility Class Add Private Constructor

Now that I know I can fix the problem. I'll refine the search conditions to show the recipe when it is most appropriate.

Search for Missing Constructors

Ideally, I don't want to create a recipe that flags an error against every class. So I'll add some additional conditions in the search so that we only match on classes that do not have a constructor.

search:

  class:
    without:
      child:
        method:
          constructor: true


The YAML is slightly different from the GUI.

In the GUI I configure it to look for a class without a child method which is a constructor 'yes'. We use 'yes' in the GUI instead of 'true' to make the GUI a little more human friendly.


Search for class in code


This recipe will now only reveal itself for any class without a constructor.

Narrow Search for Likely Culprits

So I might want to go further and look for the presence of static methods or fields.

I look for any class without a constructor and which has either all public static fields or all public static methods.

search:

  class:
    with:
      anyOf:
      - child:
          method:
            allOf:
            - modifier: "public"
            - modifier: "static"
      - child:
          field:
            allOf:
            - modifier: "static"
            - modifier: "public"
    without:
      child:
        method:
          constructor: true


Since Sensei is used to help me, as a  programmer, in the IDE, rather than to statically analyze the code and report all errors, this filter is good enough to rule out most classes in my code base where I might have a good reason to have a default public constructor.

In some projects, this might be a step too far because the utility classes could have private methods, so I might choose to look for the presence of 'any' public static methods, rather than 'all'.

       - child:
           field:
              anyOf:
                - modifier: "static"
                - modifier: "public"


Hints

Sensei is not designed to replace a Static Analysis tool. Sensei can augment a Static Analysis tool for common issues associated with your coding process, or technology. By replicating enough of the matching to highlight an issue, and supporting the development process by generating the QuickFix code.

What I'm trying to do is create a simple enough recipe that includes all the situations I need it, but filter it so that it doesn't get suggested in every class.

When working on recipes I try to de-risk them, in this case, I wasn't sure if I could create the private constructor so I created the QuickFix first. Then refactored the search conditions to make them more specific.

Sometimes when working on recipes I'm not sure how to perform the search, so I work on that first.

I find recipes easier to create when I build them incrementally, switching between refactoring of the QuickFix and the search.

---


You can install Sensei from within IntelliJ using "Preferences \ Plugins" (Mac) or "Settings \ Plugins" (Windows)  then just search for "sensei secure code"


The source code and recipes for this can be found in the `sensei-blog-examples` repository in the Secure Code Warrior GitHub account, in the `pojoexamples` module.

https://github.com/securecodewarrior/sensei-blog-examples

‍

标语

在 AI 驱动的开发成果发布前实现有效治理

评估 AI 辅助风险,在提交代码时强制执行安全编码策略,并加速整个软件开发生命周期的安全交付。

预约演示
资源库

探索更多博客

获取有关安全编码、人工智能治理和软件风险管理的专家内容。

浏览全部
案例研究
筛选标签
这是 div 块中的一段文本。

统一的安全文化:Sage 如何打造其安全倡导者项目

了解 Sage 如何通过灵活且以关系为导向的方法提升安全性,成功培养了 200 多名安全倡导者,并实现了显著的风险降低。

了解更多
浏览器窗口图标,背景为蓝色渐变,中间有粗体字母 KVK,下方有两条蓝色水平线。
案例研究
筛选标签
这是 div 块中的一段文本。

荷兰商会:大规模开发者驱动的安全实践

荷兰商会(Kamer van Koophandel)分享了他们如何通过基于角色的认证、信任评分基准测试以及共享安全责任的文化,将安全编码融入日常开发工作。

了解更多
Insurance Australia Group 标志,显示在网页浏览器窗口设计中,包含蓝色和灰色的文字与图标。
案例研究
筛选标签
这是 div 块中的一段文本。

“代码游戏”如何引领 IAG 集团迈向更安全的编码未来

IAG 集团是亚太地区多家领先保险公司的幕后推手,为数百万客户提供保险承保服务,年保费规模约达 114 亿澳元。

了解更多
博客
筛选标签
这是 div 块中的一段文本。

赋能要素 10:将培训与职业发展挂钩

只有当培训具有切实的职业价值时,它才会受到重视。赋能要素 10 展示了如何通过将安全编码与绩效目标挂钩来推动持久的参与。

了解更多
博客
筛选标签
这是 div 块中的一段文本。

如果模型只是安全保障中最小的一环,那么究竟是什么让 AI 生成的代码更安全?

今年早些时候,我们发布了 SCW AI 信任指数,并与皇家墨尔本理工大学(RMIT University)合作,对来自 OpenAI、Anthropic、Google 等机构的 16 款主流 AI 模型进行了评估,共生成 1760 个代码库,平均每个代码库检测出 15 个安全漏洞。基于我们首创的评估方法,这些数据最终证实:安全风险在特定模型和技术栈框架下是具有可预测性的。 

‍

了解更多
博客
筛选标签
这是 div 块中的一段文本。

直接在人工智能工具中运行您的安全编码程序

通过任何兼容 MCP 的 AI 工具,即可追踪培训进度、起草任务(Quests)并生成 SCW 安全编码计划报告。所有 SCW 方案均已包含此功能。

了解更多

在产品发布前确保 AI 驱动的开发安全

洞察开发风险,执行安全策略,并在整个软件开发生命周期中预防漏洞。

预约演示
未找到相关项目。