Skip to content

Conversation

@Galoretka
Copy link

@Galoretka Galoretka commented Aug 9, 2025

  • Ensure END marker is searched relative to BEGIN before slicing
  • Validate positions (no +len on -1) to avoid empty/incorrect slices when END is missing
  • Applied to read_certificate_content and read_private_key_content

Summary by CodeRabbit

  • Refactor
    • Improved the logic for extracting certificate and private key content, ensuring more reliable detection of PEM boundaries. No changes to user-facing features.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Aug 9, 2025

Walkthrough

The extraction logic in two functions of addCertificatesInTs.py was refactored to use explicit variables for PEM begin and end markers. The search for the end marker now starts after the begin marker, clarifying boundary detection. No changes were made to function signatures or exported entities.

Changes

Cohort / File(s) Change Summary
PEM Extraction Logic Refactor
common/src/scripts/addCertificatesInTs.py
Refactored certificate and private key extraction logic to use explicit begin/end marker variables; improved boundary detection; added final newline. No changes to function signatures or exports.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

In code where certificates hide and seek,
Markers now guide what we extract and keep.
Begin and end, so clearly defined,
No more confusion—just logic aligned!
A newline concludes this tidy affair,
For scripts that handle secrets with care.
🔐✨

Note

🔌 MCP (Model Context Protocol) integration is now available in Early Access!

Pro users can now connect to remote MCP servers under the Integrations page to get reviews and chat conversations that understand additional development context.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
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.

Actionable comments posted: 0

🧹 Nitpick comments (2)
common/src/scripts/addCertificatesInTs.py (2)

10-20: DRY the PEM extraction and keep cleaning consistent even on fallback

Both functions duplicate the same extraction pattern. Consider a small helper to:

  • Find [BEGIN..END] chunk relative to BEGIN (as you do now).
  • If either marker is missing, default pem_chunk = full_text (no early return).
  • Always pass pem_chunk through the same cleaning pipeline for consistent output, matching the docstring promise to remove extra lines.

Example helper and usage:

def _extract_pem_chunk(full_text: str, begin_marker: str, end_marker: str) -> str:
    start_idx = full_text.find(begin_marker)
    if start_idx == -1:
        return full_text  # fallback: no early return in callers; let them clean

    end_pos = full_text.find(end_marker, start_idx)
    if end_pos == -1:
        return full_text  # fallback

    return full_text[start_idx:end_pos + len(end_marker)]

Use it in both functions:

# In read_certificate_content
begin_marker = "-----BEGIN CERTIFICATE-----"
end_marker = "-----END CERTIFICATE-----"
pem_chunk = _extract_pem_chunk(full_text, begin_marker, end_marker)
# then run the existing cleaned_lines = [] logic on pem_chunk, always

# In read_private_key_content
begin_marker = "-----BEGIN PRIVATE KEY-----"
end_marker = "-----END PRIVATE KEY-----"
pem_chunk = _extract_pem_chunk(full_text, begin_marker, end_marker)
# then run the existing cleaned_lines = [] logic on pem_chunk, always

Also applies to: 37-47


37-47: Broaden private key marker support (RSA/EC/ENCRYPTED) for real-world robustness

Many PEMs use RSA PRIVATE KEY, EC PRIVATE KEY, or ENCRYPTED PRIVATE KEY. Consider trying a small set of known BEGIN/END pairs and picking the first present to avoid returning the raw text when a valid key exists.

Example:

key_types = [
    ("-----BEGIN PRIVATE KEY-----", "-----END PRIVATE KEY-----"),
    ("-----BEGIN RSA PRIVATE KEY-----", "-----END RSA PRIVATE KEY-----"),
    ("-----BEGIN EC PRIVATE KEY-----", "-----END EC PRIVATE KEY-----"),
    ("-----BEGIN ENCRYPTED PRIVATE KEY-----", "-----END ENCRYPTED PRIVATE KEY-----"),
]

for b, e in key_types:
    start_idx = full_text.find(b)
    if start_idx != -1:
        end_pos = full_text.find(e, start_idx)
        if end_pos != -1:
            pem_chunk = full_text[start_idx:end_pos + len(e)]
            break
else:
    pem_chunk = full_text  # fallback
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5356b8b and edcb64e.

📒 Files selected for processing (1)
  • common/src/scripts/addCertificatesInTs.py (3 hunks)
🔇 Additional comments (3)
common/src/scripts/addCertificatesInTs.py (3)

10-20: Robust END marker search and index validation — good fix

Searching the END marker relative to the BEGIN marker and guarding for -1 eliminates the prior slice bug and prevents cross-block captures. This is the correct and safer approach.


37-47: Mirror fix for private keys — looks correct

Same benefits here: relative search and index validation fix the incorrect-slice edge cases for key files too.


114-114: Guarded entrypoint is correct

The script runs only when invoked directly; safe for imports and CI.

@remicolin
Copy link
Collaborator

hey, could you give more context on this PR?
do you have sample of certificates that are not working with the current implementation?

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.

2 participants