@appthreat/caxa 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/main.yml +46 -0
- package/LICENSE +22 -0
- package/README.md +119 -0
- package/build/index.d.mts +19 -0
- package/build/index.d.mts.map +1 -0
- package/build/index.mjs +185 -0
- package/build/index.mjs.map +1 -0
- package/examples/echo-command-line-parameters/index.mjs +1 -0
- package/examples/false/index.mjs +1 -0
- package/examples/native-modules/index.mjs +50 -0
- package/examples/native-modules/package-lock.json +1028 -0
- package/examples/native-modules/package.json +8 -0
- package/examples/web-application/package-lock.json +1190 -0
- package/examples/web-application/package.json +14 -0
- package/examples/web-application/source/index.mts +11 -0
- package/examples/web-application/tsconfig.json +19 -0
- package/package.json +45 -0
- package/source/index.mts +337 -0
- package/source/tsconfig.json +19 -0
- package/stubs/stub.go +298 -0
package/stubs/stub.go
ADDED
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
package main
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"archive/tar"
|
|
5
|
+
"bytes"
|
|
6
|
+
"compress/gzip"
|
|
7
|
+
"context"
|
|
8
|
+
"encoding/json"
|
|
9
|
+
"errors"
|
|
10
|
+
"fmt"
|
|
11
|
+
"io"
|
|
12
|
+
"log"
|
|
13
|
+
"os"
|
|
14
|
+
"os/exec"
|
|
15
|
+
"path"
|
|
16
|
+
"path/filepath"
|
|
17
|
+
"regexp"
|
|
18
|
+
"strconv"
|
|
19
|
+
"strings"
|
|
20
|
+
"time"
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
func main() {
|
|
24
|
+
executableFile, err := os.Executable()
|
|
25
|
+
if err != nil {
|
|
26
|
+
log.Fatalf("caxa stub: Failed to find executable: %v", err)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
executable, err := os.ReadFile(executableFile)
|
|
30
|
+
if err != nil {
|
|
31
|
+
log.Fatalf("caxa stub: Failed to read executable: %v", err)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
footerSeparator := []byte("\n")
|
|
35
|
+
footerIndex := bytes.LastIndex(executable, footerSeparator)
|
|
36
|
+
if footerIndex == -1 {
|
|
37
|
+
log.Fatalf("caxa stub: Failed to find footer (did you append an archive and a footer to the stub?): %v", err)
|
|
38
|
+
}
|
|
39
|
+
footerString := executable[footerIndex+len(footerSeparator):]
|
|
40
|
+
var footer struct {
|
|
41
|
+
Identifier string `json:"identifier"`
|
|
42
|
+
Command []string `json:"command"`
|
|
43
|
+
UncompressionMessage string `json:"uncompressionMessage"`
|
|
44
|
+
}
|
|
45
|
+
if err := json.Unmarshal(footerString, &footer); err != nil {
|
|
46
|
+
log.Fatalf("caxa stub: Failed to parse JSON in footer: %v", err)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
var applicationDirectory string
|
|
50
|
+
for extractionAttempt := 0; true; extractionAttempt++ {
|
|
51
|
+
lock := path.Join(os.TempDir(), "caxa/locks", footer.Identifier, strconv.Itoa(extractionAttempt))
|
|
52
|
+
applicationDirectory = path.Join(os.TempDir(), "caxa/applications", footer.Identifier, strconv.Itoa(extractionAttempt))
|
|
53
|
+
applicationDirectoryFileInfo, err := os.Stat(applicationDirectory)
|
|
54
|
+
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
|
55
|
+
log.Fatalf("caxa stub: Failed to find information about the application directory: %v", err)
|
|
56
|
+
}
|
|
57
|
+
if err == nil && !applicationDirectoryFileInfo.IsDir() {
|
|
58
|
+
log.Fatalf("caxa stub: Path to application directory already exists and isn’t a directory: %v", err)
|
|
59
|
+
}
|
|
60
|
+
if err == nil && applicationDirectoryFileInfo.IsDir() {
|
|
61
|
+
lockFileInfo, err := os.Stat(lock)
|
|
62
|
+
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
|
63
|
+
log.Fatalf("caxa stub: Failed to find information about the lock: %v", err)
|
|
64
|
+
}
|
|
65
|
+
if err == nil && !lockFileInfo.IsDir() {
|
|
66
|
+
log.Fatalf("caxa stub: Path to lock already exists and isn’t a directory: %v", err)
|
|
67
|
+
}
|
|
68
|
+
if err == nil && lockFileInfo.IsDir() {
|
|
69
|
+
// Application directory exists and lock exists as well, so a previous extraction wasn’t successful or an extraction is happening right now and hasn’t finished yet, in either case, start over with a fresh name.
|
|
70
|
+
continue
|
|
71
|
+
}
|
|
72
|
+
if err != nil && errors.Is(err, os.ErrNotExist) {
|
|
73
|
+
// Application directory exists and lock doesn’t exist, so a previous extraction was successful. Use the cached version of the application directory and don’t extract again.
|
|
74
|
+
break
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if err != nil && errors.Is(err, os.ErrNotExist) {
|
|
78
|
+
ctx, cancelCtx := context.WithCancel(context.Background())
|
|
79
|
+
if footer.UncompressionMessage != "" {
|
|
80
|
+
fmt.Fprint(os.Stderr, footer.UncompressionMessage)
|
|
81
|
+
go func() {
|
|
82
|
+
ticker := time.NewTicker(time.Second * 5)
|
|
83
|
+
defer ticker.Stop()
|
|
84
|
+
for {
|
|
85
|
+
select {
|
|
86
|
+
case <-ticker.C:
|
|
87
|
+
fmt.Fprint(os.Stderr, ".")
|
|
88
|
+
case <-ctx.Done():
|
|
89
|
+
fmt.Fprintln(os.Stderr, "")
|
|
90
|
+
return
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}()
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if err := os.MkdirAll(lock, 0755); err != nil {
|
|
97
|
+
log.Fatalf("caxa stub: Failed to create the lock directory: %v", err)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// The use of ‘Repeat’ below is to make it even more improbable that the separator will appear literally in the compiled stub.
|
|
101
|
+
archiveSeparator := []byte("\n" + strings.Repeat("CAXA", 3) + "\n")
|
|
102
|
+
archiveIndex := bytes.Index(executable, archiveSeparator)
|
|
103
|
+
if archiveIndex == -1 {
|
|
104
|
+
log.Fatalf("caxa stub: Failed to find archive (did you append the separator when building the stub?): %v", err)
|
|
105
|
+
}
|
|
106
|
+
archive := executable[archiveIndex+len(archiveSeparator) : footerIndex]
|
|
107
|
+
|
|
108
|
+
if err := Untar(bytes.NewReader(archive), applicationDirectory); err != nil {
|
|
109
|
+
log.Fatalf("caxa stub: Failed to uncompress archive: %v", err)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
os.Remove(lock)
|
|
113
|
+
|
|
114
|
+
cancelCtx()
|
|
115
|
+
break
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
expandedCommand := make([]string, len(footer.Command))
|
|
120
|
+
applicationDirectoryPlaceholderRegexp := regexp.MustCompile(`\{\{\s*caxa\s*\}\}`)
|
|
121
|
+
for key, commandPart := range footer.Command {
|
|
122
|
+
expandedCommand[key] = applicationDirectoryPlaceholderRegexp.ReplaceAllLiteralString(commandPart, applicationDirectory)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
command := exec.Command(expandedCommand[0], append(expandedCommand[1:], os.Args[1:]...)...)
|
|
126
|
+
command.Stdin = os.Stdin
|
|
127
|
+
command.Stdout = os.Stdout
|
|
128
|
+
command.Stderr = os.Stderr
|
|
129
|
+
err = command.Run()
|
|
130
|
+
var exitError *exec.ExitError
|
|
131
|
+
if errors.As(err, &exitError) {
|
|
132
|
+
os.Exit(exitError.ExitCode())
|
|
133
|
+
} else if err != nil {
|
|
134
|
+
log.Fatalf("caxa stub: Failed to run command: %v", err)
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Adapted from https://github.com/golang/build/blob/db2c93053bcd6b944723c262828c90af91b0477a/internal/untar/untar.go and https://github.com/mholt/archiver/tree/v3.5.0
|
|
139
|
+
|
|
140
|
+
// Copyright 2017 The Go Authors. All rights reserved.
|
|
141
|
+
// Use of this source code is governed by a BSD-style
|
|
142
|
+
// license that can be found in the LICENSE file.
|
|
143
|
+
|
|
144
|
+
// Package untar untars a tarball to disk.
|
|
145
|
+
// package untar
|
|
146
|
+
|
|
147
|
+
// import (
|
|
148
|
+
// "archive/tar"
|
|
149
|
+
// "compress/gzip"
|
|
150
|
+
// "fmt"
|
|
151
|
+
// "io"
|
|
152
|
+
// "log"
|
|
153
|
+
// "os"
|
|
154
|
+
// "path"
|
|
155
|
+
// "path/filepath"
|
|
156
|
+
// "strings"
|
|
157
|
+
// "time"
|
|
158
|
+
// )
|
|
159
|
+
|
|
160
|
+
// TODO(bradfitz): this was copied from x/build/cmd/buildlet/buildlet.go
|
|
161
|
+
// but there were some buildlet-specific bits in there, so the code is
|
|
162
|
+
// forked for now. Unfork and add some opts arguments here, so the
|
|
163
|
+
// buildlet can use this code somehow.
|
|
164
|
+
|
|
165
|
+
// Untar reads the gzip-compressed tar file from r and writes it into dir.
|
|
166
|
+
func Untar(r io.Reader, dir string) error {
|
|
167
|
+
return untar(r, dir)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
func untar(r io.Reader, dir string) (err error) {
|
|
171
|
+
t0 := time.Now()
|
|
172
|
+
nFiles := 0
|
|
173
|
+
madeDir := map[string]bool{}
|
|
174
|
+
// defer func() {
|
|
175
|
+
// td := time.Since(t0)
|
|
176
|
+
// if err == nil {
|
|
177
|
+
// log.Printf("extracted tarball into %s: %d files, %d dirs (%v)", dir, nFiles, len(madeDir), td)
|
|
178
|
+
// } else {
|
|
179
|
+
// log.Printf("error extracting tarball into %s after %d files, %d dirs, %v: %v", dir, nFiles, len(madeDir), td, err)
|
|
180
|
+
// }
|
|
181
|
+
// }()
|
|
182
|
+
zr, err := gzip.NewReader(r)
|
|
183
|
+
if err != nil {
|
|
184
|
+
return fmt.Errorf("requires gzip-compressed body: %v", err)
|
|
185
|
+
}
|
|
186
|
+
tr := tar.NewReader(zr)
|
|
187
|
+
loggedChtimesError := false
|
|
188
|
+
for {
|
|
189
|
+
f, err := tr.Next()
|
|
190
|
+
if err == io.EOF {
|
|
191
|
+
break
|
|
192
|
+
}
|
|
193
|
+
if err != nil {
|
|
194
|
+
// log.Printf("tar reading error: %v", err)
|
|
195
|
+
return fmt.Errorf("tar error: %v", err)
|
|
196
|
+
}
|
|
197
|
+
if !validRelPath(f.Name) {
|
|
198
|
+
return fmt.Errorf("tar contained invalid name error %q", f.Name)
|
|
199
|
+
}
|
|
200
|
+
rel := filepath.FromSlash(f.Name)
|
|
201
|
+
abs := filepath.Join(dir, rel)
|
|
202
|
+
|
|
203
|
+
fi := f.FileInfo()
|
|
204
|
+
mode := fi.Mode()
|
|
205
|
+
switch {
|
|
206
|
+
case mode.IsRegular():
|
|
207
|
+
// Make the directory. This is redundant because it should
|
|
208
|
+
// already be made by a directory entry in the tar
|
|
209
|
+
// beforehand. Thus, don't check for errors; the next
|
|
210
|
+
// write will fail with the same error.
|
|
211
|
+
dir := filepath.Dir(abs)
|
|
212
|
+
if !madeDir[dir] {
|
|
213
|
+
if err := os.MkdirAll(filepath.Dir(abs), 0755); err != nil {
|
|
214
|
+
return err
|
|
215
|
+
}
|
|
216
|
+
madeDir[dir] = true
|
|
217
|
+
}
|
|
218
|
+
wf, err := os.OpenFile(abs, os.O_RDWR|os.O_CREATE|os.O_TRUNC, mode.Perm())
|
|
219
|
+
if err != nil {
|
|
220
|
+
return err
|
|
221
|
+
}
|
|
222
|
+
n, err := io.Copy(wf, tr)
|
|
223
|
+
if closeErr := wf.Close(); closeErr != nil && err == nil {
|
|
224
|
+
err = closeErr
|
|
225
|
+
}
|
|
226
|
+
if err != nil {
|
|
227
|
+
return fmt.Errorf("error writing to %s: %v", abs, err)
|
|
228
|
+
}
|
|
229
|
+
if n != f.Size {
|
|
230
|
+
return fmt.Errorf("only wrote %d bytes to %s; expected %d", n, abs, f.Size)
|
|
231
|
+
}
|
|
232
|
+
modTime := f.ModTime
|
|
233
|
+
if modTime.After(t0) {
|
|
234
|
+
// Clamp modtimes at system time. See
|
|
235
|
+
// golang.org/issue/19062 when clock on
|
|
236
|
+
// buildlet was behind the gitmirror server
|
|
237
|
+
// doing the git-archive.
|
|
238
|
+
modTime = t0
|
|
239
|
+
}
|
|
240
|
+
if !modTime.IsZero() {
|
|
241
|
+
if err := os.Chtimes(abs, modTime, modTime); err != nil && !loggedChtimesError {
|
|
242
|
+
// benign error. Gerrit doesn't even set the
|
|
243
|
+
// modtime in these, and we don't end up relying
|
|
244
|
+
// on it anywhere (the gomote push command relies
|
|
245
|
+
// on digests only), so this is a little pointless
|
|
246
|
+
// for now.
|
|
247
|
+
// log.Printf("error changing modtime: %v (further Chtimes errors suppressed)", err)
|
|
248
|
+
loggedChtimesError = true // once is enough
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
nFiles++
|
|
252
|
+
case mode.IsDir():
|
|
253
|
+
if err := os.MkdirAll(abs, 0755); err != nil {
|
|
254
|
+
return err
|
|
255
|
+
}
|
|
256
|
+
madeDir[abs] = true
|
|
257
|
+
case f.Typeflag == tar.TypeSymlink:
|
|
258
|
+
// leafac: Added by me to support symbolic links. Adapted from https://github.com/mholt/archiver/blob/v3.5.0/tar.go#L254-L276 and https://github.com/mholt/archiver/blob/v3.5.0/archiver.go#L313-L332
|
|
259
|
+
err := os.MkdirAll(filepath.Dir(abs), 0755)
|
|
260
|
+
if err != nil {
|
|
261
|
+
return fmt.Errorf("%s: making directory for file: %v", abs, err)
|
|
262
|
+
}
|
|
263
|
+
_, err = os.Lstat(abs)
|
|
264
|
+
if err == nil {
|
|
265
|
+
err = os.Remove(abs)
|
|
266
|
+
if err != nil {
|
|
267
|
+
return fmt.Errorf("%s: failed to unlink: %+v", abs, err)
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
err = os.Symlink(f.Linkname, abs)
|
|
272
|
+
if err != nil {
|
|
273
|
+
return fmt.Errorf("%s: making symbolic link for: %v", abs, err)
|
|
274
|
+
}
|
|
275
|
+
default:
|
|
276
|
+
return fmt.Errorf("tar file entry %s contained unsupported file type %v", f.Name, mode)
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
return nil
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
func validRelativeDir(dir string) bool {
|
|
283
|
+
if strings.Contains(dir, `\`) || path.IsAbs(dir) {
|
|
284
|
+
return false
|
|
285
|
+
}
|
|
286
|
+
dir = path.Clean(dir)
|
|
287
|
+
if strings.HasPrefix(dir, "../") || strings.HasSuffix(dir, "/..") || dir == ".." {
|
|
288
|
+
return false
|
|
289
|
+
}
|
|
290
|
+
return true
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
func validRelPath(p string) bool {
|
|
294
|
+
if p == "" || strings.Contains(p, `\`) || strings.HasPrefix(p, "/") || strings.Contains(p, "../") {
|
|
295
|
+
return false
|
|
296
|
+
}
|
|
297
|
+
return true
|
|
298
|
+
}
|