Skip to content

Wallos: Open-source, self-hostable personal subscription tracker. Visualize your recurring expenses, manage your budget, and save money.

fix(subscriptions): enforce foreign-key ownership on subscription save (GHSA-797x-473j-px9p)

Miguel Ribeiroba9973d9e2b3
diff --git a/endpoints/cronjobs/sendnotifications.php b/endpoints/cronjobs/sendnotifications.php
index 507d074..7a225d2 100644
--- a/endpoints/cronjobs/sendnotifications.php
+++ b/endpoints/cronjobs/sendnotifications.php
@@ -455,8 +455,9 @@ while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {
$mail->Port = $email['smtpPort'];
- $stmt = $db->prepare('SELECT * FROM household WHERE id = :userId');
- $stmt->bindValue(':userId', $payerUserId, SQLITE3_INTEGER);
+ $stmt = $db->prepare('SELECT * FROM household WHERE id = :id AND user_id = :ownerId');
+ $stmt->bindValue(':id', $payerUserId, SQLITE3_INTEGER);
+ $stmt->bindValue(':ownerId', $userId, SQLITE3_INTEGER);
$result = $stmt->execute();
$user = $result->fetchArray(SQLITE3_ASSOC);
@@ -500,8 +501,9 @@ while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {
} else {
foreach ($notify as $payerUserId => $perUser) {
// Get name of user from household table
- $stmt = $db->prepare('SELECT * FROM household WHERE id = :userId');
- $stmt->bindValue(':userId', $payerUserId, SQLITE3_INTEGER);
+ $stmt = $db->prepare('SELECT * FROM household WHERE id = :id AND user_id = :ownerId');
+ $stmt->bindValue(':id', $payerUserId, SQLITE3_INTEGER);
+ $stmt->bindValue(':ownerId', $userId, SQLITE3_INTEGER);
$result = $stmt->execute();
$user = $result->fetchArray(SQLITE3_ASSOC);
@@ -559,8 +561,9 @@ while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {
} else {
foreach ($notify as $payerUserId => $perUser) {
// Get name of user from household table
- $stmt = $db->prepare('SELECT * FROM household WHERE id = :userId');
- $stmt->bindValue(':userId', $payerUserId, SQLITE3_INTEGER);
+ $stmt = $db->prepare('SELECT * FROM household WHERE id = :id AND user_id = :ownerId');
+ $stmt->bindValue(':id', $payerUserId, SQLITE3_INTEGER);
+ $stmt->bindValue(':ownerId', $userId, SQLITE3_INTEGER);
$result = $stmt->execute();
$user = $result->fetchArray(SQLITE3_ASSOC);
@@ -614,8 +617,9 @@ while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {
if ($telegramNotificationsEnabled) {
foreach ($notify as $payerUserId => $perUser) {
// Get name of user from household table
- $stmt = $db->prepare('SELECT * FROM household WHERE id = :userId');
- $stmt->bindValue(':userId', $payerUserId, SQLITE3_INTEGER);
+ $stmt = $db->prepare('SELECT * FROM household WHERE id = :id AND user_id = :ownerId');
+ $stmt->bindValue(':id', $payerUserId, SQLITE3_INTEGER);
+ $stmt->bindValue(':ownerId', $userId, SQLITE3_INTEGER);
$result = $stmt->execute();
$user = $result->fetchArray(SQLITE3_ASSOC);
@@ -667,8 +671,9 @@ while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {
if ($pushplusNotificationsEnabled) {
foreach ($notify as $payerUserId => $perUser) {
// Get name of user from household table
- $stmt = $db->prepare('SELECT * FROM household WHERE id = :userId');
- $stmt->bindValue(':userId', $payerUserId, SQLITE3_INTEGER);
+ $stmt = $db->prepare('SELECT * FROM household WHERE id = :id AND user_id = :ownerId');
+ $stmt->bindValue(':id', $payerUserId, SQLITE3_INTEGER);
+ $stmt->bindValue(':ownerId', $userId, SQLITE3_INTEGER);
$result = $stmt->execute();
$user = $result->fetchArray(SQLITE3_ASSOC);
@@ -729,8 +734,9 @@ while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {
} else {
foreach ($notify as $payerUserId => $perUser) {
// Get name of user from household table
- $stmt = $db->prepare('SELECT * FROM household WHERE id = :userId');
- $stmt->bindValue(':userId', $payerUserId, SQLITE3_INTEGER);
+ $stmt = $db->prepare('SELECT * FROM household WHERE id = :id AND user_id = :ownerId');
+ $stmt->bindValue(':id', $payerUserId, SQLITE3_INTEGER);
+ $stmt->bindValue(':ownerId', $userId, SQLITE3_INTEGER);
$result = $stmt->execute();
$user = $result->fetchArray(SQLITE3_ASSOC);
@@ -789,8 +795,9 @@ while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {
if ($pushoverNotificationsEnabled) {
foreach ($notify as $payerUserId => $perUser) {
// Get name of user from household table
- $stmt = $db->prepare('SELECT * FROM household WHERE id = :userId');
- $stmt->bindValue(':userId', $payerUserId, SQLITE3_INTEGER);
+ $stmt = $db->prepare('SELECT * FROM household WHERE id = :id AND user_id = :ownerId');
+ $stmt->bindValue(':id', $payerUserId, SQLITE3_INTEGER);
+ $stmt->bindValue(':ownerId', $userId, SQLITE3_INTEGER);
$result = $stmt->execute();
$user = $result->fetchArray(SQLITE3_ASSOC);
@@ -836,8 +843,9 @@ while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {
} else {
foreach ($notify as $payerUserId => $perUser) {
// Get name of user from household table
- $stmt = $db->prepare('SELECT * FROM household WHERE id = :userId');
- $stmt->bindValue(':userId', $payerUserId, SQLITE3_INTEGER);
+ $stmt = $db->prepare('SELECT * FROM household WHERE id = :id AND user_id = :ownerId');
+ $stmt->bindValue(':id', $payerUserId, SQLITE3_INTEGER);
+ $stmt->bindValue(':ownerId', $userId, SQLITE3_INTEGER);
$result = $stmt->execute();
$user = $result->fetchArray(SQLITE3_ASSOC);
@@ -897,8 +905,9 @@ while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {
} else {
foreach ($notify as $payerUserId => $perUser) {
// Get name of user from household table
- $stmt = $db->prepare('SELECT * FROM household WHERE id = :userId');
- $stmt->bindValue(':userId', $payerUserId, SQLITE3_INTEGER);
+ $stmt = $db->prepare('SELECT * FROM household WHERE id = :id AND user_id = :ownerId');
+ $stmt->bindValue(':id', $payerUserId, SQLITE3_INTEGER);
+ $stmt->bindValue(':ownerId', $userId, SQLITE3_INTEGER);
$result = $stmt->execute();
$user = $result->fetchArray(SQLITE3_ASSOC);
@@ -965,8 +974,9 @@ while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {
if ($serverchanNotificationsEnabled) {
foreach ($notify as $payerUserId => $perUser) {
// Get name of user from household table
- $stmt = $db->prepare('SELECT * FROM household WHERE id = :userId');
- $stmt->bindValue(':userId', $payerUserId, SQLITE3_INTEGER);
+ $stmt = $db->prepare('SELECT * FROM household WHERE id = :id AND user_id = :ownerId');
+ $stmt->bindValue(':id', $payerUserId, SQLITE3_INTEGER);
+ $stmt->bindValue(':ownerId', $userId, SQLITE3_INTEGER);
$result = $stmt->execute();
$user = $result->fetchArray(SQLITE3_ASSOC);
@@ -1028,8 +1038,9 @@ while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {
foreach ($notify as $payerUserId => $perUser) {
// Get name of user from household table
- $stmt = $db->prepare('SELECT * FROM household WHERE id = :userId');
- $stmt->bindValue(':userId', $payerUserId, SQLITE3_INTEGER);
+ $stmt = $db->prepare('SELECT * FROM household WHERE id = :id AND user_id = :ownerId');
+ $stmt->bindValue(':id', $payerUserId, SQLITE3_INTEGER);
+ $stmt->bindValue(':ownerId', $userId, SQLITE3_INTEGER);
$result = $stmt->execute();
$user = $result->fetchArray(SQLITE3_ASSOC);
diff --git a/endpoints/subscription/add.php b/endpoints/subscription/add.php
index 050ed92..03d4e03 100644
--- a/endpoints/subscription/add.php
+++ b/endpoints/subscription/add.php
@@ -269,6 +269,55 @@ if ($replacementSubscriptionId !== null) {
}
}
+// Reject any foreign key that doesn't belong to the authenticated user, so a
+// subscription can't be attached to another tenant's currency/category/
+// household member/payment method (IDs are global auto-increment integers
+// and easily enumerable).
+function rejectForeignId($message)
+{
+ header('Content-Type: application/json');
+ echo json_encode(['status' => 'Error', 'message' => $message]);
+ exit();
+}
+
+$currStmt = $db->prepare("SELECT id FROM currencies WHERE id = :id AND user_id = :userId");
+$currStmt->bindParam(':id', $currencyId, SQLITE3_INTEGER);
+$currStmt->bindParam(':userId', $userId, SQLITE3_INTEGER);
+$currResult = $currStmt->execute();
+if (!$currResult || !$currResult->fetchArray()) {
+ rejectForeignId('The specified currency does not exist or does not belong to you.');
+}
+
+if ($categoryId !== null && $categoryId !== '') {
+ $catStmt = $db->prepare("SELECT id FROM categories WHERE id = :id AND user_id = :userId");
+ $catStmt->bindParam(':id', $categoryId, SQLITE3_INTEGER);
+ $catStmt->bindParam(':userId', $userId, SQLITE3_INTEGER);
+ $catResult = $catStmt->execute();
+ if (!$catResult || !$catResult->fetchArray()) {
+ rejectForeignId('The specified category does not exist or does not belong to you.');
+ }
+}
+
+if ($payerUserId !== null && $payerUserId !== '') {
+ $payerStmt = $db->prepare("SELECT id FROM household WHERE id = :id AND user_id = :userId");
+ $payerStmt->bindParam(':id', $payerUserId, SQLITE3_INTEGER);
+ $payerStmt->bindParam(':userId', $userId, SQLITE3_INTEGER);
+ $payerResult = $payerStmt->execute();
+ if (!$payerResult || !$payerResult->fetchArray()) {
+ rejectForeignId('The specified household member does not exist or does not belong to you.');
+ }
+}
+
+if ($paymentMethodId !== null && $paymentMethodId !== '') {
+ $pmStmt = $db->prepare("SELECT id FROM payment_methods WHERE id = :id AND (user_id = :userId OR user_id = 0 OR user_id IS NULL)");
+ $pmStmt->bindParam(':id', $paymentMethodId, SQLITE3_INTEGER);
+ $pmStmt->bindParam(':userId', $userId, SQLITE3_INTEGER);
+ $pmResult = $pmStmt->execute();
+ if (!$pmResult || !$pmResult->fetchArray()) {
+ rejectForeignId('The specified payment method does not exist or does not belong to you.');
+ }
+}
+
if ($logoUrl !== "") {
$result = getLogoFromUrl($logoUrl, '../../images/uploads/logos/', $name, $settings, $i18n);
if ($result['success']) {