Skip to content
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

snapshot: read meta.json correctly. #5193

Merged
merged 2 commits into from
Jan 8, 2019
Merged

snapshot: read meta.json correctly. #5193

merged 2 commits into from
Jan 8, 2019

Conversation

hanshasselberg
Copy link
Member

Fixes #4452.

I basically did what @maf23 suggested, and made sure we read the file entirely. Since we don't need the json decoder in this case I read the file first and unmarshal afterwards. Apparently ReadAll reads everything in contrast to Decode. I added a test with the file provided by @maf23 which couldn't be verified before even though it is a valid snapshot. This will prevent us from changing the decoding in a way that runs into that same problem again.
I do think this is a very strange bug btw.

@hanshasselberg hanshasselberg requested a review from a team January 6, 2019 00:18
@@ -193,8 +194,11 @@ func read(in io.Reader, metadata *raft.SnapshotMeta, snap io.Writer) error {

switch hdr.Name {
case "meta.json":
dec := json.NewDecoder(io.TeeReader(archive, metaHash))
if err := dec.Decode(&metadata); err != nil {
buf, err := ioutil.ReadAll(io.TeeReader(archive, metaHash))
Copy link
Member

Choose a reason for hiding this comment

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

I think it's worth commenting on why this is needed. The root cause seems to be something like:

// json.Decode will stop reading as soon as the initial JSON element (the object) is closed 
// (i.e. at the closing brace) which means the final newline, if any, is not read and so is not 
// delivered to `metaHash`. This means that the hash calculated is not the same as the
// one in SHA256SUMS which was calculated over the raw bytes in the file. By explicitly 
// reading the whole thing first we ensure that we calculate the hash over the exact
// same bytes regardless of whitespace.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point, I will add a comment!

@hanshasselberg hanshasselberg merged commit 0fc1c20 into master Jan 8, 2019
@hanshasselberg hanshasselberg deleted the verify_snapshot branch January 8, 2019 16:06
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