-
Notifications
You must be signed in to change notification settings - Fork 349
✨ feat: add checksum validation #672
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
Open
noetica-cloud
wants to merge
17
commits into
amacneil:main
Choose a base branch
from
noetica-cloud:feat/checksum
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
143754d
✨ feat: add checksum validation
b641e43
Add retro-compatibility
47da91f
Update README file
1961335
Make it platform resilient
c8ad9d7
Fix path definition for windows platform
c709768
Strip BOMs and replace stabilize checksum computation accross platforms
d50efe7
Replace clickhouse use of system database
ffa7420
Fix sonar issues on dockerfile
17c4a6c
Fix postgresql driver tests to ignore newer PostgreSQL toolchains' en…
2800ac6
Move lenient as the default checksum mode
f027206
Merge branch 'main' into feat/checksum
noetica-cloud ee627bc
fix issues reported by Cursor Bugbot
0366dae
fix format
41c7fec
Fix sync-go-toolchain workflow for fork PRs
bf22061
fix issues reported by Cursor Bugbot
c58a3a9
Fix lack of ON CLUSTER clause in AddChecksumColumn for clickhouse
d8b8b6a
Merge branch 'main' into feat/checksum
noetica-cloud File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| package dbmate | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "crypto/sha256" | ||
| "encoding/hex" | ||
| "errors" | ||
| "strings" | ||
| ) | ||
|
|
||
| type ChecksumMode int | ||
|
|
||
| const ( | ||
| ChecksumNone ChecksumMode = iota | ||
| ChecksumLenient | ||
| ChecksumStrict | ||
| ) | ||
|
|
||
| var ErrUnknownChecksumMode = errors.New("unknown checksum mode") | ||
|
|
||
| var utf8BOM = []byte{0xEF, 0xBB, 0xBF} | ||
|
|
||
| // ParseChecksumMode parses environment/CLI strings to a ChecksumMode. | ||
| // Accepted strings (case-insensitive): "NONE", "LENIENT", "STRICT". | ||
| func ParseChecksumMode(s string) (ChecksumMode, error) { | ||
| switch strings.ToUpper(strings.TrimSpace(s)) { | ||
| case "NONE": | ||
| return ChecksumNone, nil | ||
| case "", "LENIENT": | ||
| return ChecksumLenient, nil | ||
| case "STRICT": | ||
| return ChecksumStrict, nil | ||
| default: | ||
| return ChecksumLenient, ErrUnknownChecksumMode | ||
| } | ||
| } | ||
|
|
||
| func ModeToString(m ChecksumMode) string { | ||
| switch m { | ||
| case ChecksumNone: | ||
| return "NONE" | ||
| case ChecksumLenient: | ||
| return "LENIENT" | ||
| case ChecksumStrict: | ||
| return "STRICT" | ||
| default: | ||
| return "UNKNOWN" | ||
| } | ||
| } | ||
|
|
||
| // ComputeChecksum computes a SHA256 checksum of the given bytes after | ||
| // canonicalizing text. We strip a leading UTF-8 BOM (if present) and normalize | ||
| // CRLF -> LF so checksums are stable across platforms. | ||
| func ComputeChecksum(b []byte) string { | ||
| // strip UTF-8 BOM if present | ||
| b = bytes.TrimPrefix(b, utf8BOM) | ||
|
|
||
| // normalize CRLF -> LF | ||
| b = bytes.ReplaceAll(b, []byte("\r\n"), []byte("\n")) | ||
| sum := sha256.Sum256(b) | ||
| return hex.EncodeToString(sum[:]) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package dbmate | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestComputeChecksum_LFvsCRLF(t *testing.T) { | ||
| a := []byte("-- migrate:up\nCREATE TABLE foo (id INTEGER);\n") | ||
| b := []byte("-- migrate:up\r\nCREATE TABLE foo (id INTEGER);\r\n") | ||
| ha := ComputeChecksum(a) | ||
| hb := ComputeChecksum(b) | ||
| if ha != hb { | ||
| t.Fatalf("checksums differ for LF vs CRLF: %s != %s", ha, hb) | ||
| } | ||
| } | ||
|
|
||
| func TestComputeChecksum_BOMStripped(t *testing.T) { | ||
| bom := []byte{0xEF, 0xBB, 0xBF} | ||
| body := []byte("-- migrate:up\nCREATE TABLE foo (id INTEGER);\n") | ||
| withBOM := append(bom, body...) | ||
| h1 := ComputeChecksum(body) | ||
| h2 := ComputeChecksum(withBOM) | ||
| if h1 != h2 { | ||
| t.Fatalf("checksums differ with/without BOM: %s != %s", h1, h2) | ||
| } | ||
| } | ||
|
|
||
| func TestComputeChecksum_CRLFandBOM(t *testing.T) { | ||
| bom := []byte{0xEF, 0xBB, 0xBF} | ||
| lf := []byte("-- migrate:up\nCREATE TABLE foo (id INTEGER);\n") | ||
| crlf := bytes.ReplaceAll(lf, []byte("\n"), []byte("\r\n")) | ||
| hlf := ComputeChecksum(lf) | ||
| hcrlf := ComputeChecksum(crlf) | ||
| if hlf != hcrlf { | ||
| t.Fatalf("checksums differ for CRLF vs LF: %s != %s", hlf, hcrlf) | ||
| } | ||
| // BOM CRLF | ||
| withBOM := append(bom, crlf...) | ||
| h3 := ComputeChecksum(withBOM) | ||
| if h3 != hlf { | ||
| t.Fatalf("checksums differ for BOMCRLF vs LF: %s != %s", h3, hlf) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.