-
Notifications
You must be signed in to change notification settings - Fork 220
[dev] [Marfuen] mariano/dustin-fix #1904
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
Open
github-actions
wants to merge
1
commit into
main
Choose a base branch
from
mariano/dustin-fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+174
−12
Open
Changes from all commits
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
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 |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| import { env } from '@/env.mjs'; | ||
| import Stripe from 'stripe'; | ||
|
|
||
| // Initialize Stripe client with secret key from environment | ||
| const stripeSecretKey = env.STRIPE_SECRET_KEY; | ||
|
|
||
| if (!stripeSecretKey) { | ||
| console.warn('STRIPE_SECRET_KEY is not set - Stripe auto-approval will be disabled'); | ||
| } | ||
|
|
||
| export const stripe = stripeSecretKey | ||
| ? new Stripe(stripeSecretKey, { | ||
| apiVersion: '2025-11-17.clover', | ||
| }) | ||
| : null; | ||
|
|
||
| /** | ||
| * Extract domain from a website URL or email | ||
| * @param input - URL (e.g., "https://example.com") or email (e.g., "user@example.com") | ||
| * @returns Normalized domain (e.g., "example.com") | ||
| */ | ||
| export const extractDomain = (input: string): string | null => { | ||
| if (!input) return null; | ||
|
|
||
| try { | ||
| // If it looks like an email, extract domain from after @ | ||
| if (input.includes('@') && !input.includes('://')) { | ||
| const domain = input.split('@')[1]?.toLowerCase().trim(); | ||
| return domain || null; | ||
| } | ||
|
|
||
| // Otherwise, treat as URL | ||
| let url = input.trim().toLowerCase(); | ||
|
|
||
| // Add protocol if missing | ||
| if (!url.startsWith('http://') && !url.startsWith('https://')) { | ||
| url = `https://${url}`; | ||
| } | ||
|
|
||
| const parsed = new URL(url); | ||
| return parsed.hostname.replace(/^www\./, ''); | ||
| } catch { | ||
| return null; | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Check if a domain belongs to an existing Stripe customer | ||
| * Searches by customer email domain and metadata | ||
| * | ||
| * @param domain - The domain to check (e.g., "acme.com") | ||
| * @returns Customer ID if found, null otherwise | ||
| */ | ||
| export const findStripeCustomerByDomain = async ( | ||
| domain: string, | ||
| ): Promise<{ customerId: string; customerName: string | null } | null> => { | ||
| if (!stripe) { | ||
| console.warn('Stripe client not initialized - skipping customer lookup'); | ||
| return null; | ||
| } | ||
|
|
||
| if (!domain) { | ||
| return null; | ||
| } | ||
|
|
||
| const normalizedDomain = domain.toLowerCase().trim(); | ||
|
|
||
| try { | ||
| // Search for customers with emails matching this domain | ||
| // Stripe's search supports email domain matching | ||
| const customers = await stripe.customers.search({ | ||
| query: `email~"@${normalizedDomain}"`, | ||
| limit: 1, | ||
| }); | ||
|
|
||
| if (customers.data.length > 0) { | ||
| const customer = customers.data[0]; | ||
| return { | ||
| customerId: customer.id, | ||
| customerName: customer.name ?? null, | ||
| }; | ||
| } | ||
|
|
||
| // Fallback: Check customers with domain in metadata | ||
| // This handles cases where customer email might not match company domain | ||
| const customersWithMetadata = await stripe.customers.search({ | ||
| query: `metadata["domain"]:"${normalizedDomain}"`, | ||
| limit: 1, | ||
| }); | ||
|
|
||
| if (customersWithMetadata.data.length > 0) { | ||
| const customer = customersWithMetadata.data[0]; | ||
| return { | ||
| customerId: customer.id, | ||
| customerName: customer.name ?? null, | ||
| }; | ||
| } | ||
|
|
||
| return null; | ||
| } catch (error) { | ||
| console.error('Error searching Stripe customers:', error); | ||
| return null; | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Check if a domain is an active Stripe customer with a valid subscription | ||
| * | ||
| * @param domain - The domain to check | ||
| * @returns true if domain has an active subscription | ||
| */ | ||
| export const isDomainActiveStripeCustomer = async (domain: string): Promise<boolean> => { | ||
| const customer = await findStripeCustomerByDomain(domain); | ||
|
|
||
| if (!customer) { | ||
| return false; | ||
| } | ||
|
|
||
| if (!stripe) { | ||
| return false; | ||
| } | ||
|
|
||
| try { | ||
| // Check if customer has an active subscription | ||
| const subscriptions = await stripe.subscriptions.list({ | ||
| customer: customer.customerId, | ||
| status: 'active', | ||
| limit: 1, | ||
| }); | ||
|
|
||
| return subscriptions.data.length > 0; | ||
| } catch (error) { | ||
| console.error('Error checking Stripe subscriptions:', error); | ||
| return false; | ||
| } | ||
| }; |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Domain-based access grants for common email providers
The auto-approval logic in
isDomainActiveStripeCustomergrants access based solely on matching the user's email domain with any Stripe customer's email domain. This means users with common email domains likegmail.com,yahoo.com, oroutlook.comcould gain unauthorized access to any organization they're a member of, if any existing Stripe customer uses the same email provider. The domain check doesn't verify the user actually belongs to the paying organization, only that someone with the same email domain has an active subscription somewhere.Additional Locations (1)
apps/app/src/lib/stripe.ts#L53-L103