メインコンテンツまでスキップ

ストレージの保護

認可されたユーザーのみがファイルにアクセスできるように、ストレージバケットを保護する @secureStorage デコレーター

Squid のストレージ機能では、Squid Client SDK を使用してファイルを管理できるので、どのクライアントからもファイルにアクセスできます。Squid Client SDK を使用した Squid ストレージの利用方法の詳細については、view the Squid storage documentation をご覧ください。

Squid ストレージバケット統合のセキュリティを高めるには、Squid バックエンド内の SquidService クラスで @secureStorage デコレーターを使用し、統合IDとアクションの種類を渡します。次のコードは、組み込みのストレージバケットへのフルアクセスを許可します:

Backend code
import { secureStorage, SquidService } from '@squidcloud/backend';

@secureStorage('all', 'built_in_storage')
allowAllAccessToBuiltInStorage(): boolean {
return true;
}

別のストレージ統合のための Squid ストレージバケットを保護するには、デコレーターに統合IDを指定します:

Backend code
import { secureStorage, SquidService } from '@squidcloud/backend';

@secureStorage('all', 'YOUR_STORAGE_INTEGRATION_ID')
allowAllAccessToStorageIntegration(): boolean {
return true;
}

利用可能なアクションタイプは以下の通りです:

'read'

'read' アクションには、メタデータの読み取り、ダウンロード URL の生成、ファイルのダウンロード、およびディレクトリ内容の一覧表示が含まれます。

'write'

'write' アクションには、新しいファイルの挿入、既存ファイルの更新、およびファイルの削除が含まれます。

'insert'

'insert' アクションは、新規ファイルの挿入を許可しますが、既存ファイルの内容の更新や削除は許可しません。

'delete'

'delete' アクションは、既存のファイルの削除を許可します。

'all'

'all' アクションは、すべての利用可能なバケットアクションを含みます。

書き込みの保護

次の関数は、認証済みユーザーが組み込みのストレージバケットに対してファイルをアップロード、更新、および削除することを許可します:

Backend code
import { secureStorage, SquidService } from '@squidcloud/backend';

@secureStorage('write', 'built_in_storage')
allowAuthenticatedWrites(): boolean {
return this.isAuthenticated();
}

読み取りの保護

ディレクトリ名、ファイルメタデータの読み取り、およびダウンロード URL の生成を保護するには、'read' アクションタイプを使用します。下記の関数は、ファイルパスがユーザーのユーザーIDの場合にダウンロード URL を生成することを許可します:

Backend code
import { secureStorage, SquidService, StorageContext } from '@squidcloud/backend';

@secureStorage('read', 'built_in_storage')
allowReadUserFiles(context: StorageContext): boolean {
// Validate if the requested action is to get a download URL
if (context.functionality !== 'getDownloadUrl') {
return false;
}

const userId = this.getUserAuth()?.userId;
if (!userId) return false;

// Check any paths the user is trying to read to verify they're in the user's directory
for (const path of context.pathsInBucket) {
if (if (!path.startsWith(`user/${userId}`)) {
return false
}
}
return true;
}

関数のパラメーターとして StorageContext オブジェクトを含めることで、セキュリティ関数はクライアントが実行しようとしているアクションに関する情報へアクセスできます。以下は、例としての StorageContext オブジェクトを示しています:

{
integrationId: 'built_in_storage',
pathsInBucket: [ 'test/path/img.jpg' ],
action: 'read',
functionality: 'getFileMetadata'
}