Skip to content

Commit

Permalink
Enforce renderable interface for security.
Browse files Browse the repository at this point in the history
  • Loading branch information
vedanshujain committed Nov 15, 2024
1 parent 0c12e40 commit 12e7e6c
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 3 deletions.
23 changes: 22 additions & 1 deletion app/src/lib/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace App;

final class Cart {
require_once __DIR__ . '/Interfaces/PublicRenderable.php';
use App\Interfaces\PublicRenderable;

final class Cart implements PublicRenderable {
public array $items;
public array $coupons;
public array $fees;
Expand All @@ -11,4 +14,22 @@ final class Cart {
public array $billing_address;
public bool $needs_shipping;
public bool $needs_payment;

public function to_public_array(): array {
return array(
'items' => array_map(
function ( $item ) {
return $item->to_public_array();
},
$this->items ?? array()
),
'coupons' => $this->coupons ?? array(),
'fees' => $this->fees ?? array(),
'totals' => $this->totals ?? array(),
'shipping_address' => $this->shipping_address ?? array(),
'billing_address' => $this->billing_address ?? array(),
'needs_shipping' => $this->needs_shipping ?? false,
'needs_payment' => $this->needs_payment ?? false,
);
}
}
27 changes: 26 additions & 1 deletion app/src/lib/CartItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace App;

final class CartItem {
require_once __DIR__ . '/Interfaces/PublicRenderable.php';
use App\Interfaces\PublicRenderable;

final class CartItem implements PublicRenderable {
public string $key;
public int $id;
public string $type;
Expand All @@ -21,4 +24,26 @@ final class CartItem {
public array $item_data;
public array $prices;
public array $totals;

public function to_public_array(): array {
return array(
'key' => $this->key,
'id' => $this->id,
'type' => $this->type,
'quantity' => $this->quantity,
'quantity_limits' => $this->quantity_limits ?? array(),
'name' => $this->name,
'short_description' => $this->short_description,
'description' => $this->description,
'sku' => $this->sku,
'backorders_allowed' => $this->backorders_allowed,
'show_backorder_badge' => $this->show_backorder_badge,
'sold_individually' => $this->sold_individually,
'images' => $this->images,
'variation' => $this->variation ?? array(),
'item_data' => $this->item_data ?? array(),
'prices' => $this->prices,
'totals' => $this->totals,
);
}
}
3 changes: 3 additions & 0 deletions app/src/lib/HotStore/ProductHotStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ private static function zkey(): string {
public static function fetch( array $product_ids ): array {
global $redis;
$serialized_products = $redis->hMGet( self::hkey(), $product_ids );
if ( ! $serialized_products ) {
return array();
}
$products = array();
foreach ( $serialized_products as $id => $product ) {
if ( ! $product ) {
Expand Down
6 changes: 6 additions & 0 deletions app/src/lib/Interfaces/PublicRenderable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace App\Interfaces;

interface PublicRenderable {
public function to_public_array(): array;
}
4 changes: 3 additions & 1 deletion app/src/store/cart-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ function validate_cart_items( Cart $cart ): array {
$cart_item_subtotal = $cart_item->prices['price'] * $cart_item->quantity;
$cart_item_total = $cart_item_subtotal;

$cart_item->key = $id;
$cart_item->type = $product->type;
$cart_item->name = $product->name;
$cart_item->short_description = $product->short_description;
$cart_item->description = $product->description;
Expand Down Expand Up @@ -219,5 +221,5 @@ function is_read_request(): bool {

function render_cart( Cart $cart ): void {
header( 'Content-Type: application/json' );
print_r( json_encode( $cart ) );
print_r( json_encode( $cart->to_public_array() ) );
}

0 comments on commit 12e7e6c

Please sign in to comment.