CybercentreCanada / assemblyline

AssemblyLine 4: File triage and malware analysis
https://cybercentrecanada.github.io/assemblyline4_docs/
MIT License
243 stars 15 forks source link

Bug:[assemblyline-core:v4.5.0.] stable35 to stable43 -- bugfix missing #255

Closed kulukami closed 1 month ago

kulukami commented 2 months ago

Describe the bug assemblyline-core PR#909 is shown in v4.5.0.stable17 but missing from v4.5.0.stable35. And bug still exists in v4.5.0.stable43

UnboundLocalError: 
    cannot access local variable 'tag_name' where it is not associated with a value

To Reproduce

  1. assemblyline-core/updater/helper.py 'v4.5.0.stable17'

    def _get_dockerhub_tags(image_name, update_channel, proxies=None):
    # Find latest tag for each types
    rv = []
    url = f"https://{DEFAULT_DOCKER_REGISTRY}/v2/repositories/{image_name}/tags" \
          f"?page_size=50&page=1&name={update_channel}"
    while True:
        resp = requests.get(url, proxies=proxies)
        if resp.ok:
            resp_data = resp.json()
            rv.extend([x['name'] for x in resp_data['results']])
            # Page until there are no results left
            url = resp_data.get('next', None)
            if url is None:
                break
        else:
            break
    
    return rv
  2. assemblyline-core/updater/helper.py 'v4.5.0.stable43'

tags not init with []

def _get_dockerhub_tags(image_name, update_channel, prefix, proxies=None, docker_registry=DEFAULT_DOCKER_REGISTRY,
                        logger: Logger=None):
    """
    Retrieve DockerHub tags for a specific image and update channel.
    Args:
    image_name (str): Full name of the image (including namespace).
    update_channel (str): Specific channel to filter tags (e.g., stable, beta).
    proxies (dict, optional): Proxy configuration.
    docker_registry (str, optional): Docker registry URL. Defaults to DEFAULT_DOCKER_REGISTRY.
    Returns:
    list: A list of tag names that match the given criteria, or None if an error occurs.
    """
    namespace, repository = image_name.split('/', 1)
    base_url = f"https://{docker_registry}/v2/namespaces/{namespace}/repositories/{repository}/tags"
    query_params = {
        'page_size': 50,
        'page': 1,
        'name': update_channel,
        'ordering': 'last_updated'
    }
    url = f"{base_url}?{urlencode(query_params)}"
    while True:
        try:
            response = requests.get(url, proxies=proxies)
            response.raise_for_status()  # Raises an HTTPError for bad responses
            response_data = response.json()
            tags = []
            if namespace == "cccs":
                # The tags are coming from a repository managed by the Assemblyline team
                for tag_info in response_data['results']:
                    tag_name = tag_info.get('name')
                    if tag_name and tag_name == f"{FRAMEWORK_VERSION}.{SYSTEM_VERSION}.{update_channel}":
                        # Ignore tag aliases containing the update_channel
                        continue
                    if tag_name and tag_name.startswith(f"{FRAMEWORK_VERSION}.{SYSTEM_VERSION}"):
                        # Because the order of paging is based on `last_updated`,
                        # we can return the first valid candidate
                        logger.info(f"{prefix}Valid tag found for {repository}: {tag_name}")
                        return [tag_name]
            else:
                # The tags are coming from another repository, so we're don't know the ordering of tags
                tags.extend(tag_info.get('name') for tag_info in response_data['results'] if tag_info.get('name'))
            url = response_data.get('next')
            if not url:
                logger.info(f"{prefix}No more pages left to fetch.")
                break
        except requests.exceptions.HTTPError as e:
            if response.status_code == 429:
                # Based on https://docs.docker.com/docker-hub/api/latest/#tag/rate-limiting
                # We've hit the rate limit so we have to wait and try again later
                logger.warning(f"{prefix}Rate limit reached for DockerHub. Retrying after sleep..")
                time.sleep(int(response.headers['retry-after']) - int(time.time()))
            else:
                logger.error(f"{prefix}HTTP error occurred: {e}")
            break
        except requests.exceptions.ConnectionError as e:
            logger.error(f"{prefix}Connection error occurred: {e}")
            break
        except requests.exceptions.Timeout as e:
            logger.error(f"{prefix}Request timed out.")
            break
        except requests.exceptions.RequestException as e:
            logger.error(f"{prefix}An error occurred during requests: {e}")
            break
        except ValueError as e:
            logger.error(f"{prefix}JSON decode error: {e}")
            break
    if tags:
        logger.info(f"{prefix}Tags retrieved successfully: {tags}")
    return tags
  1. assemblyline-core 'v4.5.0.stable17' commit
cccs-rs commented 1 month ago

Patch should be included in 4.5.0.44 release