Node.jsãPolicyã«ããå®å ¨ã«å®è¡ãã
ãã®è¨äºã¯ãNode.js Advent Calendar 2019ã®18æ¥ç®ã§ãã
Policy ã¨ã¯ï¼
å®è¡ã³ã¼ããå¶å¾¡ããã»ãã¥ãªãã£æ©æ§ãNode.jsã«å
¥ãã¾ããã
ããªã·ã¼ãã¡ã¤ã«ã使ããæ´åæ§ã®ãã§ãã¯ãè¡ãã¾ãã
ããã«ãããrequire
ããã¨ãã«ãã¡ã¤ã«ãå¤åãã¦ããªããã¨ãä¿è¨¼ãããã¨ãå¯è½ã§ãã
ã¢ã«ã´ãªãºã ã¯ãw3cã«ããSRIã¨åæ§ã§ãã
ãã®æ©è½ã¯ãã¾ã å®é¨ä¸ã®ãã§ã¼ãºãªãããã©ã°ãå¿ è¦ã¨ãªãã¾ãã
使ãæ¹
// app.js console.log('app'); require('fs'); require('./foo.js');
// foo.js console.log('foo');
æåã«åãã¡ã¤ã«ã®ããã·ã¥å¤ãçæããå¿ è¦ãããã¾ãã
$ printf "sha384-$(cat app.js | openssl dgst -sha384 -binary | base64)" sha384-stoY+K7ZeOkSLHmCYOOrfWcLsKFb1Niv/dkz6f9Q0UP5FQi9pRTglvtGcQ/IPyZR $ printf "sha384-$(cat foo.js | openssl dgst -sha384 -binary | base64)" sha384-1RHAoU62Cn5DMHZLSZZKEnvcml+GOtj5O4BXAUohiYtjGzo0v/iFIM7KOxRwoG7P
policy.jsonãä½æãã以ä¸ã®ããã«ç»é²ãã¾ãã
ããã¯ãå®è¡ãã¡ã¤ã«ãã¹ã¦åæããå¿
è¦ãããã¾ãã(entry-pointå«ã)
{ "resources": { "./app.js": { "integrity": "sha384-stoY+K7ZeOkSLHmCYOOrfWcLsKFb1Niv/dkz6f9Q0UP5FQi9pRTglvtGcQ/IPyZR", "dependencies": { "fs": true, "./foo.js": "./foo.js" } }, "./foo.js": { "integrity": "sha384-1RHAoU62Cn5DMHZLSZZKEnvcml+GOtj5O4BXAUohiYtjGzo0v/iFIM7KOxRwoG7P" } } }
dependencies
㯠booleanã§ãå¯è½ã§ãããæ¬çªç°å¢ã§ã¯æ¨å¥¨ããã¾ããã
ä»åã¯ããã«ãã¤ã³ã¢ã¸ã¥ã¼ã«ã®fsãªã®ã§true
ã¨ãã¦ãã¾ãã
åãã¡ã¤ã«ã§require
ããã¢ã¸ã¥ã¼ã«ãã¡ã¤ã«ã¯ããã¹ã¦åæããå¿
è¦æ§ãããã¾ãã
Dependency Redirection(dependencies
)ã¯ãrequire.cache
ãmodule.constructor
çã®ã¢ã¯ã»ã¹ã«å¯¾ãã¦ã¯ãé²ããã¨ãã§ãã¾ããã
ã¤ã¾ããrequire
ã®ã¿ã®ãã§ãã¯ãè¡ãã¨ãããã¨ã«ãªãã¾ãã
以ä¸ã®ããã«policy.jsonãæå®ãã¦å®è¡ãã¾ãã
$ node --experimental-policy=policy.json app.js
ã¾ã¨ã
å³æ ¼ã«è¡ãã®ã§ããã°ãã³ã¼ãããããã·ã¥å¤ã¯å¤ããããPolicyã使ãã®ã¯æå¹ã§ãã
ããããããä¸ã¤ã¥ã¤è¡ãã®ã¯å¤§å¤ãªãããä½ãããã®CLIã§ä¸æ¬ã§è¡ããæ©æ§ãå¿
è¦ã§ãã
ã¾ã ã¦ã¼ã¶ã¼ã©ã³ãã«ã¯èªåãç¥ãéãåå¨ããªãããä»å¾ã«æå¾
ã§ãã