-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix template signing singnature issue #5869
Conversation
WalkthroughThe pull request involves modifications to the Changes
Assessment against linked issues
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this 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
🧹 Outside diff range and nitpick comments (1)
.gitignore (1)
14-30
: Consider the implications of restricting binary ignores to root levelThe change from
**/pattern
to/pattern
means binaries will only be ignored in the root directory. This could lead to:
- Unintended tracking of binaries in subdirectories
- Potential repository bloat if large binaries are committed
- Security concerns if sensitive binaries exist in subdirectories
Consider keeping the
**/
prefix for critical binaries likenuclei
,debug-*
, andnuclei-stats*
.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (1)
.gitignore
(1 hunks)
🔇 Additional comments (2)
.gitignore (2)
14-30
: Verify binary locations in the codebase
Let's verify if any of these binaries exist in subdirectories that would now be tracked.
✅ Verification successful
The .gitignore entries are correctly placed and necessary
The search results show that these entries in .gitignore are for build artifacts and binaries that would be generated in the root directory:
- Source files exist in subdirectories (e.g.,
cmd/docgen/docgen.go
,cmd/integration-test/integration-test.go
) but the .gitignore entries are for their compiled binaries in the root - Main entry points found in
cmd/
and other directories are source files, not the compiled binaries - No binary files were found in subdirectories that would be incorrectly tracked
- The patterns match the standard Go build output location, where binaries are placed in the root directory by default
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Check for binary files in subdirectories that would now be tracked
# Look for potential binary files in subdirectories
for pattern in bindgen debug- docgen functional-test fuzzplayground integration-test jsdocgen main memogen nuclei nuclei-stats nuclei_dev nuclei_main scan-charts scrapefunc scrapefuncs tsgen; do
echo "Checking for '$pattern' in subdirectories:"
fd -t f "$pattern" --min-depth 2
done
# Check if any of these are actually needed for template signing
rg -i "template.*sign.*binary" --type go -A 5
Length of output: 6462
5-6
: Verify if .exe files are needed for template signing
While the **/*.DS_Store
pattern is a good improvement, the **/*.exe
pattern might be too restrictive if any executables are needed for template signing. Please verify if this change could impact the template signing process.
✅ Verification successful
Based on the extensive search results, I can now provide a definitive response about the .exe files and template signing concern:
The .gitignore change is safe and won't impact template signing
The template signing functionality in Nuclei is based on cryptographic signatures stored within the template files themselves, not on executable files. The signing process:
- Uses ECDSA signatures stored at the end of template files
- Is verified using public key certificates
- Does not rely on any .exe files for verification
The change from *.exe
to **/*.exe
in .gitignore is safe because:
- Template signing is implemented through code signatures within YAML template files
- The signing process uses cryptographic keys and certificates, not executables
- No .exe files are required for the template signing or verification process
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Check if there are any .exe files in the repository that might be needed
# for template signing
# Search for references to .exe files in the codebase
rg -i "\.exe" --type go -g '!.gitignore' -A 5
# Look for any template signing related files
rg -i "template.*sign" --type go -A 5
Length of output: 70358
Script:
#!/bin/bash
# Look for template signing related files and functionality
rg -i "template.*sign|sign.*template" --type go -A 5
# Look for any signing related configuration or documentation
fd -e md -e txt "sign" -X cat {}
Length of output: 72167
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Outside diff range and nitpick comments (1)
cmd/tools/signer/main.go (1)
87-93
: Refactor repeated code into a helper functionThe code for reading the template file, extracting the signature and content, and computing the hash is repeated. Refactoring this into a helper function enhances maintainability and reduces duplication.
Proposed refactoring:
+func readTemplateSignatureAndHash(template string) (string, [32]byte, error) { + bin, err := os.ReadFile(template) + if err != nil { + return "", [32]byte{}, err + } + sig, content := signer.ExtractSignatureAndContent(bin) + content = bytes.ReplaceAll(content, []byte("\r\n"), []byte("\n")) + hash := sha256.Sum256(content) + return sig, hash, nil +} // Before signing -sig, content := signer.ExtractSignatureAndContent(bin) -hash := sha256.Sum256(content) +sig, hash, err := readTemplateSignatureAndHash(template) +if err != nil { + gologger.Fatal().Msgf("failed to read template file %s: %s", template, err) +} // After signing -bin2, err := os.ReadFile(template) -if err != nil { - gologger.Fatal().Msgf("failed to read signed template file %s: %s", template, err) -} -sig2, content2 := signer.ExtractSignatureAndContent(bin2) -hash2 := sha256.Sum256(content2) +sig2, hash2, err := readTemplateSignatureAndHash(template) +if err != nil { + gologger.Fatal().Msgf("failed to read signed template file %s: %s", template, err) +}
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (3)
Makefile
(1 hunks)cmd/tools/signer/main.go
(1 hunks)pkg/templates/compile.go
(1 hunks)
🔇 Additional comments (2)
Makefile (1)
45-48
: LGTM!
The new template-signer
build target is correctly defined and follows the existing pattern.
pkg/templates/compile.go (1)
454-456
: LGTM!
Adding verbose logging for template verification improves traceability and debugging.
There was a problem hiding this 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
🧹 Outside diff range and nitpick comments (4)
pkg/templates/signer/tmpl_signer.go (4)
20-20
: Consider using yaml.v3 instead of yaml.v2While yaml.v2 works, yaml.v3 provides better performance, security features, and handles line endings more consistently. However, this change should align with the project's overall yaml package usage.
38-48
: Add error logging for YAML parsing failuresWhile the fallback to original content is correct, logging YAML parsing errors would help with debugging template issues.
var normalized interface{} if err := yaml.Unmarshal(content, &normalized); err != nil { + gologger.Debug().Msgf("Failed to normalize template content: %v", err) return signature, content } normalizedBytes, err := yaml.Marshal(normalized) if err != nil { + gologger.Debug().Msgf("Failed to marshal normalized template content: %v", err) return signature, content }
40-40
: Consider using a more specific type for YAML unmarshallingUsing
interface{}
for YAML unmarshalling is very broad. Consider using a more specific type that represents your template structure to catch potential issues earlier.
38-48
: Add test cases for cross-platform signature verificationThe changes aim to fix cross-platform signature issues. Please add test cases that verify:
- Templates signed on Linux work on Windows
- Templates signed on Windows work on Linux
- Templates with various line endings (CRLF, LF) are handled correctly
Would you like me to help create these test cases?
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (1)
pkg/templates/signer/tmpl_signer.go
(2 hunks)
🔇 Additional comments (2)
pkg/templates/signer/tmpl_signer.go (2)
38-48
: LGTM - Clean solution for cross-platform signature normalization
The YAML-based normalization approach is a robust solution for handling cross-platform line ending differences. It's more reliable than string replacement and handles edge cases well.
38-48
: Verify the impact on template formatting
The YAML normalization might affect templates that rely on specific formatting or whitespace. Please ensure this change doesn't break any existing templates, especially those with:
- Multi-line strings
- Custom indentation
- Specific line ending requirements in payloads
✅ Verification successful
Based on the test files and implementation review, I can now provide the final response:
YAML normalization is safe for template formatting
The implementation is safe because:
- The normalization is only used for signature verification, not template execution
- The test cases demonstrate that the original content is returned if YAML parsing fails
- The code includes a specific comment acknowledging payload preservation: "if we use strings.ReplaceAll(content,"\r\n","\n"), it most likely will break some payload in logic"
The test suite verifies:
- Simple templates
- Templates with imports
- Templates with code protocols
- Various tampering scenarios
All while preserving the original content when YAML parsing fails, which acts as a safety mechanism for any non-standard YAML content.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Description: Check for templates that might be affected by YAML normalization
# Look for templates with multi-line strings or specific formatting
rg -l '(?m)^\s*[^#\n-][^:]+:\s*\|\s*$'
# Look for templates with potential whitespace-sensitive payloads
rg -l '(?m)^\s*payload:\s*\|\s*$'
Length of output: 2698
Script:
#!/bin/bash
# Let's check for actual template files with multi-line content and analyze their structure
# First, let's look at template files specifically
fd -e yaml -e yml . pkg/templates integration_tests/templates | xargs rg -l '(?m)^\s*[^#\n-][^:]+:\s*\|\s*$'
# Then, let's examine a few examples of templates with multi-line content to understand their structure
fd -e yaml -e yml . pkg/templates integration_tests/templates | xargs rg -A 5 '(?m)^\s*[^#\n-][^:]+:\s*\|\s*$'
# Also check for any documentation or tests mentioning template formatting requirements
rg -i "template.*format|formatting.*template" -A 3 pkg/templates
Length of output: 1887
Script:
#!/bin/bash
# Let's examine the actual implementation and usage of the YAML normalization
# Check the signer package for any tests or documentation about formatting preservation
rg -A 5 "yaml\." pkg/templates/signer/
# Look for any existing test files that verify template signing behavior
fd ".*test.*\.go" pkg/templates/signer/
# Check if there are any existing test templates used for signing verification
fd -e yaml -e yml . pkg/templates/signer/testdata
Length of output: 1220
Script:
#!/bin/bash
# Let's examine the test files to understand how YAML normalization is tested
# Check the test files content
cat pkg/templates/signer/tmpl_signer_test.go
cat pkg/templates/signer/handler_test.go
# Look for any test cases that specifically deal with formatting
rg -A 10 "TestSign|TestVerify" pkg/templates/signer/
Length of output: 5151
Proposed changes
Context
New Changes
Checklist
Summary by CodeRabbit
.gitignore
patterns to broaden the scope for ignoring certain files in subdirectories.template-signer
component in the Makefile.