-
Notifications
You must be signed in to change notification settings - Fork 0
Compute and store message sha256 on send #18
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
Closed
Changes from 1 commit
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ import ( | |
|
|
||
| "github.com/gin-gonic/gin" | ||
| "github.com/jackc/pgx/v5" | ||
| "github.com/markmnl/fmsgd/pkg/fmsg" | ||
|
|
||
| "github.com/markmnl/fmsg-webapi/db" | ||
| "github.com/markmnl/fmsg-webapi/middleware" | ||
|
|
@@ -806,7 +807,7 @@ func (h *MessageHandler) Send(c *gin.Context) { | |
| } | ||
|
|
||
| ctx := c.Request.Context() | ||
| existing, _, err := h.fetchMessage(ctx, msgID) | ||
| existing, dataPath, err := h.fetchMessage(ctx, msgID) | ||
| if err != nil { | ||
| if errors.Is(err, pgx.ErrNoRows) { | ||
| c.JSON(http.StatusNotFound, gin.H{"error": "message not found"}) | ||
|
|
@@ -826,8 +827,97 @@ func (h *MessageHandler) Send(c *gin.Context) { | |
| return | ||
| } | ||
|
|
||
| // Fetch psha256 (the parent message's sha256 hash used as the pid wire field). | ||
| var psha256 []byte | ||
| if existing.HasPid { | ||
| _ = h.DB.Pool.QueryRow(ctx, "SELECT psha256 FROM msg WHERE id = $1", msgID).Scan(&psha256) | ||
| } | ||
|
Comment on lines
+830
to
+843
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @copilot apply changes based on this feedback
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in commit 18ccd92:
|
||
|
|
||
| // Fetch attachment metadata needed for hash computation. | ||
| type attachMeta struct { | ||
| flags int16 | ||
| typ string | ||
| filename string | ||
| filesize int32 | ||
| path string | ||
| } | ||
| var attachments []attachMeta | ||
| attRows, attErr := h.DB.Pool.Query(ctx, | ||
| "SELECT flags, type, filename, filesize, filepath FROM msg_attachment WHERE msg_id = $1 ORDER BY position", | ||
| msgID, | ||
| ) | ||
| if attErr == nil { | ||
| for attRows.Next() { | ||
| var a attachMeta | ||
| if scanErr := attRows.Scan(&a.flags, &a.typ, &a.filename, &a.filesize, &a.path); scanErr == nil { | ||
| attachments = append(attachments, a) | ||
| } | ||
| } | ||
| attRows.Close() | ||
| } | ||
|
|
||
| now := float64(time.Now().UnixMicro()) / 1e6 | ||
| if _, err = h.DB.Pool.Exec(ctx, "UPDATE msg SET time_sent = $1 WHERE id = $2", now, msgID); err != nil { | ||
|
|
||
| // Build fmsg.Header for hash computation. | ||
| fromUser, fromDomain := parseAddr(existing.From) | ||
| hdr := fmsg.Header{ | ||
| Version: uint8(existing.Version), | ||
| From: fmsg.Address{User: fromUser, Domain: fromDomain}, | ||
| Timestamp: now, | ||
| Topic: existing.Topic, | ||
| Type: existing.Type, | ||
| Size: uint32(existing.Size), | ||
| Filepath: dataPath, | ||
| } | ||
|
|
||
| if existing.HasPid { | ||
| hdr.Flags |= fmsg.FlagHasPid | ||
| hdr.Pid = psha256 | ||
| } | ||
| if existing.Important { | ||
| hdr.Flags |= fmsg.FlagImportant | ||
| } | ||
| if existing.NoReply { | ||
| hdr.Flags |= fmsg.FlagNoReply | ||
| } | ||
| if existing.HasAddTo { | ||
| hdr.Flags |= fmsg.FlagHasAddTo | ||
| for _, addr := range existing.AddTo { | ||
| u, d := parseAddr(addr) | ||
| hdr.AddTo = append(hdr.AddTo, fmsg.Address{User: u, Domain: d}) | ||
| } | ||
| if existing.AddToFrom != nil { | ||
| u, d := parseAddr(*existing.AddToFrom) | ||
| hdr.AddToFrom = &fmsg.Address{User: u, Domain: d} | ||
| } | ||
| } | ||
|
|
||
| for _, addr := range existing.To { | ||
| u, d := parseAddr(addr) | ||
| hdr.To = append(hdr.To, fmsg.Address{User: u, Domain: d}) | ||
| } | ||
|
|
||
| for _, a := range attachments { | ||
| hdr.Attachments = append(hdr.Attachments, fmsg.AttachmentHeader{ | ||
| Flags: uint8(a.flags), | ||
| Type: a.typ, | ||
| Filename: a.filename, | ||
| Size: uint32(a.filesize), | ||
| Filepath: a.path, | ||
| }) | ||
| } | ||
|
|
||
| msgHash, err := hdr.GetMessageHash() | ||
| if err != nil { | ||
| log.Printf("send message %d: compute hash: %v", msgID, err) | ||
| c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to compute message hash"}) | ||
| return | ||
| } | ||
|
|
||
| if _, err = h.DB.Pool.Exec(ctx, | ||
| "UPDATE msg SET time_sent = $1, sha256 = $2 WHERE id = $3", | ||
| now, msgHash, msgID, | ||
| ); err != nil { | ||
| log.Printf("send message %d: %v", msgID, err) | ||
| c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to send message"}) | ||
| return | ||
|
|
@@ -995,15 +1085,15 @@ func (h *MessageHandler) AddRecipients(c *gin.Context) { | |
| // after performing their own authorization checks. | ||
| func (h *MessageHandler) fetchMessage(ctx context.Context, msgID int64) (*models.Message, string, error) { | ||
| row := h.DB.Pool.QueryRow(ctx, | ||
| `SELECT version, pid, no_reply, is_important, is_deflate, time_sent, from_addr, topic, type, size, filepath FROM msg WHERE id = $1`, | ||
| `SELECT version, pid, no_reply, is_important, is_deflate, time_sent, from_addr, add_to_from, topic, type, size, filepath FROM msg WHERE id = $1`, | ||
| msgID, | ||
| ) | ||
|
|
||
| msg := &models.Message{} | ||
| var pid *int64 | ||
| var timeSent *float64 | ||
| var dataPath string | ||
| if err := row.Scan(&msg.Version, &pid, &msg.NoReply, &msg.Important, &msg.Deflate, &timeSent, &msg.From, &msg.Topic, &msg.Type, &msg.Size, &dataPath); err != nil { | ||
| if err := row.Scan(&msg.Version, &pid, &msg.NoReply, &msg.Important, &msg.Deflate, &timeSent, &msg.From, &msg.AddToFrom, &msg.Topic, &msg.Type, &msg.Size, &dataPath); err != nil { | ||
| return nil, "", err | ||
| } | ||
| msg.PID = pid | ||
|
|
||
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.