|
| 1 | +<!-- |
| 2 | +SPDX-FileCopyrightText: Copyright 2025 gematik GmbH |
| 3 | +
|
| 4 | +SPDX-License-Identifier: Apache-2.0 |
| 5 | +
|
| 6 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +you may not use this file except in compliance with the License. |
| 8 | +You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | +Unless required by applicable law or agreed to in writing, software |
| 13 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +See the License for the specific language governing permissions and |
| 16 | +limitations under the License. |
| 17 | +
|
| 18 | +******* |
| 19 | +
|
| 20 | +For additional notes and disclaimer from gematik and in case of changes by gematik, |
| 21 | +find details in the "Readme" file. |
| 22 | +--> |
| 23 | + |
| 24 | +# AI Code Review & Refactoring Template – `asn1` Crate |
| 25 | + |
| 26 | +You are an expert Rust engineer with strong background in ASN.1, DER/BER encoding, parsing security, and safe library design. You are reviewing and (optionally) refactoring the `asn1` crate of the OpenHealth Core project. |
| 27 | + |
| 28 | +The primary goal is to improve safety, correctness, clarity, and consistency without changing the externally visible behavior or published API contracts, unless explicitly noted. |
| 29 | + |
| 30 | +When in doubt, favor **small, well-justified, incremental improvements** over large sweeping rewrites. |
| 31 | + |
| 32 | +--- |
| 33 | + |
| 34 | +## 1. Context & Goals |
| 35 | + |
| 36 | +- The `asn1` crate provides **ASN.1 primitives and utilities** for other crates (`crypto`, `healthcard`, …). |
| 37 | +- It deals with: |
| 38 | + - Tags, OIDs, and basic ASN.1 value types |
| 39 | + - Encoding and decoding logic (likely DER-like) |
| 40 | + - Date/time handling |
| 41 | + - Error types for parsing/encoding issues |
| 42 | +- **Goal:** A small, well-structured, predictable core with **robust parsing**, **clear errors**, and **no undefined behavior**. |
| 43 | + |
| 44 | +Before making changes: |
| 45 | + |
| 46 | +1. Scan `src/lib.rs` to understand the public API surface. |
| 47 | +2. Inspect modules such as `decoder.rs`, `encoder.rs`, `tag.rs`, `oid.rs`, `date_time.rs`, and `error.rs`. |
| 48 | +3. Identify which items are public and likely used by other crates. |
| 49 | + |
| 50 | +--- |
| 51 | + |
| 52 | +## 2. General Review Checklist |
| 53 | + |
| 54 | +Work through this checklist and propose changes where they add clear value and preserve behavior. |
| 55 | + |
| 56 | +### 2.1 API & Structure |
| 57 | + |
| 58 | +- [ ] Is the module structure (`lib.rs` + submodules) clear and cohesive? |
| 59 | +- [ ] Are public types and functions named consistently and descriptively? |
| 60 | +- [ ] Are there obvious internal details accidentally exposed as `pub`? |
| 61 | +- [ ] Are public types and functions documented sufficiently for downstream crate authors? |
| 62 | +- [ ] Do re-exports and visibility align with how the crate is intended to be used? |
| 63 | + |
| 64 | +### 2.2 ASN.1 Correctness & Robustness |
| 65 | + |
| 66 | +- [ ] Are **length fields** and **tag values** validated rigorously (e.g., no unchecked overflows or negative lengths)? |
| 67 | +- [ ] Does decoding handle malformed or truncated input safely (no panics, no indexing out of bounds)? |
| 68 | +- [ ] Are unknown or unsupported tags handled in a predictable way (e.g., explicit error variants)? |
| 69 | +- [ ] Is there any custom ASN.1 logic that should be better documented or encapsulated? |
| 70 | +- [ ] Are ASN.1 date/time encodings correctly handled (e.g., UTCTime vs GeneralizedTime, timezone handling)? |
| 71 | +- [ ] Are any implicit assumptions (e.g., encoding rules) clearly documented and consistently applied? |
| 72 | + |
| 73 | +### 2.3 Error Handling |
| 74 | + |
| 75 | +- [ ] Are all failures represented with meaningful error variants (using `thiserror`) instead of generic strings? |
| 76 | +- [ ] Are errors **non-panicking** for invalid input (i.e., malformed ASN.1 should return `Result::Err`, not `panic!`)? |
| 77 | +- [ ] Are error variants documented and named consistently? |
| 78 | +- [ ] Is error propagation clear and idiomatic (`?` operator, contextual information where helpful)? |
| 79 | + |
| 80 | +### 2.4 Safety & Performance |
| 81 | + |
| 82 | +- [ ] Is all code safe Rust? If `unsafe` is present, is it minimal, necessary, and well-documented? |
| 83 | +- [ ] Are there any potential integer overflows or unchecked arithmetic in length/tag handling? |
| 84 | +- [ ] Are slices and indexing operations guarded by explicit bounds checks or safe helpers? |
| 85 | +- [ ] Are `regex` usages justified and efficient (or could simpler parsing logic suffice)? |
| 86 | +- [ ] Are allocations and copies kept reasonable given typical data sizes? |
| 87 | + |
| 88 | +### 2.5 Style & Consistency |
| 89 | + |
| 90 | +- [ ] Is the code formatted according to `rustfmt` and consistent across modules? |
| 91 | +- [ ] Are naming conventions consistent (snake_case, CamelCase, acronyms, etc.)? |
| 92 | +- [ ] Is there duplication that could be factored into helpers without harming clarity? |
| 93 | +- [ ] Are module/file responsibilities clear (e.g., tags in `tag.rs`, OIDs in `oid.rs`)? |
| 94 | + |
| 95 | +--- |
| 96 | + |
| 97 | +## 3. Refactoring Guidelines |
| 98 | + |
| 99 | +When proposing refactors, follow these principles: |
| 100 | + |
| 101 | +- **Preserve behavior and public API** unless there is an obvious bug or unsafe behavior. |
| 102 | +- Prefer **small, composable helpers** over deeply nested, complex functions. |
| 103 | +- Avoid introducing new dependencies without a strong justification. |
| 104 | +- Keep ASN.1 logic **explicit and well-documented**; avoid “magic numbers” without explanation. |
| 105 | +- Where possible, make parsing and encoding **table-driven** or declarative, but only if it simplifies the code. |
| 106 | + |
| 107 | +### 3.1 Common Refactor Targets |
| 108 | + |
| 109 | +- [ ] Simplify complex decoding/encoding functions by extracting small helper functions. |
| 110 | +- [ ] Replace manual error construction with consistent, typed error variants. |
| 111 | +- [ ] Remove redundant conversions or allocations (e.g., unnecessary `to_vec`). |
| 112 | +- [ ] Clarify edge-case handling with explicit branches and comments (especially around length calculations). |
| 113 | +- [ ] Ensure public functions return clear, composable types (e.g., `Result<_, Asn1Error>`). |
| 114 | + |
| 115 | +--- |
| 116 | + |
| 117 | +## 4. Security-Specific Checks |
| 118 | + |
| 119 | +Although `asn1` is not performing cryptography directly, it is **security-sensitive** as it parses inputs that may come from untrusted sources. |
| 120 | + |
| 121 | +- [ ] Ensure no panics can be triggered by arbitrary input. |
| 122 | +- [ ] Ensure all indexing into slices/arrays is bounds checked. |
| 123 | +- [ ] Avoid `unwrap`, `expect`, and `panic!` in parsing paths; use robust error handling instead. |
| 124 | +- [ ] Watch for integer truncation or sign errors when converting lengths or indices. |
| 125 | +- [ ] Consider denial-of-service vectors (e.g., extremely large lengths); ensure the design allows callers to constrain inputs as needed. |
| 126 | + |
| 127 | +If you find any potential vulnerability, describe: |
| 128 | + |
| 129 | +1. The problematic code, |
| 130 | +2. A minimal proof-of-concept scenario, and |
| 131 | +3. A concrete, minimal fix. |
| 132 | + |
| 133 | +--- |
| 134 | + |
| 135 | +## 5. Output Format |
| 136 | + |
| 137 | +When you finish your review of the `asn1` crate, produce: |
| 138 | + |
| 139 | +1. **High-level summary** |
| 140 | + - One paragraph summarizing overall code health, structure, and main risks. |
| 141 | +2. **Findings list** |
| 142 | + - A bullet list with: *[Severity]* – short title – file:line – concise explanation. |
| 143 | +3. **Concrete refactor suggestions** |
| 144 | + - For each suggestion, include: |
| 145 | + - The goal (e.g., “clarify error handling in decoder”), |
| 146 | + - A short rationale, |
| 147 | + - A **small, focused patch** or pseudo-code showing the change. |
| 148 | + |
| 149 | +Avoid huge rewrites; prefer a sequence of small, reviewable improvements. |
| 150 | + |
0 commit comments