github / github/codeql

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

Đang mở
#22,361 5 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
CodeQL
Star
10.1k
Fork
2.1k
Merge trung bình
2 ngày 15 giờ
Pull request đã merge (30 ngày)
141

Mô tả

## 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

```kotlin
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.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Hướng nghiên cứu

Bắt đầu với bản tái hiện Kotlin và truy vấn java/field-masks-super-field, sau đó so sánh cách xử lý của nó với PR #10859 về Kotlin Live Literals. Xác nhận sealed class, object subclasses và open val parameters được Java extractor biểu diễn như thế nào. Hoàn tất khi cảnh báo được suppress cho mẫu này, trong khi field shadowing thực sự vẫn được báo cáo.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
java, kotlin
Lĩnh vực
security
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Sôi nổi
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
64/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.