@@ -4,117 +4,121 @@ const fs = require('fs'); // @TODO move to 'imports from' when moved to TS !
44const core = require ( '@actions/core' ) ; // @TODO move to 'imports from' when moved to TS !
55const io = require ( '@actions/io' ) ; // @TODO move to 'imports from' when moved to TS !
66
7- const { path : pathSDK , glob : globSDK , outputs : outputsSDK , CONSTANTS : SDK_CONSTANTS } = require ( './node-sdk' ) ; // @TODO move to 'imports from' when moved to TS !
7+ const SDK = require ( './node-sdk' ) ; // @TODO move to 'imports from' when moved to TS !
88
99async function run ( ) {
10+ const trustedPathHelper = SDK . path . trustedPathHelpers ( ) ;
1011 /** INPUTS **/
11- const NAME_INPUT = core . getInput ( 'NAME ' , { required : true } ) ;
12+ const NAME_INPUT = core . getInput ( 'name ' , { required : true } ) ;
1213 const FORMAT_INPUT = core . getInput ( 'format' , { required : true } ) ;
1314 const REPORTS_INPUT = core . getInput ( 'files' , { required : true } ) ;
1415 // Following inputs are not marked as required by the action but a default value must be there, so using `required` works
1516 const PATH_INPUT = core . getInput ( 'path' , { required : true } ) ;
1617 const FLAG_LIST_INPUT = core . getMultilineInput ( 'flags' , { required : true } ) ;
1718 const FOLLOW_SYMLINK_INPUT = core . getBooleanInput ( 'follow-symbolic-links' , { required : true } ) ;
1819
19- const groupDirectory = await core . group (
20+ const trustedGroupDirectory = await core . group (
2021 'Resolve group directory path' ,
2122 async ( ) => {
22- const res = path . resolve ( PATH_INPUT , NAME_INPUT ) ;
23+ const res = trustedPathHelper . trust ( path . join ( PATH_INPUT , NAME_INPUT ) ) ;
2324 core . info ( 'group directory=' + res ) ;
2425
2526 return res ;
2627 }
2728 ) ;
2829
29- const originalReportPaths = await core . group (
30+ const trustedOriginalReportPaths = await core . group (
3031 'Resolve reports' ,
3132 async ( ) => {
3233 const result = [ ] ;
33- for await ( const fp of globSDK . lookup ( REPORTS_INPUT , { followSymbolicLinks : FOLLOW_SYMLINK_INPUT } ) ) {
34- const normalizedFp = pathSDK . relativeToGHWorkspace ( fp ) ;
34+ for await ( const fp of SDK . glob . lookup ( REPORTS_INPUT , { followSymbolicLinks : FOLLOW_SYMLINK_INPUT } ) ) {
35+ const normalizedFp = trustedPathHelper . toWorkspaceRelative ( fp ) ;
3536 core . info ( 'Found ' + normalizedFp ) ;
3637 result . push ( normalizedFp ) ;
3738 }
3839 return result ;
3940 }
4041 ) ;
41- core . debug ( 'reports to copy=' + JSON . stringify ( originalReportPaths ) ) ;
42+ core . debug ( 'reports to copy=' + JSON . stringify ( trustedOriginalReportPaths ) ) ;
4243
43- if ( 0 === originalReportPaths . length ) {
44+ if ( 0 === trustedOriginalReportPaths . length ) {
4445 core . setFailed ( 'You must provide at least one report !' ) ;
4546 }
4647
47- const reportsMap = await core . group (
48+ const trustedReportsMap = await core . group (
4849 'Build reports map' ,
4950 async ( ) => {
5051 let counter = 0 ;
51- return originalReportPaths . map ( filepath => {
52+ return trustedOriginalReportPaths . map ( trustedSource => {
5253 // Ensure report files uniqueness while keeping a bit of clarity regarding the mapping with original files !
53- const filename = path . basename ( filepath ) + '-report-' + ( ++ counter ) ;
54- const destination = pathSDK . relativeToGHWorkspace ( groupDirectory , filename ) ;
55- core . info ( filepath + ' => ' + destination ) ;
56- return { source : filepath , filename : filename , dest : destination } ;
54+ const trustedFilename = path . basename ( trustedSource ) + '-report-' + ( ++ counter ) ; // Only trusted content !
55+ const trustedDestination = path . join ( trustedGroupDirectory , trustedFilename ) ; // Only trusted content !
56+ core . info ( trustedSource + ' => ' + trustedDestination ) ;
57+
58+ return { source : trustedSource , filename : trustedFilename , dest : trustedDestination } ;
5759 } ) ;
5860 }
5961 ) ;
60- core . debug ( 'reports map=' + JSON . stringify ( reportsMap ) ) ;
62+ core . debug ( 'reports map=' + JSON . stringify ( trustedReportsMap ) ) ;
6163
62- const metadata = await core . group (
64+ const trustedMetadata = await core . group (
6365 'Build group metadata' ,
6466 async ( ) => {
6567 const res = {
6668 name : NAME_INPUT ,
6769 format : FORMAT_INPUT ,
68- reports : reportsMap . map ( v => v . filename ) ,
70+ reports : trustedReportsMap . map ( v => v . filename ) ,
6971 flags : FLAG_LIST_INPUT
7072 } ;
7173 core . info ( 'Created' ) ;
7274
7375 return res ;
7476 }
7577 ) ;
76- core . debug ( 'metadata=' + JSON . stringify ( metadata ) ) ;
78+ core . debug ( 'metadata=' + JSON . stringify ( trustedMetadata ) ) ;
7779
7880 await core . group ( 'Create group directory' , ( ) => {
79- core . info ( 'Create group directory at ' + groupDirectory ) ;
81+ core . info ( 'Create group directory at ' + trustedGroupDirectory ) ;
8082
81- return io . mkdirP ( groupDirectory )
83+ return io . mkdirP ( trustedGroupDirectory )
8284 } ) ;
8385
8486 await core . group (
8587 'Copy reports' ,
86- async ( ) => reportsMap . map ( async ( { source , dest } ) => {
87- core . info ( source + ' => ' + dest ) ;
88+ async ( ) => trustedReportsMap . map ( async ( trustedMap ) => {
89+ core . info ( trustedMap . source + ' => ' + trustedMap . dest ) ;
8890
89- return io . cp ( source , dest ) ;
91+ return io . cp ( trustedMap . source , trustedMap . dest ) ;
9092 } )
9193 ) ;
9294
9395 await core . group (
9496 'Create metadata file' ,
9597 async ( ) => {
96- const filepath = path . join ( groupDirectory , SDK_CONSTANTS . METADATA_FILENAME ) ;
97- core . info ( 'Create metadata file at ' + filepath + ' with: ' + JSON . stringify ( metadata ) ) ;
98- fs . writeFileSync ( filepath , JSON . stringify ( metadata ) ) ;
98+ const trustedFp = trustedPathHelper . trust ( path . resolve ( trustedGroupDirectory , SDK . METADATA_FILENAME ) ) ;
99+ core . info ( 'Create metadata file at ' + trustedFp + ' with: ' + JSON . stringify ( trustedMetadata ) ) ;
100+
101+ fs . writeFileSync ( trustedFp , JSON . stringify ( trustedMetadata ) ) ;
99102 } ) ;
100103
101104 const outputs = await core . group (
102105 'Build action outputs' ,
103106 async ( ) => {
107+ // Be sure to validate any path returned to the end-user !
104108 const res = { } ;
105109
106110 core . info ( "Build 'path' output" ) ;
107- res . path = groupDirectory ;
111+ res . path = trustedPathHelper . trust ( trustedGroupDirectory ) ;
108112 core . info ( "Build 'reports' output" ) ;
109- res . reports = metadata . reports . join ( '\n' ) ;
113+ res . reports = trustedMetadata . reports . join ( '\n' ) ;
110114 core . info ( "Build 'files' output" ) ;
111- res . files = originalReportPaths . join ( '\n' ) ;
115+ res . files = trustedReportsMap . map ( v => v . source ) . join ( '\n' ) ;
112116
113117 return res ;
114118 }
115119 ) ;
116120 core . debug ( 'outputs=' + JSON . stringify ( outputs ) ) ;
117- outputsSDK . bindActionOutputs ( outputs ) ;
121+ SDK . outputs . bindFrom ( outputs ) ;
118122}
119123
120124run ( ) ;
0 commit comments