Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

feat: detect invalid binaries and show warning #1124

Merged
merged 4 commits into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 11 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@
},
"dependencies": {
"@babel/parser": "7.16.8",
"@netlify/binary-info": "^1.0.0",
"@netlify/esbuild": "0.14.25",
"@vercel/nft": "^0.20.0",
"archiver": "^5.3.0",
"common-path-prefix": "^3.0.0",
"cp-file": "^9.0.0",
"del": "^6.0.0",
"elf-cam": "^0.1.1",
"end-of-stream": "^1.4.4",
"es-module-lexer": "^0.10.0",
"execa": "^5.0.0",
Expand Down
22 changes: 18 additions & 4 deletions src/runtimes/detect_runtime.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type { Buffer } from 'buffer'

import { detect, Runtime } from 'elf-cam'
import { detect, Runtime, Arch, Platform, BinaryInfo } from '@netlify/binary-info'

import { cachedReadFile, FsCache } from '../utils/fs.js'

import type { RuntimeName } from './runtime.js'

const isValidFunctionBinary = (info: BinaryInfo) => info.arch === Arch.Amd64 && info.platform === Platform.Linux

// Try to guess the runtime by inspecting the binary file.
export const detectBinaryRuntime = async function ({
fsCache,
Expand All @@ -20,15 +22,27 @@ export const detectBinaryRuntime = async function ({
// We're using the Type Assertion because the `cachedReadFile` abstraction
// loses part of the return type information. We can safely say it's a
// Buffer in this case because we're not specifying an encoding.
const binaryType = detect(buffer as Buffer)
const binaryInfo = detect(buffer as Buffer)

if (!isValidFunctionBinary(binaryInfo)) {
console.warn(`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

zip-it-and-ship-it can be invoked as a binary, which prints a JSON string to stdout. For this reason, we don't typically print any additional messages because that'll technically make the output incorrect. To get around this, we've been adding properties to the output and then reading them from Netlify Build and/or CLI and show warning/error messages accordingly.

This is something we should revisit, because I don't think we're actually using the binary version anywhere, but I still wanted to mention it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, can you point me to something? I cannot find any other case where we supply warnings/errors in the json response.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@danez danez Jun 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem here is that I cannot do it this way because the function is never actually returned by zisi. It is simply ignored. Now I could change that and mark it as invalid/incompatible and handle the ignoring outside of zisi, but that is a pretty hefty breaking change for just a simple warning.
I would rather try to remove the bin of zisi altogether and keep the printing as is :)

Or not do the warning at all.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see. Agreed, let's not overcomplicate things! I think it's fine to ship this as is, and then look into removing the binary separately.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the logic slightly to simply not print the warning in cli mode.

Found incompatible prebuilt function binary in ${path}.
The binary needs to be built for Linux/Amd64, but it was built for ${Platform[binaryInfo.platform]}/${
Arch[binaryInfo.arch]
}`)

return undefined
}

switch (binaryType) {
switch (binaryInfo.runtime) {
case Runtime.Go:
return 'go'
case Runtime.Rust:
return 'rs'
default:
return undefined
}
} catch {}
} catch {
// Possible errors are: non binary files, arch/platforms not supported by binary-info, path is directory
}
}
Binary file not shown.
11 changes: 11 additions & 0 deletions tests/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2692,3 +2692,14 @@ test('listFunctionsFiles includes in-source config declarations', async (t) => {
t.is(func.schedule, '@daily')
})
})

test('listFunctionsFiles does not include wrong arch functions and warns', async (t) => {
sinon.spy(console, 'warn')
const functions = await listFunctionsFiles(join(FIXTURES_DIR, 'wrong-prebuilt-architecture'))

t.is(functions.length, 0)
t.is(console.warn.called, true)
t.is(console.warn.calledWith(sinon.match(/Darwin\/Arm64/)), true)

console.warn.restore()
})