Skip to content

Avoid app crash with no cookie value#49

Merged
waisingyiu merged 3 commits intomainfrom
wsy/fix-cookie-missing
Jul 22, 2025
Merged

Avoid app crash with no cookie value#49
waisingyiu merged 3 commits intomainfrom
wsy/fix-cookie-missing

Conversation

@waisingyiu
Copy link
Contributor

What does this change?

We noticed that the quiz builder crashed when there were no cookie value. The quiz builder passed in an undefined value as the cookie and the pan-domain-node library threw a TypeError when it attempted to parse it with cookie.parse(requestCookies).

The library expects a string as the cookie value in its verify method, but the quiz builder was built in Javascript and it bypassed the typechecking.

The pull request changes the function signature of PanDomainAuthentication.verify to accommodate an undefined value in the cookie value and return failed status with no-cookie as the reason. It is the same response it returns when the specific cookie gutoolsAuth-assym is missing.

How to test

Added an unit test on this case.

I've also tested the quiz builder locally which pulls the local pan-domain-node. I could open the quiz builder successfully after I manually deleted all its cookies through the browser's developer tool.

How can we measure success?

Avoid app crash due to missing cookie values.

Have we considered potential risks?

Should be minimal.

@waisingyiu waisingyiu requested a review from a team as a code owner July 21, 2025 08:47
@changeset-bot
Copy link

changeset-bot bot commented Jul 21, 2025

🦋 Changeset detected

Latest commit: 15775a7

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@guardian/pan-domain-node Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

@SHession SHession left a comment

Choose a reason for hiding this comment

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

Some small comments to consider, hopefully not too pedantic

@@ -0,0 +1,5 @@
---
"@guardian/pan-domain-node": patch
Copy link
Contributor

Choose a reason for hiding this comment

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

For Semver this should technically be a minor change:

MINOR version when you add functionality in a backward compatible manner

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agree!

jest.setSystemTime(100);

const panda = new PanDomainAuthentication('rightcookiename', 'region', 'bucket', 'keyfile', guardianValidation);
// There is a valid Panda cookie in here, but it's under the wrong name
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not quite sure what this comment means, I think it can just be removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry for this irrelevant comment - I've removed it.

src/panda.ts Outdated
Comment on lines 151 to 156
if (!requestCookies) {
return {
success: false,
reason: 'no-cookie'
};
}
Copy link
Contributor

Choose a reason for hiding this comment

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

We could avoid this early return by passing an empty string to cookie.parse i.e. cookie.parse(requestCookies || ""). This way we could avoid any duplicate logic from the verify function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea! I've made the change and I think using nullish coalescing operator (??) here might make the code clearer.

@waisingyiu waisingyiu requested a review from SHession July 21, 2025 17:07
@waisingyiu waisingyiu merged commit 8b09a54 into main Jul 22, 2025
1 check passed
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

Comments