Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Detect and block a binary file

Is it possible to write a script in Adaptavist ScriptRunner which detects any binary file and denies commit which includes the file?

3 answers

1 accepted

2 votes
Answer accepted

Hi Piotr,

You can do this with ScriptRunner for Bitbucket Server. You need to use the pre-receive hook "protect git references" . You can enable this by going to Admin -> Pre-receive hooks -> Protect git references. The documentation for that is here.

You'll need to use a condition like the following on that hook to exclude the binary file extensions: 

pathsMatch('regex:.*\\.jar$|.*\\.exe$')

The example above shows you how to do it for jar and exe files.

You can also use the "restrict file size" hook here to block files based on file size, which you may find useful to combine with the rule above.

Alternatively you can use the following as a custom pre-receive hook, but it may not catch every case. You should try it and see if it works for you:

import com.atlassian.bitbucket.commit.CommitService
import com.atlassian.bitbucket.content.AbstractDiffContentCallback
import com.atlassian.bitbucket.content.DiffRequest
import com.atlassian.bitbucket.content.Path
import com.atlassian.bitbucket.hook.HookResponse
import com.atlassian.bitbucket.repository.RefChange
import com.atlassian.bitbucket.repository.RefChangeType
import com.atlassian.bitbucket.repository.Repository
import com.atlassian.sal.api.component.ComponentLocator
import com.onresolve.scriptrunner.canned.bitbucket.util.BitbucketCannedScriptUtils

import javax.annotation.Nullable

def commitService = ComponentLocator.getComponent(CommitService)

def repository = repository as Repository
def refChanges = refChanges as Collection<RefChange>
def hookResponse = hookResponse as HookResponse

def msg = new StringBuilder()

refChanges.each { refChange ->
if (refChange.type != RefChangeType.DELETE) {

def commits = refChange.getCommits(repository)
if (!commits) {
// empty branch
return
}
def diffRequestBuilder = new DiffRequest.Builder(repository, refChange.toHash)

commits.first().parents.each {
diffRequestBuilder.sinceId(it.id)
}

commitService.streamDiff(diffRequestBuilder.build(), new AbstractDiffContentCallback() {
@Override
void onBinary(@Nullable Path src, @Nullable Path dst) throws IOException {
msg << "Binary file $dst not allowed - on branch: ${refChange.ref.id}"
super.onBinary(src, dst)
}
})
}
}

if (msg) {
hookResponse.out().write BitbucketCannedScriptUtils.wrapHookResponse(msg)
return false
}

return true

Hope this helps.

Adam

Hi Adam,

Thank you for your answer.

I guess relying on file extensions is the only way here - it is not possible to read a file somehow using ScriptRunner for BitBucket Server and detect any binary file, right?

 

Thanks,

Piotr

Hi Piotr, 

You can do this but you need to perform a diff. I've updated my answer above. There are some cases which it may miss, but this is what Atlassian uses internally in Bitbucket to detect binary files.

Thanks,
Adam

 

Thank you !

0 votes

Hi Piotr,

ScriptRunner for Bitbucket provides a Pre-receive hook to restrict files by file size. Refer to https://scriptrunner.adaptavist.com/4.3.0/bitbucket/PreReceiveHooks.html

You can write a custom script if you would like to check for file types.

Cheers

Bhushan

Hi Adam,

Thank you for your answer.

I guess relying on file extensions is the only way here - it is not possible to read a file somehow using ScriptRunner for BitBucket Server and detect any binary file, right?

 

Thanks,

Piotr

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Marketplace Apps & Integrations

Do Not Lose your Customer’s Trust

Missing deadlines is one of the biggest problems every team lead wants to avoid when dealing with managed services. When the customer contracts your company to help with IT services it is expected th...

24 views 0 0
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you