@@ -2,6 +2,7 @@ import { describe, test } from 'node:test';
22import assert from 'node:assert' ;
33import os from 'os' ;
44import { SerializableError } from './serializable_error' ;
5+ import { pathToFileURL } from 'node:url' ;
56
67void describe ( 'serializable error' , ( ) => {
78 class ErrorWithDetailsAndCode extends Error {
@@ -26,6 +27,21 @@ void describe('serializable error', () => {
2627 } ) ;
2728 } ) ;
2829
30+ void test ( 'that regular stack trace does not contain user homedir for file url paths' , ( ) => {
31+ const error = new Error ( 'test error' ) ;
32+ error . stack = `at methodName (${ pathToFileURL (
33+ process . cwd ( )
34+ ) . toString ( ) } /node_modules/@aws-amplify/test-package/lib/test.js:12:34)\n`;
35+ const serializableError = new SerializableError ( error ) ;
36+ assert . ok ( serializableError . trace ) ;
37+ serializableError . trace ?. forEach ( ( trace ) => {
38+ assert . ok (
39+ trace . file . includes ( os . homedir ( ) ) == false ,
40+ `${ os . homedir ( ) } is included in the ${ trace . file } `
41+ ) ;
42+ } ) ;
43+ } ) ;
44+
2945 void test ( 'that if code is available it is used as the error name' , ( ) => {
3046 const error = new ErrorWithDetailsAndCode (
3147 'some error message' ,
@@ -42,15 +58,15 @@ void describe('serializable error', () => {
4258 assert . deepStrictEqual ( serializableError . name , 'Error' ) ;
4359 } ) ;
4460
45- void test ( 'that no change in error details that does not have AWS ARNs' , ( ) => {
61+ void test ( 'that no change in error details that does not have AWS ARNs or stacks ' , ( ) => {
4662 const error = new ErrorWithDetailsAndCode (
4763 'some error message' ,
48- 'some error details that do not have ARNs'
64+ 'some error details that do not have ARNs or stacks '
4965 ) ;
5066 const serializableError = new SerializableError ( error ) ;
5167 assert . deepStrictEqual (
5268 serializableError . details ,
53- 'some error details that do not have ARNs'
69+ 'some error details that do not have ARNs or stacks '
5470 ) ;
5571 } ) ;
5672
@@ -66,9 +82,112 @@ void describe('serializable error', () => {
6682 ) ;
6783 } ) ;
6884
85+ void test ( 'that stacks are escaped when error details has two AWS stacks' , ( ) => {
86+ const error = new ErrorWithDetailsAndCode (
87+ 'some error message' ,
88+ // eslint-disable-next-line spellcheck/spell-checker
89+ 'some error details with stack: amplify-testapp-test-sandbox-1234abcd and stack: amplify-testapp-test-branch-1234abcd and something else'
90+ ) ;
91+ const serializableError = new SerializableError ( error ) ;
92+ assert . deepStrictEqual (
93+ serializableError . details ,
94+ 'some error details with stack: <escaped stack> and stack: <escaped stack> and something else'
95+ ) ;
96+ } ) ;
97+
6998 void test ( 'that error message is sanitized by removing invalid characters' , ( ) => {
7099 const error = new ErrorWithDetailsAndCode ( 'some" er❌ror ""m"es❌sage❌' ) ;
71100 const serializableError = new SerializableError ( error ) ;
72101 assert . deepStrictEqual ( serializableError . message , 'some error message' ) ;
73102 } ) ;
103+
104+ void test ( 'that error message does not contain AWS ARNs or stacks' , ( ) => {
105+ const error = new ErrorWithDetailsAndCode (
106+ // eslint-disable-next-line spellcheck/spell-checker
107+ 'test error with stack: amplify-testapp-test-branch-1234abcd and arn: arn:aws-iso:service:region::res'
108+ ) ;
109+ const serializableError = new SerializableError ( error ) ;
110+ assert . deepStrictEqual (
111+ serializableError . message ,
112+ 'test error with stack: <escaped stack> and arn: <escaped ARN>'
113+ ) ;
114+ } ) ;
115+
116+ void test ( 'that error message does not contain user homedir' , ( ) => {
117+ const error = new ErrorWithDetailsAndCode ( `${ process . cwd ( ) } test error` ) ;
118+ const serializableError = new SerializableError ( error ) ;
119+ const matches = [
120+ ...serializableError . message . matchAll ( new RegExp ( os . homedir ( ) , 'g' ) ) ,
121+ ] ;
122+ assert . ok (
123+ matches . length === 0 ,
124+ `${ os . homedir ( ) } is included in ${ serializableError . message } `
125+ ) ;
126+ } ) ;
127+
128+ void test ( 'that error message does not contain file url path with user homedir' , ( ) => {
129+ const error = new ErrorWithDetailsAndCode (
130+ `${ pathToFileURL ( process . cwd ( ) ) . toString ( ) } test error`
131+ ) ;
132+ const serializableError = new SerializableError ( error ) ;
133+ const matches = [
134+ ...serializableError . message . matchAll ( new RegExp ( os . homedir ( ) , 'g' ) ) ,
135+ ] ;
136+ assert . ok (
137+ matches . length === 0 ,
138+ `${ os . homedir ( ) } is included in ${ serializableError . message } `
139+ ) ;
140+ } ) ;
141+
142+ void test ( 'that error details do not contain user homedir' , ( ) => {
143+ const error = new ErrorWithDetailsAndCode (
144+ 'test error' ,
145+ `${ process . cwd ( ) } test details`
146+ ) ;
147+ const serializableError = new SerializableError ( error ) ;
148+ const matches = serializableError . details
149+ ? [ ...serializableError . details . matchAll ( new RegExp ( os . homedir ( ) , 'g' ) ) ]
150+ : [ ] ;
151+ assert . ok (
152+ serializableError . details && matches . length === 0 ,
153+ `${ os . homedir ( ) } is included in ${ serializableError . details } `
154+ ) ;
155+ } ) ;
156+
157+ void test ( 'that error details do not contain file url path with user homedir' , ( ) => {
158+ const error = new ErrorWithDetailsAndCode (
159+ 'test error' ,
160+ `${ pathToFileURL ( process . cwd ( ) ) . toString ( ) } test details`
161+ ) ;
162+ const serializableError = new SerializableError ( error ) ;
163+ const matches = serializableError . details
164+ ? [ ...serializableError . details . matchAll ( new RegExp ( os . homedir ( ) , 'g' ) ) ]
165+ : [ ] ;
166+ assert . ok (
167+ serializableError . details && matches . length === 0 ,
168+ `${ os . homedir ( ) } is included in ${ serializableError . details } `
169+ ) ;
170+ } ) ;
171+
172+ void test ( 'that error details do not contain AWS ARNs or stacks' , ( ) => {
173+ const error = new ErrorWithDetailsAndCode (
174+ 'test error' ,
175+ // eslint-disable-next-line spellcheck/spell-checker
176+ 'test error with stack: amplify-testapp-test-branch-1234abcd and arn: arn:aws-iso:service:region::res'
177+ ) ;
178+ const serializableError = new SerializableError ( error ) ;
179+ assert . deepStrictEqual (
180+ serializableError . details ,
181+ 'test error with stack: <escaped stack> and arn: <escaped ARN>'
182+ ) ;
183+ } ) ;
184+
185+ void test ( 'that error details is sanitized by removing invalid characters' , ( ) => {
186+ const error = new ErrorWithDetailsAndCode (
187+ 'test error' ,
188+ 'some" er❌ror ""m"es❌sage❌'
189+ ) ;
190+ const serializableError = new SerializableError ( error ) ;
191+ assert . deepStrictEqual ( serializableError . details , 'some error message' ) ;
192+ } ) ;
74193} ) ;
0 commit comments