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

feat: move to whitelist database table from growthbook #864

Merged
merged 4 commits into from
Nov 11, 2024

Conversation

dcshzj
Copy link
Contributor

@dcshzj dcshzj commented Nov 7, 2024

Problem

Our email whitelist lives in Growthbook, which makes adding new users a little difficult as it involves both updating the database and updating the Growthbook feature flag.

Fixes ISOM-1668.

Solution

Breaking Changes

  • Yes - this PR contains breaking changes
    • Add the existing whitelist emails on Growthbook into the database.
  • No - this PR is backwards compatible

Features:

  • Create a new Whitelist table that can take the email and expiry date.
  • Enhance the whitelisting logic to allow for domain whitelists and domain suffix whitelisting.
  • Added tests for the whitelist check logic.

@dcshzj dcshzj requested a review from a team as a code owner November 7, 2024 05:32
Copy link

vercel bot commented Nov 7, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
isomer-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 8, 2024 10:42am

Copy link

linear bot commented Nov 7, 2024

@datadog-opengovsg
Copy link

datadog-opengovsg bot commented Nov 7, 2024

Datadog Report

Branch report: feat/whitelist-db-table
Commit report: 7e60e9f
Test service: isomer-studio

✅ 0 Failed, 175 Passed, 34 Skipped, 36.75s Total Time
➡️ Test Sessions change in coverage: 1 no change

@dcshzj dcshzj requested review from harishv7 and a team November 7, 2024 08:02
// Step 1: Check if the exact email address is whitelisted
const exactMatch = await db
.selectFrom("Whitelist")
.where("email", "=", lowercaseEmail)
Copy link
Contributor

Choose a reason for hiding this comment

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

is this userinput sanitised?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes it is sanitised via the zod schema of the caller procedure, but sure added an additional guard here in 805eebf.

return true
}

// Step 3: Check if the suffix of the email domain is whitelisted
Copy link
Contributor

Choose a reason for hiding this comment

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

what is the use case for suffix match?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is to whitelist .gov.sg, so we can whitelist all domains that end with a .gov.sg in the domain name.

Copy link

@spaceraccoon spaceraccoon Nov 8, 2024

Choose a reason for hiding this comment

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

Bypassed with this POC:

var validator = require('validator');

const email = '"[email protected]@"@example.org'
const lowercaseEmail = email.toLowerCase()
console.log(validator.isEmail(email))

const emailParts = lowercaseEmail.split("@")
console.log(emailParts)

console.log(emailParts.length)
if (emailParts.length !== 2 && !emailParts[1]) {
  console.log('fail')
} else {
  const emailDomain = `@${emailParts[1]}`
  console.log(emailDomain)
}
node index.js
true
[ '"john..d', 'open.gov.sg', '"', 'example.org' ]
4
@open.gov.sg

This is because RFC-compliant emails that pass validators allow a lot of sus stuff.

This is a common issue and we wrote a specific validator in starter-kitty for this, but I'd like to stress-test this a bit more: https://github.com/opengovsg/starter-kitty/tree/develop/packages/validators/src/email.

Copy link
Contributor

@harishv7 harishv7 left a comment

Choose a reason for hiding this comment

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

Let's get a quick review from @spaceraccoon since this touches parts of auth

Copy link
Contributor

@harishv7 harishv7 left a comment

Choose a reason for hiding this comment

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

lgtm, pending approval from @spaceraccoon on security side of things

return true
}

// Step 3: Check if the suffix of the email domain is whitelisted
Copy link

@spaceraccoon spaceraccoon Nov 8, 2024

Choose a reason for hiding this comment

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

Bypassed with this POC:

var validator = require('validator');

const email = '"[email protected]@"@example.org'
const lowercaseEmail = email.toLowerCase()
console.log(validator.isEmail(email))

const emailParts = lowercaseEmail.split("@")
console.log(emailParts)

console.log(emailParts.length)
if (emailParts.length !== 2 && !emailParts[1]) {
  console.log('fail')
} else {
  const emailDomain = `@${emailParts[1]}`
  console.log(emailDomain)
}
node index.js
true
[ '"john..d', 'open.gov.sg', '"', 'example.org' ]
4
@open.gov.sg

This is because RFC-compliant emails that pass validators allow a lot of sus stuff.

This is a common issue and we wrote a specific validator in starter-kitty for this, but I'd like to stress-test this a bit more: https://github.com/opengovsg/starter-kitty/tree/develop/packages/validators/src/email.


// Step 2: Check if the exact email domain is whitelisted
const emailParts = lowercaseEmail.split("@")
if (emailParts.length !== 2 && !emailParts[1]) {

Choose a reason for hiding this comment

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

Suggested change
if (emailParts.length !== 2 && !emailParts[1]) {
if (emailParts.length !== 2 || !emailParts[1]) {

This is the mistake that allowed my bypass, but overall is a bit sus

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Adjusted in b102054! I think doing a .pop() will enforce that we always take the last part, and it's something I see FormSG doing too: https://github.com/opengovsg/FormSG/blob/49e88ce138bdcb36b7ff15ce10332ea60ba5f0d3/src/app/modules/auth/auth.service.ts#L65

Copy link

@spaceraccoon spaceraccoon left a comment

Choose a reason for hiding this comment

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

Noted that email whitelisting is not a long-term solution for authentication due to multiple downstream gotchas, and that SingPass SSO is one of the better solutions.

@dcshzj dcshzj merged commit 9f28c0c into main Nov 11, 2024
17 of 18 checks passed
@dcshzj dcshzj deleted the feat/whitelist-db-table branch November 11, 2024 06:05
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