Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 9054

typeORMでセキュアなカラムをselectさせない

$
0
0

typeORMで、パスワード等のカラムを扱う時にうっかりapiのレスポンスに値を含め無い様にする仕組みが用意されています。

何もしない場合

@Entity()exportclassUser{@PrimaryGeneratedColumn()readonlyid:number;@Column({name:'name',length:255,})name:string;@Column('varchar',{name:'password'})password:string;}

selectすると全カラムが普通に取得されます。うっかりそのままAPIのレスポンスに含めるとまずいです。

除外する

@Entity()exportclassUser{@PrimaryGeneratedColumn()readonlyid:number;@Column({name:'name',length:255,})name:string;@Column('varchar',{name:'password',select:false})// ここpassword:string;}

{select: false}というオプションをつければOKです。SQLレベルで除外されます。

ちなみに何かしらの理由で敢えて取得したい場合は、明示的に書けばOKです。

constuser=awaitthis.userRepository.find({select:['password']});

参考

TypeORM - Amazing ORM for TypeScript and JavaScript (ES7, ES6, ES5). Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, WebSQL databases. Works in NodeJS, Browser, Ionic, Cordova and Electron platforms.

typeorm - Is it possible to 'protect' a property and exclude it from select statements - Stack Overflow


Viewing all articles
Browse latest Browse all 9054

Latest Images

Trending Articles