Skip to content

feat (Private Key Editing): Added private key format normalization#1080

Merged
GT-610 merged 4 commits intolollipopkit:mainfrom
GT-610:normalize-key
Mar 21, 2026
Merged

feat (Private Key Editing): Added private key format normalization#1080
GT-610 merged 4 commits intolollipopkit:mainfrom
GT-610:normalize-key

Conversation

@GT-610
Copy link
Copy Markdown
Collaborator

@GT-610 GT-610 commented Mar 21, 2026

Resolve #910.

Added the _normalizePrivateKey method to normalize private key formats:

  • Removes whitespace characters from Base64 content
  • Ensures the standard format of 64 characters per line
  • Ensures that the private key ends with a newline character

Summary by CodeRabbit

  • Bug Fixes
    • Private key input is now PEM-aware on save: excess whitespace is removed, the Base64 body is reformatted into consistent 64-character lines, header/footer and any embedded metadata header lines are preserved, and a trailing newline is ensured. Validation prevents accidental alteration of non-PEM content.

Added the _normalizePrivateKey method to normalize private key formats:
- Removes whitespace characters from Base64 content
- Ensures the standard format of 64 characters per line
- Ensures that the private key ends with a newline character
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 21, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 9c49258d-95a2-4d0d-b799-a5fe6aeb3aa9

📥 Commits

Reviewing files that changed from the base of the PR and between a14ca2c and 21d7c37.

📒 Files selected for processing (1)
  • lib/view/page/private_key/edit.dart
🚧 Files skipped from review as they are similar to previous changes (1)
  • lib/view/page/private_key/edit.dart

📝 Walkthrough

Walkthrough

Added PEM-aware private key normalization and a _normalizePrivateKey helper that validates matching -----BEGIN ...----- / -----END ...----- boundaries. For PEMs with metadata header lines (lines containing : not starting with -----), the body and structure are preserved and a trailing newline is ensured. For regular PEMs, the Base64 body is concatenated, whitespace removed, re-wrapped to 64-character lines, and rebuilt with header/footer plus a trailing newline. The save path now trims input, standardizes line separators, and applies _normalizePrivateKey before attempting decryption and saving.

Suggested reviewers

  • lollipopkit
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding private key format normalization functionality to the private key editing feature.
Linked Issues check ✅ Passed The PR implements normalization of private key formats to accept common representations and avoid overly strict validation, directly addressing issue #910's requirement to not over-validate key formats.
Out of Scope Changes check ✅ Passed All changes are scoped to the private key normalization feature requested in issue #910; no unrelated modifications are present.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai bot requested a review from lollipopkit March 21, 2026 11:16
chatgpt-codex-connector[bot]

This comment was marked as resolved.

devin-ai-integration[bot]

This comment was marked as resolved.

coderabbitai[bot]

This comment was marked as resolved.

…rving metadata headers

Properly handles metadata headers (such as Proc-Type and DEK-Info) in encrypted PEM keys and preserves these headers when cleaning up Base64 content. Additionally, optimizes the logic for removing whitespace characters and improves performance by using precompiled regular expressions.
coderabbitai[bot]

This comment was marked as resolved.

… window caching logic

- Remove unused egui::Context parameters from functions related to settings_page
- Add a check for the length of items in the selection window cache to improve cache validity
- Simplify the cache data structure and remove unnecessary online data validation logic
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
lib/view/page/private_key/edit.dart (1)

133-136: Also validate matching PEM type between BEGIN/END.

Boundary format is checked, but mismatched pairs (e.g., BEGIN RSA + END EC) still pass this gate and get rewritten. Consider enforcing same label before mutating input.

♻️ Proposed refinement
-final _pemBeginRegex = RegExp(r'^-----BEGIN [A-Z0-9 ]+-----$');
-final _pemEndRegex = RegExp(r'^-----END [A-Z0-9 ]+-----$');
+final _pemBeginRegex = RegExp(r'^-----BEGIN ([A-Z0-9 ]+)-----$');
+final _pemEndRegex = RegExp(r'^-----END ([A-Z0-9 ]+)-----$');
...
-    if (!_pemBeginRegex.hasMatch(header) || !_pemEndRegex.hasMatch(footer)) {
+    final beginMatch = _pemBeginRegex.firstMatch(header);
+    final endMatch = _pemEndRegex.firstMatch(footer);
+    if (beginMatch == null || endMatch == null) {
+      return key;
+    }
+    if (beginMatch.group(1) != endMatch.group(1)) {
       return key;
     }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@lib/view/page/private_key/edit.dart` around lines 133 - 136, The code
currently only checks PEM boundary formats using _pemBeginRegex and _pemEndRegex
but doesn't ensure the labels match; update the validation so after confirming
both regexes match, extract the label/group from header and footer (using the
capture group(s) in _pemBeginRegex/_pemEndRegex), compare them for equality, and
if they differ return key without mutating; apply this check in the same
function where header/footer are validated before any rewriting (referencing the
header, footer, _pemBeginRegex, _pemEndRegex symbols).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@lib/view/page/private_key/edit.dart`:
- Around line 133-136: The code currently only checks PEM boundary formats using
_pemBeginRegex and _pemEndRegex but doesn't ensure the labels match; update the
validation so after confirming both regexes match, extract the label/group from
header and footer (using the capture group(s) in _pemBeginRegex/_pemEndRegex),
compare them for equality, and if they differ return key without mutating; apply
this check in the same function where header/footer are validated before any
rewriting (referencing the header, footer, _pemBeginRegex, _pemEndRegex
symbols).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 630cae22-bc94-4448-9cb9-2bfe65f8d745

📥 Commits

Reviewing files that changed from the base of the PR and between 92f6a41 and a14ca2c.

📒 Files selected for processing (1)
  • lib/view/page/private_key/edit.dart

coderabbitai[bot]
coderabbitai bot previously approved these changes Mar 21, 2026
… in PEM-format private keys

Added validation for consistency of header and footer tags in PEM-format private keys to ensure that the content following “BEGIN” and “END” is identical
@GT-610 GT-610 merged commit 2f67938 into lollipopkit:main Mar 21, 2026
2 checks passed
@GT-610 GT-610 deleted the normalize-key branch March 21, 2026 15:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

私钥无法保存

1 participant