forked from nowsecure/node-applesign
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdepsolver.js
More file actions
206 lines (194 loc) · 5.12 KB
/
depsolver.js
File metadata and controls
206 lines (194 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
'use strict';
const fatmacho = require('fatmacho');
const macho = require('macho');
const uniq = require('uniq');
const fs = require('fs');
const isArray = require('is-array');
const MACH0_MIN_SIZE = 1024 * 4;
function resolveRpath (libs, file, lib) {
const realLib = lib.substring(6); /* chop @rpath */
const rpaths = uniq(libs.filter((x) => {
return x.indexOf('dylib') !== -1;
}).map((x) => {
return x.substring(0, x.lastIndexOf('/'));
}));
rpaths.forEach((x) => {
try {
const paz = x + realLib;
fs.statSync(paz);
return lib.replace('@rpath', paz);
} catch (e) {
/* ignored */
}
});
if (rpaths.length > 0) {
return rpaths[0] + realLib;
}
return realLib;
}
function resolveEpath (file, lib) {
const sl4sh = file.lastIndexOf('/');
const rpath = (sl4sh !== -1) ? file.substring(0, sl4sh) : '';
return lib.replace('@executable_path', rpath);
}
function resolveLpath (file, lib) {
const sl4sh = file.lastIndexOf('/');
const rpath = (sl4sh !== -1) ? file.substring(0, sl4sh) : '';
return lib.replace('@loader_path', rpath);
}
function resolvePath (executable, file, lib, libs) {
if (lib.startsWith('@rpath')) {
return resolveRpath(libs, file, lib);
}
if (lib.startsWith('@executable_path')) {
return resolveEpath(executable, lib);
}
if (lib.startsWith('@loader_path')) {
return resolveLpath(file, lib);
}
throw new Error('Cannot resolve rpath');
}
function getMachoLibs (file, cb) {
try {
fs.readFile(file, (err, data) => {
if (err) {
console.error(err);
return cb(err);
}
if (data.length < MACH0_MIN_SIZE) {
return cb(new Error('mach0 files can\'t be that small'));
}
var exec = null;
try {
exec = macho.parse(data);
} catch (e) {
try {
var fat = fatmacho.parse(data);
} catch (e2) {
return cb(e2);
}
for (let i = 0; i < fat.length; i++) {
try {
exec = macho.parse(fat[0].data);
break;
} catch (e2) {
/* ignore exceptions here */
console.error(file);
console.error(e2);
return cb(null, []);
}
}
}
if (exec === null) {
/* skip fat file here*/
return cb(null, []);
}
const libs = exec.cmds.filter((x) => {
return x.type === 'load_dylib' || x.type === 'load_weak_dylib';
}).map((x) => {
return x.name;
});
cb(null, libs);
});
} catch (e) {
cb(e);
}
}
function layerize (state) {
let currentLayer = 0;
const result = [];
// fs.writeFileSync('lala.json', JSON.stringify(state));
let processing = false;
do {
result[currentLayer] = [];
for (let lib of Object.keys(state)) {
const deps = state[lib].deps;
if (deps.length === 0) {
if (state[lib].layer === -1) {
result[currentLayer].push(lib);
state[lib].layer = 0;
}
}
let allDepsSolved = true;
for (let dep of deps) {
const depLayer = state[dep]? state[dep].layer: 0;
if (depLayer === -1 || depLayer === currentLayer) {
allDepsSolved = false;
break;
}
}
processing = true;
if (allDepsSolved) {
if (state[lib].layer === -1) {
result[currentLayer].push(lib);
state[lib].layer = currentLayer;
}
processing = false;
}
}
currentLayer++;
} while (processing);
return result;
}
function flattenize (layers) {
const list = [];
for (let layer of layers) {
for (let lib of layer) {
list.push(lib);
}
}
return list;
}
module.exports = function depSolver (executable, libs, parallel, cb) {
if (libs.length === 0) {
return cb(null, []);
}
const state = {};
let peekableLibs = libs.slice(0);
const peek = () => {
const target = peekableLibs.pop();
getMachoLibs(target, (error, macholibs) => {
if (target === undefined || error || !isArray(macholibs)) {
return cb(error);
}
state[target] = {
layer: -1,
deps: []
};
for (let r of macholibs) {
if (!r.startsWith('/')) {
const realPath = resolvePath(executable, target, r, libs);
try {
fs.statSync(realPath);
state[target].deps.push(realPath);
} catch (e) {
}
}
}
if (peekableLibs.length === 0) {
const layers = layerize(state);
if (parallel) {
cb(null, layers);
} else {
const finalLibs = flattenize(layers);
if (libs.length !== finalLibs.length) {
console.log('Orphaned libraries found');
const orphaned = libs.filter(lib => finalLibs.indexOf(lib) === -1);
orphaned.forEach(lib => {
console.log(' *', lib);
});
/*
* sign those anyways, just ensure to
* sign them before the app executable
*/
finalLibs.unshift(...orphaned);
}
cb(null, finalLibs);
}
} else {
peek();
}
});
};
peek();
};