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

ストレージの保護

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

Squid の storage 機能では、Squid Client SDK を使ってファイルを管理できるため、どのクライアントからでもファイルにアクセスできます。Squid Client SDK で Squid storage を使用する方法について詳しくは、Squid storage ドキュメントを参照してください

Squid storage bucket integration を保護するには、Squid backend の SquidService クラス内で @secureStorage デコレーターを使用し、integration ID とアクション種別を渡します。次のコードは、組み込みの storage bucket へのフルアクセスを許可します。

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

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

別の storage integration 用に Squid storage bucket を保護するには、デコレーターに integration 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' アクションには、利用可能なすべてのバケットアクションが含まれます。

書き込みの保護

次の関数は、認証済みユーザーが組み込みの storage bucket にファイルをアップロード、更新、削除できるようにします。

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'
}