False positive: java/field-masks-super-field triggered on Kotlin sealed class with open val constructor parameters

未关闭
#22,361 5 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
64/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
java, kotlin
领域
security

调研方向

从 Kotlin 复现和 java/field-masks-super-field 查询开始,然后将其处理方式与关于 Kotlin Live Literals 的 PR #10859 进行比较。确认 Java extractor 如何表示 sealed class、object subclasses 和 open val parameters。完成的标准是:针对这种模式抑制 alert,同时继续报告真正的 field shadowing。

由索引模型根据 Issue 内容生成。

描述

Summary

The rule java/field-masks-super-field is producing a false positive on a Kotlin sealed class that uses open val constructor parameters. No actual field shadowing exists in the source code.

CodeQL Version

GitHub Advanced Security (cloud) - latest on github.dev

Language

Kotlin (analysed via Java extractor)

Minimal Reproduction

sealed class ImageType(open val width: Int, open val height: Int) {
    object Portrait  : ImageType(78, 98)
    object Square    : ImageType(78, 78)
    object PortraitLarge : ImageType(163, 205)
}

What CodeQL Reports

"This field shadows another field called width/height in a superclass."

Rule ID: java/field-masks-super-field

Why This Is a False Positive

  • No subclass redeclares width or height in its body
  • Every object subclass simply passes values via the constructor to the parent
  • There is no Java-style field shadowing at the source level
  • The alert appears to be triggered by synthetic bridge method scaffolding
    that Kotlin generates for open val properties, which the Java extractor
    misidentifies as a field declaration in the subclass

Related

This appears to be in the same category as PR #10859 which excluded Kotlin Live Literals from this same rule:
https://github.com/github/codeql/pull/10859

That PR acknowledged that Kotlin-generated bytecode patterns can trigger false positives in java/field-masks-super-field. The sealed class + object + open val pattern appears to be another such case.

Workaround

Removing open from the constructor parameters eliminates the alert and is safe when no subclass actually overrides the properties. However
this forces unnecessary code changes to work around a false positive.

Expected Behaviour

The rule should not fire when no subclass explicitly redeclares the field in its body - consistent with how PR #10859 handled Live Literals.

主要语言
CodeQL
星标
10.1k
派生
2.1k
平均合并
2 天 11 小时
30 天内合并 PR
129

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

github/codeql 的其他 Issue

查看 github/codeql 的全部 Issue

相似的 Issue

更多 Security Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。