Skip to content

remove headers if recipients = 1; copy protected headers to headers f…#83

Open
ilya-korotya wants to merge 2 commits into
mainfrom
fix/remove-header-from-jwe
Open

remove headers if recipients = 1; copy protected headers to headers f…#83
ilya-korotya wants to merge 2 commits into
mainfrom
fix/remove-header-from-jwe

Conversation

@ilya-korotya

Copy link
Copy Markdown
Contributor

…or jwe flatted message

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull Request Overview

This PR modifies the AnoncryptPacker to handle JWE (JSON Web Encryption) messages with single recipients by removing headers and copying protected headers to headers for flattened JWE format.

  • Removes headers from JWE messages when there's only one recipient
  • Adds logic to copy protected headers to headers field for flattened JWE messages during unpacking
  • Updates tests to use proper constructor and verification methods

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
packers/anoncrypt.go Implements header removal for single-recipient messages and protected header copying logic
packers/anoncrypt_test.go Updates test to use constructor, changes verification approach, and comments out second recipient

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread packers/anoncrypt.go
Comment on lines +168 to +178
if len(recipients) == 1 {
j := map[string]interface{}{}
if err := json.Unmarshal(ret, &j); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal jwe message")
}
delete(j, "header")
ret, err = json.Marshal(j)
if err != nil {
return nil, errors.Wrap(err, "failed to marshal jwe message")
}
}

Copilot AI Oct 8, 2025

Copy link

Choose a reason for hiding this comment

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

[nitpick] The variable name j is ambiguous and doesn't convey its purpose. Consider renaming it to jweMap or jweObject to better reflect that it represents the JWE message structure.

Copilot uses AI. Check for mistakes.
Comment thread packers/anoncrypt.go
func (p *AnoncryptPacker) Unpack(envelope []byte) (*iden3comm.BasicMessage, error) {
firstChar := strings.TrimSpace(string(envelope))[0]
if firstChar == '{' {
var j map[string]interface{}

Copilot AI Oct 8, 2025

Copy link

Choose a reason for hiding this comment

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

[nitpick] The variable name j is ambiguous and doesn't convey its purpose. Consider renaming it to jweMap or jweObject to better reflect that it represents the JWE message structure.

Copilot uses AI. Check for mistakes.
Comment thread packers/anoncrypt.go
return nil, errors.New("invalid jwe token: missing protected header")
}

protectedHeaders, err := base64.RawURLEncoding.DecodeString(j["protected"].(string))

Copilot AI Oct 8, 2025

Copy link

Choose a reason for hiding this comment

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

Type assertion j["protected"].(string) could panic if the value is not a string. Add a type check or use the two-value form of type assertion to handle this safely.

Suggested change
protectedHeaders, err := base64.RawURLEncoding.DecodeString(j["protected"].(string))
protectedStr, ok := j["protected"].(string)
if !ok {
return nil, errors.New("invalid jwe token: 'protected' field is not a string")
}
protectedHeaders, err := base64.RawURLEncoding.DecodeString(protectedStr)

Copilot uses AI. Check for mistakes.
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.

3 participants